Skip to content

Commit

Permalink
Move form environment from page rendering to document level (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 authored Sep 1, 2022
1 parent 36c95aa commit 3500197
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/pypdfium2/_helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def __init__(
self._actual_input = input_data
self._rendering_input = None
self._ld_data = None
self._form_env = None
self._form_config = None

self._password = password
self._file_access = file_access
Expand Down Expand Up @@ -143,13 +145,32 @@ def close(self):
Close the document to release allocated memory.
This function shall be called when finished working with the object.
"""
if self._form_env is not None:
pdfium.FPDFDOC_ExitFormFillEnvironment(self._form_env)
id(self._form_config)
pdfium.FPDF_CloseDocument(self.raw)
if self._ld_data is not None:
self._ld_data.close()
if self._autoclose and is_input_buffer(self._actual_input):
self._actual_input.close()


def init_formenv(self):
"""
Initialise a form environment object for this document.
If already initialised, the existing one will be returned instead.
:meth:`.close` will free the form environment, if initialised.
Returns:
FPDF_FORMHANDLE:
"""
if self._form_env is None:
self._form_config = pdfium.FPDF_FORMFILLINFO()
self._form_config.version = 2
self._form_env = pdfium.FPDFDOC_InitFormFillEnvironment(self.raw, self._form_config)
return self._form_env


def get_version(self):
"""
Returns:
Expand Down
5 changes: 1 addition & 4 deletions src/pypdfium2/_helpers/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,8 @@ def render_base(
pdfium.FPDF_RenderPage_Close(self.raw)

if draw_forms:
form_info = pdfium.FPDF_FORMFILLINFO()
form_info.version = 1
form_env = pdfium.FPDFDOC_InitFormFillEnvironment(self.pdf.raw, form_info)
form_env = self.pdf.init_formenv()
pdfium.FPDF_FFLDraw(form_env, *render_args)
pdfium.FPDFDOC_ExitFormFillEnvironment(form_env)

return buffer, cl_string, (width, height)

Expand Down

0 comments on commit 3500197

Please sign in to comment.