Skip to content

Commit

Permalink
call init_doc after every metadata change
Browse files Browse the repository at this point in the history
  • Loading branch information
JorjMcKie committed Jun 5, 2024
1 parent c31ba4f commit 6b5438f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
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

0 comments on commit 6b5438f

Please sign in to comment.