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

Problem with closing PDF file saved by PyPDF2 #963

Closed
Kaemer1645 opened this issue Jun 9, 2022 · 16 comments · Fixed by #970
Closed

Problem with closing PDF file saved by PyPDF2 #963

Kaemer1645 opened this issue Jun 9, 2022 · 16 comments · Fixed by #970
Assignees
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF

Comments

@Kaemer1645
Copy link

I'm create PDF file using PyPDF2. After that when I'm oppening this file with Adobe Acrobat and I would like to close it. The window appears with messege like "Would you like to save this file?"
I haven't got this problem with previous versions of PyPDF2.

Environment

$ python -m platform
Windows-10-10.0.19044-SP0

$ python -c "import PyPDF2;print(PyPDF2.__version__)"
2.1.0

Code

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

from PyPDF2 import PdfFileWriter
import os

c_dir = os.getcwd()
path = os.path.join(c_dir,'test_2.pdf')
pdf = PdfFileWriter()
pdf.addBlankPage(100, 100)
pdf.write(open(path, 'wb'))

PDF

test_2.pdf

@Kaemer1645 Kaemer1645 added the is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF label Jun 9, 2022
@Kaemer1645
Copy link
Author

I checked which version work properly and that is:
pypdf==1.27.2
pypdf==1.27.3
pypdf==1.27.4
pypdf==1.27.5
everythng above work wrong.

@MartinThoma
Copy link
Member

Thank you for reporting the issue 🙏

I don't quite understand: Which versions work and which are broken?

@MartinThoma
Copy link
Member

Please note that pdf.write(open(path, 'wb')) is not good style. This leaves the file handle open. Instead, you should write:

with open(path, "wb") as fh:
    pdf.write(fh)

After the with-block, the file handle fh will be closed automatically by Python. I'm uncertain if this is related.

@Kaemer1645
Copy link
Author

I know use with style is more pythonic but using "with" it didn't work too.

The versions which work is
pypdf==1.27.2
pypdf==1.27.3
pypdf==1.27.4
pypdf==1.27.5
but every above this versions like 1.27.6 1.27.8... didn't work.

Code

# from PyPDF2 import PdfFileWriter
import os

c_dir = os.getcwd()
path = os.path.join(c_dir, 'test_10.pdf')
pdf = PdfFileWriter()
pdf.addBlankPage(100, 100)
file = open(path, 'wb')
pdf.write(file)
file.close()

pdf2 = PdfFileWriter()
path2 = os.path.join(c_dir, 'test_11.pdf')
with open(path2, "wb") as fh:
    pdf2.addBlankPage(100, 100)
    pdf2.write(fh)

Both work wrong on version >= 1.27.6

@pubpub-zz
Copy link
Collaborator

@Kaemer1645 ,
Can you try to see what is the result of analysis of the generated pdf to have a better understanding (ex not tried)
https://www.datalogics.com/products/pdf-tools/pdf-checker/

@Kaemer1645
Copy link
Author

@pubpub-zz
Copy link
Collaborator

@Kaemer1645,
may I bother you a little more : can you post the output with version 1.27.5 ? thx

@pubpub-zz
Copy link
Collaborator

@MartinThoma ,
Looking carefully to the generated file, I can see an AcroForm field where Fields(identified as mandatory) is missing. When I manually delete the field (not the object which remains unlinked to the root object after file writing), the pdf file did not asked for saving at closure. can you double check and investigate about any change about this field ?

@Kaemer1645
Copy link
Author

@pubpub-zz of course I can
test_1_27_6.pdf
test_1_27_5.pdf

@MartinThoma
Copy link
Member

@pubpub-zz I didn't have the time to look into that since yesterday, but what popped into my mind is #414 (introduced in 1.27.6)

@mawr-san
Copy link

mawr-san commented Jun 10, 2022

Hi there,
Got the exact same problem after updating an old version of PyPDF2. My script is only merging files.
Here my actual versions :

PS E:\> python --version
Python 3.10.5
PS E:\> python -m platform
Windows-10-10.0.19044-SP0
PS E:\> python -c "import PyPDF2;print(PyPDF2.__version__)"
2.1.0

If this could help, when I try to merge the problematic files, it return this messages at some point :
C:\Program Files\Python310\lib\site-packages\PyPDF2_writer.py:875: UserWarning: Unable to resolve [IndirectObject: IndirectObject(96, 0)], returning NullObject instead warnings.warn(

@pubpub-zz
Copy link
Collaborator

The issue comes from #412 (refer to discussion #355)

when /NeedAppearances is set to true, it asks for saving

My current proposal would be to remove call to set_need_appearances_writer() in __init__() and just call it in update_page_form_field_values()

@MartinThoma , @MasterOdin your opinion ?

@pubpub-zz
Copy link
Collaborator

@Kaemer1645, if you want for the moment, you may add

from PyPDF2.generic import NameObject,BooleanObject
pdf._root_object["/AcroForm"][NameObject("/NeedAppearances")] = BooleanObject(False)

before calling pdf.write(...)

@MartinThoma
Copy link
Member

@mawr-san / @Kaemer1645 Would you please check if the following PDF also has the mentioned issue:

potentially-solved.pdf

If the bug doesn't appear with that PDF, we can merge #970 (going with the fix proposed by @pubpub-zz )

@Kaemer1645
Copy link
Author

@MartinThoma everything works fine!

MartinThoma added a commit that referenced this issue Jun 11, 2022
Issue: When creating files with the current PpdfWriter,
Adobe Acrobat asks 'would you like to save this file'
when attempting to close it - although no changes were made.

Fix: Remove 'self.set_need_appearances_writer()' from writers
     __init__ function

Caused-by: #412 (see #355)

Closes #963

Co-authored-by: pubpub-zz <4083478+pubpub-zz@users.noreply.github.com>
@MartinThoma
Copy link
Member

MartinThoma commented Jun 11, 2022

Nice! Thanks for confirming!

The fix is now in the main branch on GitHub and I'll create a new release on PyPI some time tomorrow :-)

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants