Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call init_doc after every metadata change #3555

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ def get_area(*args) -> float:
return f * rect.width * rect.height


def set_metadata(doc: pymupdf.Document, m: dict) -> None:
def set_metadata(doc: pymupdf.Document, m: dict = None) -> None:
"""Update the PDF /Info object.

Args:
Expand All @@ -1202,7 +1202,9 @@ def set_metadata(doc: pymupdf.Document, m: dict) -> None:
raise ValueError("is no PDF")
if doc.is_closed or doc.is_encrypted:
raise ValueError("document closed or encrypted")
if type(m) is not dict:
if m is None:
m = {}
elif type(m) is not dict:
raise ValueError("bad metadata")
keymap = {
"author": "Author",
Expand Down Expand Up @@ -1238,6 +1240,7 @@ def set_metadata(doc: pymupdf.Document, m: dict) -> None:
doc.xref_set_key(-1, "Info", "%i 0 R" % info_xref)
elif m == {}: # remove existing metadata
doc.xref_set_key(-1, "Info", "null")
doc.init_doc()
return

for key, val in [(k, v) for k, v in m.items() if keymap[k] is not None]:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_3237():
metadata1 = doc.metadata
metadata1 = repr(metadata1).encode('utf8')
doc.set_metadata({})
doc.init_doc()

metadata2 = doc.metadata
metadata2 = repr(metadata2).encode('utf8')
print(f'{metadata1=}')
Expand Down
Loading