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

Simply duplicate fillable pdf but can't get fields() from the new one. #1792

Closed
remove158 opened this issue Apr 15, 2023 · 7 comments · Fixed by #1806
Closed

Simply duplicate fillable pdf but can't get fields() from the new one. #1792

remove158 opened this issue Apr 15, 2023 · 7 comments · Fixed by #1806
Labels
Easy This issue is a good starting point for first-time contributors nf-documentation Non-functional change: Documentation

Comments

@remove158
Copy link

remove158 commented Apr 15, 2023

I just simply duplicate fillable pdf form using PdfWriter.add_page. After that I open the new one with PdfReader. Now the new pdf return None from reader.get_fields()

  1. My first fillable form has many fields when I use reader.get_fields()
  2. The new one return None (or empty dict {} in case I update the old one with writer.update_page_form_field_values(writer.pages[0], {}) before saving)

Environment

Which environment were you using when you encountered the problem?

pypdf2 = "^3.0.1"
python = "^3.11"

Code + PDF

This is a minimal, complete example that shows the issue:

from PyPDF2 import PdfReader, PdfWriter

source =  'tests/jt32.pdf'
out = 'output.pdf'
reader = PdfReader(source)
writer = PdfWriter()

fields = reader.get_fields()
print(fields)
for i in range(len(reader.pages)):
    writer.add_page(reader.pages[i])
    

with open(out, 'wb') as output_file:
    writer.write(output_file)

reader_2 = PdfReader(out)
print(reader_2.get_fields())

pdf: https://cdn.discordapp.com/attachments/1082352569973751868/1096945829291503676/jt32.pdf

Share here the PDF file(s) that cause the issue. The smaller they are, the
better. Let us know if we may add them to our tests!

Traceback

This is the complete Traceback I see:
Noting error !!

@MartinThoma
Copy link
Member

PyPDF2 is deprecated. Use pypdf.

@remove158
Copy link
Author

remove158 commented Apr 16, 2023

PyPDF2 is deprecated. Use pypdf.

Thanks for the quick reply, I've tried both pypdf, pypdf2, it doesn't matter which version. Both versions produce the same results.

python = "^3.11"
pypdf = "^3.7.1"
pypdf2 = "^3.0.1"

@pubpub-zz
Copy link
Collaborator

fields are part of the PDF structure not pages. try append() adding all the pages at once instead of add_page()

@MartinThoma
Copy link
Member

Thanks for the quick reply, I've tried both pypdf, pypdf2, it doesn't matter which version. Both versions produce the same results.

That is not the point. When you use deprecated software you cannot expect any support. Development will continue with pypdf. The project is called pypdf, not PyPDF2. We don't have the resources to maintain two projects. By directly using the maintained project you will get better results (at least in some cases, see CHANGELOG) and it helps us to spend our time more effectively.

@neversphere
Copy link
Contributor

fields are part of the PDF structure not pages. try append() adding all the pages at once instead of add_page()

The sample code in the docs doesn't mention this (https://pypdf.readthedocs.io/en/stable/user/forms.html?highlight=Interactions%20with%20PDF%20Forms). Does it need to be updated?

@MartinThoma
Copy link
Member

Adding some information there is certainly a good idea! Do you want to open a PR @neversphere ? 😇

@MartinThoma MartinThoma added nf-documentation Non-functional change: Documentation Easy This issue is a good starting point for first-time contributors labels Apr 21, 2023
@neversphere
Copy link
Contributor

#1806

MartinThoma pushed a commit that referenced this issue Apr 23, 2023
Update docs to use append rather than add_page so that the PDF structure is copied. This ensures the form structure is kept which is necessary for some PDFs.

See #1790
Closes #1792
Closes #1804
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Easy This issue is a good starting point for first-time contributors nf-documentation Non-functional change: Documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants