Skip to content

Commit

Permalink
Call FPDF_LoadXFA() in init_forms(), warn on error
Browse files Browse the repository at this point in the history
FPDF_LoadXFA() currently seems to be broken in pdfium-binaries.
All the same, add the call so that pypdfium2 is prepared for an
eventual upstream fix.

Currently this only warns on error. Once the aforementioned issue
is fixed, it should be tightened to a PdfiumError exception.

See also #221 #222
  • Loading branch information
mara004 committed Jun 6, 2023
1 parent eb07605 commit 8de524e
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/pypdfium2/_helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,36 @@ def init_forms(self, config=None):
config (FPDF_FORMFILLINFO | None):
Custom form config interface to use (optional).
"""
if (self.get_formtype() == pdfium_c.FORMTYPE_NONE) or self.formenv:

formtype = self.get_formtype()
if formtype == pdfium_c.FORMTYPE_NONE or self.formenv:
return

if not config:

# safety check for older binaries (could be removed at some point)
# https://github.com/bblanchon/pdfium-binaries/issues/105
if V_PDFIUM_IS_V8 and V_BUILDNAME == "pdfium-binaries" and int(V_LIBPDFIUM) <= 5677:
# https://github.com/bblanchon/pdfium-binaries/issues/105
raise RuntimeError("V8 enabled pdfium-binaries builds <= 5677 crash on init_forms().")
config = pdfium_c.FPDF_FORMFILLINFO(version=2)

if V_PDFIUM_IS_V8:
js_platform = pdfium_c.IPDF_JSPLATFORM(version=3)
config = pdfium_c.FPDF_FORMFILLINFO(version=2, xfa_disabled=False, jsPlatform=js_platform)
else:
config = pdfium_c.FPDF_FORMFILLINFO(version=2)

raw = pdfium_c.FPDFDOC_InitFormFillEnvironment(self, config)
if not raw:
raise PdfiumError(f"Initializing form env failed for document {self}.")
self.formenv = PdfFormEnv(raw, config, self)
self._add_kid(self.formenv) # guarantees correct order of closing
self._add_kid(self.formenv)

if V_PDFIUM_IS_V8 and formtype in (pdfium_c.FORMTYPE_XFA_FOREGROUND, pdfium_c.FORMTYPE_XFA_FULL):
ok = pdfium_c.FPDF_LoadXFA(self)
if not ok:
# always fails as of this writing, thus warn instead of raising an exception for now
# probably this is due to an issue with pdfium-binaries - once fixed, this may be tightened to an exception
logger.warning(f"Failed to load XFA for document {self}.")


# TODO?(v5) consider cached property
Expand Down

0 comments on commit 8de524e

Please sign in to comment.