1

With some input files my merging 2 pages into 1 page side-by-side fails. I can't find out why! Sample test code:

from PyPDF2 import PdfFileWriter, PdfFileReader
import sys
print ("2-up input " + sys.argv[1])
input1 = PdfFileReader(open(sys.argv[1], "rb"), strict=False)
output = PdfFileWriter()

lhs = input1.getPage(0)
rhs = input1.getPage(1)
lhs.mergeTranslatedPage(rhs, 420,0, True)
output.addPage(lhs)
outputStream = open(sys.argv[2], "wb")
output.write(outputStream)
outputStream.close()

The failed cases generate an output file which has ONLY the lhs page, whereas a good output has lhs and rhs pages next to each other.

A possible clue: when I run the script on a "bad" input file I get this message: "PdfReadWarning: Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1503]"

keble
  • 93
  • 1
  • 11
  • Please provide the files in question. If the issue only occurs for *some input files*, there seems to be something special about them. – mkl Dec 28 '14 at 15:59
  • If you're not wedded to PyPDF2, another Python library that can merge PDFs is [pdfrw](https://github.com/pmaupin/pdfrw/). It has 4-up and 2up examples. – Patrick Maupin Aug 10 '15 at 03:02

2 Answers2

1

I'm pretty sure this is a problem with pyPDF2. I tried an alternative non-Python solution, based on Gluing (Imposition) ... (see the 4th answer by Wang).

My code:

#!/bin/bash
# converts pdf file to 2-up pdf file
# command line argument is pdf file name
# output is output.pdf
pdftops $1 - | psnup -Pa5 -m0.6in -2 -q | pstopdf -i -o output.pdf

This does work on the troublesome files that PyPDF2 choked on. Note that the formatting arguments to pnup will depend on your documents.

I'm still learning how they work!

Community
  • 1
  • 1
keble
  • 93
  • 1
  • 11
  • If your *bad files* actually are broken, it's not a *problem with pyPDF2* but aproblem with the files. – mkl Dec 28 '14 at 16:01
  • I confirm the same. 'mergeTranslatedPage' worked on some pdf files and did not work on others. It's even worked on some pages of a pdf and not on others. Those pages that worked on have no fonts (only an image that has been added as a new page to that pdf file with acrobat pdf pro). – Haider Mar 08 '22 at 16:08
0

In my opinion, I did not check, but, here:

output.addPage(lhs)

You ask python to add to the new file only 1 page, and this page is lhs. Try to write as well:

output.addPage(rhs)
Ievgenii
  • 477
  • 1
  • 5
  • 13
  • Sorry, I did not get, you want album oriented pdf? By that I mean 2 pages on 1 paper. Or you want, like me, 2 pages separately. Because if you want album oriented pdf file, than the code, you showed me on github deals good. – Ievgenii Dec 27 '14 at 14:38
  • @levgenii yes I want 2 pages side by side. My code above is based on that github example.The problem I have is that the code works with SOME pdf files but it fails with other files – keble Dec 27 '14 at 21:40
  • I see, do not know why either – Ievgenii Dec 27 '14 at 22:39