Skip to content

BUG: Write /Root/AcroForm in set_need_appearances_writer#1639

Merged
MartinThoma merged 1 commit intopy-pdf:mainfrom
cryzed:main
Mar 5, 2023
Merged

BUG: Write /Root/AcroForm in set_need_appearances_writer#1639
MartinThoma merged 1 commit intopy-pdf:mainfrom
cryzed:main

Conversation

@cryzed
Copy link
Copy Markdown
Contributor

@cryzed cryzed commented Feb 16, 2023

PdfWriter.set_need_appearances_writer() (whether called directly or indirectly by PdfWriter.update_page_form_field_values()) fails to create the /Root/AcroForm object correctly when it doesn't already exist in the PdfWriter object.

See my comment here for more details.

Fixes #355

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 16, 2023

Codecov Report

Base: 91.92% // Head: 91.92% // Decreases project coverage by -0.01% ⚠️

Coverage data is based on head (26bdba2) compared to base (4e276b2).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1639      +/-   ##
==========================================
- Coverage   91.92%   91.92%   -0.01%     
==========================================
  Files          33       33              
  Lines        6375     6374       -1     
  Branches     1272     1272              
==========================================
- Hits         5860     5859       -1     
  Misses        327      327              
  Partials      188      188              
Impacted Files Coverage Δ
pypdf/_writer.py 84.52% <100.00%> (-0.02%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@cryzed
Copy link
Copy Markdown
Contributor Author

cryzed commented Feb 17, 2023

Nevermind, doesn't quite work yet, seemingly only renders the first page correctly in Adobe Acrobat.

@cryzed cryzed closed this Feb 17, 2023
@cryzed
Copy link
Copy Markdown
Contributor Author

cryzed commented Feb 19, 2023

I'll reopen this, because this does seem to fix /Root/AcroForm getting generated incorrectly and thus the proper setting of the /NeedAppearances flag. With this at least, it's possible to render the first page correctly in Adobe Reader (since Adobe Reader has a bug that only respects the flag for the first page of a document) and all pages in most other PDF readers.

@cryzed cryzed reopened this Feb 19, 2023
@pubpub-zz
Copy link
Copy Markdown
Collaborator

@cryzed,
can you provide a pdf that requires this change: I do not mean the original code is correct but I would have had expected the /AcroFrom being already correcty genenated as the /Fields should be correct

@cryzed
Copy link
Copy Markdown
Contributor Author

cryzed commented Feb 19, 2023

Get_Started_With_Smallpdf.pdf

# Taken from https://pypdf.readthedocs.io/en/stable/user/forms.html
from pypdf import PdfReader, PdfWriter

reader = PdfReader("Get_Started_With_Smallpdf.pdf")
writer = PdfWriter()

page = reader.pages[0]
fields = reader.get_fields()

writer.add_page(page)

writer.update_page_form_field_values(
    writer.pages[0], {"test_field": "some filled in text"}
)

# write "output" to pypdf-output.pdf
with open("filled-out.pdf", "wb") as output_stream:
    writer.write(output_stream)

PdfWriter.set_need_appearances_writer() is called implicitly by PdfWriter.update_page_form_field_values(). Open filled-out.pdf in Adobe Reader and it won't render any appearances, even on the first page. Same thing happens in Okular.

@pubpub-zz
Copy link
Copy Markdown
Collaborator

pubpub-zz commented Feb 19, 2023

add_page() is a quite low level function to add page. I recomment to use .append() which is easier to use and more powerfull.

import pyppdf

w = pypdf.PdfWriter()
w.append("filledGet_Started_With_Smallpdf.pdf", [0])
w.update_page_form_field_values(w.pages[0], {"test_field": "some filled in text"})
w.write("tt.pdf")

Although your change is a good idea 😊

@yearski
Copy link
Copy Markdown

yearski commented Mar 3, 2023

This patch does work for me in Adobe Acrobat (2022.003.20322), PDF-XChange and Edge browser but does not work in Chrome or Vivaldi browser.

@MartinThoma MartinThoma changed the title Write /Root/AcroForm correctly (Fixes #355) BUG: Write /Root/AcroForm correctly Mar 5, 2023
@MartinThoma MartinThoma requested a review from pubpub-zz March 5, 2023 15:01
@MartinThoma
Copy link
Copy Markdown
Member

@pubpub-zz Do you think this PR improves pypdf? Or is there an issue we need to address before merging?

@MartinThoma MartinThoma added the is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF label Mar 5, 2023
@pubpub-zz
Copy link
Copy Markdown
Collaborator

the new code is better than the old one. and may explain why Acrobat reader may have some difficulties to read some files. For me you can merge it.

@MartinThoma MartinThoma changed the title BUG: Write /Root/AcroForm correctly BUG: Write /Root/AcroForm in set_need_appearances_writer Mar 5, 2023
@MartinThoma MartinThoma merged commit 8b0f091 into py-pdf:main Mar 5, 2023
@MartinThoma
Copy link
Copy Markdown
Member

@cryzed Thank you for your PR 🙏

I'm sorry it took so long until it was merged. I misunderstood a part of the conversation and thought there was an issue with the PR + I had some health issues (luckily all good again :-) )

Your fix will be part of the next release, likely on 12.05.2023 with pypdf>3.5.1

@MartinThoma
Copy link
Copy Markdown
Member

If you want, I can add you to https://pypdf.readthedocs.io/en/latest/meta/CONTRIBUTORS.html :-)

@MartinThoma MartinThoma added the workflow-forms From a users perspective, forms is the affected feature/workflow label Mar 5, 2023
@cryzed
Copy link
Copy Markdown
Contributor Author

cryzed commented Mar 5, 2023

If you want, I can add you to https://pypdf.readthedocs.io/en/latest/meta/CONTRIBUTORS.html :-)

Sure, sounds good to me, if you think this was an adequate contribution!

MartinThoma added a commit that referenced this pull request Mar 12, 2023
Bug Fixes (BUG)
-  compress_content_stream not readable in Adobe Acrobat (#1698)
-  Pass logging parameters correctly in set_need_appearances_writer (#1697)
-  Write /Root/AcroForm in set_need_appearances_writer (#1639)

Robustness (ROB)
-  Allow more whitespaces within linearized file (#1701)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF workflow-forms From a users perspective, forms is the affected feature/workflow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Updated pdf fields don't show up when page is written

4 participants