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

PdfFileMerger generates blank pages on Python 3 when given file handlers #293

Closed
mluszczyk opened this issue Oct 21, 2016 · 3 comments
Closed

Comments

@mluszczyk
Copy link
Contributor

I'm using the merger like in the snippet below. Whatever input PDF file I provide, I get a PDF with the same number of pages, but all of them are blank (even if in the input file they are not blank).

from PyPDF2 import PdfFileMerger

merger = PdfFileMerger()

INPUT_PATH = "/tmp/a-pdf-file.pdf"
OUTPUT_PATH = "/tmp/output.pdf"

with open(INPUT_PATH, 'rb') as input_file:
    merger.append(input_file)

with open(OUTPUT_PATH, 'wb') as out:
    merger.write(out)

Does not happen on python 2.7, but happens in exactly the same environment on python 3.5. Tested on PyPDF2==1.26.0.

One finding I have is that the if branch on line 116 is never entered in Python 3, but it is entered on Python 2. The reason is that isinstance(open("/tmp/x"), file) is evaluated to True on Python 2, but isinstance(open("/tmp/x"), io.FileIO) is evaluated to False on Python 3 (note from io import FileIO as file at the top of the file).

@keliomer
Copy link

keliomer commented Apr 4, 2018

@mstamy2
Seems the fix (#294 ) hasn't been made available on the version available through PyPI.
Experiencing the same issue as #293 with Python 3.6.3.

I uninstalled and reinstalled through pip, and still ended up having to manually changing the line @mluszczyk did in their PR.

Could we reopen this issue until the fix is live and pip-able?

@cgarbin
Copy link

cgarbin commented Jul 9, 2018

Passing along a solution I got from https://stackoverflow.com/a/49927541/336802: keep the input files open while writing the output file.

This works with the currently released version of PyPDF2.

@kislyuk
Copy link

kislyuk commented Feb 7, 2022

Thank you @cgarbin for mentioning this!

FYI @mstamy2 this is still an issue. Pages turn out blank if the underlying fd was closed, instead of throwing an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants