Skip to content

Commit

Permalink
ROB: Let PdfWriter.merge cope with missing "/Fields" (#1628)
Browse files Browse the repository at this point in the history
Fixes #1627
  • Loading branch information
pubpub-zz authored Feb 25, 2023
1 parent 2204fd6 commit f98ff02
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
17 changes: 10 additions & 7 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2618,13 +2618,16 @@ def merge(
cast(DictionaryObject, self._root_object["/AcroForm"])["/Fields"],
)
trslat = self._id_translated[id(reader)]
for f in reader.trailer["/Root"]["/AcroForm"]["/Fields"]: # type: ignore
try:
ind = IndirectObject(trslat[f.idnum], 0, self)
if ind not in arr:
arr.append(ind)
except KeyError:
pass
try:
for f in reader.trailer["/Root"]["/AcroForm"]["/Fields"]: # type: ignore
try:
ind = IndirectObject(trslat[f.idnum], 0, self)
if ind not in arr:
arr.append(ind)
except KeyError: # for trslat[] which mean the field has not be copied through the page
pass
except KeyError: # for /Acroform or /Fields are not existing
arr = self._add_object(ArrayObject())
cast(DictionaryObject, self._root_object["/AcroForm"])[
NameObject("/Fields")
] = arr
Expand Down
24 changes: 20 additions & 4 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,14 +740,30 @@ def test_sweep_indirect_references_nullobject_exception():

@pytest.mark.external
@pytest.mark.slow
def test_write_outline_item_on_page_fitv():
url = "https://corpora.tika.apache.org/base/docs/govdocs1/922/922840.pdf"
name = "tika-922840.pdf"
@pytest.mark.parametrize(
("url", "name"),
[
(
"https://corpora.tika.apache.org/base/docs/govdocs1/924/924666.pdf",
"test_sweep_indirect_references_nullobject_exception.pdf",
),
(
"https://corpora.tika.apache.org/base/docs/govdocs1/922/922840.pdf",
"test_write_outline_item_on_page_fitv.pdf",
),
("https://github.com/py-pdf/pypdf/files/10715624/test.pdf", "iss1627.pdf"),
],
)
def test_some_appends(url, name):
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
# PdfMerger
merger = PdfMerger()
merger.append(reader)
merger.write("tmp-merger-do-not-commit.pdf")

# PdfWriter
merger = PdfWriter()
merger.append(reader)
merger.write("tmp-merger-do-not-commit.pdf")
# cleanup
os.remove("tmp-merger-do-not-commit.pdf")

Expand Down

0 comments on commit f98ff02

Please sign in to comment.