pypdfium2-team / pypdfium2

Python bindings to PDFium
https://pypdfium2.readthedocs.io/
349 stars 15 forks source link

The render func does not render form fields. #311

Closed Ezbaze closed 5 months ago

Ezbaze commented 5 months ago

Checklist

Description

For some reason the latest versions of pypdfium2 do not render form fields.

When rendering the image in 4.0.0: test_document

When rendering in versions after 4.0.0: ( Insert white rectangle :) )

Code used:

import pypdfium2 as pdfium
from PIL import Image

pdf = pdfium.PdfDocument("test_document.pdf") #Test doc atached allong with the issue

image: Image.Image = pdf[0].render(scale=1).to_pil()
image.save("test_document.png")

File used (Clean file with 1 form field): test_document.pdf

Install Info

C:\Users\Mate\Desktop\pypdfium2>python -m pypdfium2 -v
pypdfium2 4.29.0
pdfium 125.0.6406.0 at C:\Users\Mate\.pyenv\pyenv-win\versions\3.12.1\Lib\site-packages\pypdfium2_raw\pdfium.dll

C:\Users\Mate\Desktop\pypdfium2>python -VV
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]

C:\Users\Mate\Desktop\pypdfium2>python -c "import platform as p; print(p.platform())"
Windows-10-10.0.19045-SP0

C:\Users\Mate\Desktop\pypdfium2>python -m pip show pypdfium2
Name: pypdfium2
Version: 4.29.0
Summary: Python bindings to PDFium
Home-page: https://github.com/pypdfium2-team/pypdfium2
Author: pypdfium2-team
Author-email: geisserml@gmail.com
License: (Apache-2.0 OR BSD-3-Clause) AND LicenseRef-PdfiumThirdParty
Location: C:\Users\Mate\.pyenv\pyenv-win\versions\3.12.1\Lib\site-packages
Requires:
Required-by:

Validity

mara004 commented 5 months ago

You have to call pdf.init_forms() after document load, before page handle retrieval.

This was an exceptional API-breaking change in v4.1/4.2, due to issues in the previous API design, which did not correctly reflect pdfium's control flow. I resolved to do it because we were still early in the v4 series, where most embedders presumably hadn't migrated yet.

Ezbaze commented 5 months ago

I see, thanks for the reply <3

I could not find this when searching online ._.

mara004 commented 5 months ago

I could not find this when searching online ._.

The docs on init_forms() and render() [may_draw_forms] somewhat note it, but I agree it may be hard to find 😅

There were also entries in the changelog for v4.1/4.2 ("API-breaking changes around forms code, necessary to fix conceptual issues."), but maybe we should have formulated it more concretely.

Ezbaze commented 5 months ago

The docs on init_forms() and render() [may_draw_forms] somewhat note it

Thank you <3

but maybe we should have formulated it more concretely.

I think it's just the way it was worded, my search queries were "pypdfium2 form fields not rendering" and the results weren't helpful, hopefuly this comes up for anyone that stumles into the same issue :)