Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forms: appearance generation broken for autosized fields, fields with center alignment and unicode text #1919

Open
brzGatsu opened this issue Jun 26, 2023 · 9 comments
Assignees
Labels
workflow-forms From a users perspective, forms is the affected feature/workflow

Comments

@brzGatsu
Copy link

brzGatsu commented Jun 26, 2023

Hi @pubpub-zz, absolutely appreciate your effort in fixing py-pdfs form capabilities. I tested 3.11.1 and replaced pdftk with pypdf in our app. Its already looking quite good but there are still some appearance generation issues:

  • Text is always aligned left, even if the field is set to i.e. center (unless you click in the field)
  • Fields with font size set to auto are not showing at all (unless you click in the field)
  • Unicode characters such as the german Ü are not encoded properly (looking correct when clicking into the field). Not sure if it helps, but in our current code we create the fdf with this (and then hand it to pdftk) and have no encoding issues: https://github.com/ccnmtl/fdfgen/tree/master/fdfgen

Tested with current Acrobat Reader and Sumatra, happens on both.

Relates to:
#1306
#355

@brzGatsu brzGatsu changed the title Forms: appearance generation broken for autosized fields and fields with center alignment Forms: appearance generation broken for autosized fields, fields with center alignment and unicode text Jun 26, 2023
@pubpub-zz
Copy link
Collaborator

@brzGatsu.
When you are clicking on the fields the viewer is generating the appearance. this is why results looks good that way.
Can you provide some PDFs for point 1 and 2.

@pubpub-zz pubpub-zz added the needs-pdf The issue needs a PDF file to show the problem label Jun 29, 2023
@MartinThoma MartinThoma added the workflow-forms From a users perspective, forms is the affected feature/workflow label Jun 30, 2023
@brzGatsu
Copy link
Author

brzGatsu commented Jul 4, 2023

Sure, in this pdf you can observe all 3 problems:

  1. All the numbers should be center aligned
  2. The "Talente" column is set to auto font size, check the "Talente" cell in the row "Gebräuche" - its content only shows when clicking in the field.
  3. Same Row - the text Gebräuche is malformed.
    pypdf_forms_bug.pdf

@pubpub-zz pubpub-zz self-assigned this Jul 4, 2023
@pubpub-zz pubpub-zz removed the needs-pdf The issue needs a PDF file to show the problem label Jul 10, 2023
pubpub-zz added a commit to pubpub-zz/pypdf that referenced this issue Jul 22, 2023
@pubpub-zz
Copy link
Collaborator

the autosize has been fixed in PR #2002

@brzGatsu
Copy link
Author

Hi there, just want to let you know that a fix would still be still relevant for us - we are still stuck with PDFtk.

@stefan6419846
Copy link
Collaborator

As always, you are of course invited to further analyze this and propose a corresponding PR to fix this.

@brzGatsu
Copy link
Author

brzGatsu commented Jan 14, 2025

Hi @pubpub-zz,

I just tried again and was happy to see that unicode text is now supported. That leaves us with only one thing left: the alignment issue. I investigated a bit and saw, that when reading the fields (by iterating page.annotations) those that should have center alignment, have the "Q" flag set to 1. The pdf standard says Q stands for quadding (justification) and 1 is for centered. So it seems that the field is read correctly. However, when writing, this information seems to get lost and everything is left-aligned. The text is only centered while the field has keyboard focus.

@stefan6419846
Copy link
Collaborator

Could you please provide proper code and the corresponding PDF file(s) in question? I have not been able to find any code in this issue.

@brzGatsu
Copy link
Author

brzGatsu commented Jan 14, 2025

Hi, you can find an example PDF a few comments further up but I attached it again since pypdf threw some error with it. There is really not much to show in code, I basically copied the pypdf documentation on writing forms:

input.pdf

output.pdf

from pypdf import PdfReader, PdfWriter

reader = PdfReader("input.pdf")
writer = PdfWriter()
writer.append(reader)
writer.update_page_form_field_values(
    writer.pages[0],
    {"Fertigkeit1BA": "0"},
    auto_regenerate=False,
)

with open("output.pdf", "wb") as output_stream:
    writer.write(output_stream)

The zero in the "Basis" column of output.pdf should be centered

@SaintPeter
Copy link

With regards to form fields with the Auto font size, if you set the NeedAppearances flag on the PDF it will cause the viewer to refresh/resize form fields when the file is loaded.

writer.set_need_appearances_writer(True)

This doesn't resolve the alignment issue (which appears to be a miswriting of the PDF as described here: #1919 (comment)), but it does appear to fix the auto-sizing issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
workflow-forms From a users perspective, forms is the affected feature/workflow
Projects
None yet
Development

No branches or pull requests

5 participants