pymupdf / PyMuPDF

PyMuPDF is a high performance Python library for data extraction, analysis, conversion & manipulation of PDF (and other) documents.
https://pymupdf.readthedocs.io
GNU Affero General Public License v3.0
4.54k stars 447 forks source link

Widget font not being updated #3461

Closed boris-lok-pentadoc closed 1 month ago

boris-lok-pentadoc commented 1 month ago

Description of the bug

After updating Widgets value und calling update() method, the font value is reset, even though the font is present in FormFonts

How to reproduce the bug

Doc: VFNM-2018-05-22-mustervorlage-vorblatt.pdf

import fitz

doc = fitz.open("VFNM-2018-05-22-mustervorlage-vorblatt.pdf")
page = doc[0]
print(doc.FormFonts)
widgets = [i for i in page.widgets()]
print("orig", widgets[0].field_value)

widgets[0].field_value = "Hello! testing"
print("new", widgets[0].field_value)

widgets[0].text_font = "Arial"
print("new font",widgets[0].text_font)

widgets[0].update()
print("after update",widgets[0].field_value)
print("after update", widgets[0].text_font)

PyMuPDF version

1.24.2

Operating system

Linux

Python version

3.11

JorjMcKie commented 1 month ago

This is not a bug. PyMuPDF only supports Helvetica, Times-Roman, Courier and ZapfDingbats as fonts for rendering fields. This is independent from the form fonts that may be already present in the PDF (and which are extracted into FormFonts).

On updating a field, therefore automatically one of the supported 4 fonts is therefore chosen - ignoring any font specification that may already be there.

boris-lok-pentadoc commented 1 month ago

Thank you for your quick reply! I see.

I just found the wording in the documentation regarding it confusing, maybe I could submit a PR for it? It reads as if it is possible to use already embedded FormFonts

When specifying a text font for new or changed widgets, either choose one in the first table column (upper and lower case supported), or one of the already existing form fonts. In the latter case, spelling must exactly match.

To find out already existing field fonts, inspect the list Document.FormFonts.

JorjMcKie commented 1 month ago

I understand the confusion that this may cause. Thank you for your willingness to prepare a change in the documentation. Much appreciated!