-
-
Notifications
You must be signed in to change notification settings - Fork 161
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
Pages are overwritten after merging PDFs with more than 50 pages #181
Comments
The logic you use results in a very dirty PDF which seems to trigger some limitations in PDF viewers. In e.g. Chrome or Foxit you will notice the issue at 42 pages, in pdf.js is no issue at all. So to fix this just use a single run to import all pages instead of adding each after each:
You should understand that you cannot edit a PDF with FPDI but you import the pages of an existing one and place them onto new pages. If you do this recursively the internal structure will also grow appropriately and it looks like some PDF viewers have some limit in view to such structures. This may be an issue of a nesting level of graphic states - which was documented in PDF 1.7 but was removed in 2.0. Maybe it's simply a security check as such recursion looks sus. Generally try to avoid importing of PDFs which were imported earlier but start with the original documents. |
Hi @JanSlabon, Thank you very much for this insightful reply. The code provided was just a test snippet, in the real application we can of course create the single PDFs before merging them. But I would still need to combine 100's of PDFs into a single document at some point. If I use your example code I quickly run into a "Failed to open stream: Too many open files" exception. Is there any way to close the files after adding them? I will also take a look at the SetaPDF-Merger component, unfortunately with this project I'm pretty tight on paid solutions... Thank you again! |
Long time not seen this error. Especially at only 100 files. You may increase this on OS level or split this at the max number (if you do this several times, you end in the same situation as you initially reported). But again the SetaPDF-Merger component got you covered: https://manuals.setasign.com/setapdf-merger-manual/performance-optimizations/#index-2 |
Haha thanks, yeah the error did not happen around 100 files – more like 800 PDFs in. If there is no way to do this with an open source solution, I'll try to get the funds for the commercial licence. Thank you! |
You can try to hold the files in memory: https://manuals.setasign.com/fpdi-manual/v2/the-fpdi-class/#index-4 Also for this the SetaPDF-Merger got you covered as you can work with an intermediate result to free memory: https://manuals.setasign.com/setapdf-merger-manual/performance-optimizations/#index-4 |
Thank you again for your help. I was actually able to solve this with a simple Closing this ... |
After merging exactly 50 pages with
AddPage
anduseImportedPage
the leading pages are overwritten/blank. I originally though this was an issue with another package, libmergepdf, which depends on this project, but after removing the package and directly working with yours I encountered the same bug.Below a little test script to showcase the problem. The following loop generates two PDF files per iteration:
In the end I have 100 x
page-{$i}.pdf
and 100 xmerged-{$i}.pdf
files. And untilmerged-50.pdf
everything works, the PDF has 50 pages from 1-50. But inmerged-51.pdf
the first two pages are blank/white and the first content is visible on page 3 (correctly printing out "Page 3"). Every iteration removes the content from another page, so that the final filemerged-100.pdf
has 50 empty pages and the first page with the content is page 51.Any idea by what this is caused? The file size of merged PDF doesn't seem to change it, neither is the content, orientation or paper size... I'm totally lost here.
Thank you for your help!
The text was updated successfully, but these errors were encountered: