How to merge two landscape pdf pages using pyPdf
Asked Answered
T

5

6

I'm having trouble merging two PDF files with pyPdf. When I run the following code the the watermark (page1) looks fine, but the page2 has been rotated 90 degrees clockwise.

Any ideas what's going on?

Example of what's going wrong

from pyPdf import PdfFileWriter, PdfFileReader

# PDF1: A4 Landscape page created in photoshop using PdfCreator, 
input1 = PdfFileReader(file("base.pdf", "rb"))
page1 = input1.getPage(0)

# PDF2: A4 Landscape page, text only, created using Pisa (www.xhtml2pdf.com)
input2 = PdfFileReader(file("text.pdf", "rb"))
page2 = input2.getPage(0)

# Merge
page1.mergePage(page2)

# Output
output = PdfFileWriter()
output.addPage(page1)
outputStream = file("output.pdf", "wb")
output.write(outputStream)
outputStream.close()
Turn answered 18/5, 2011 at 7:26 Comment(3)
Are you sure they are both landscape? It looks like the left one is portrait.Octavo
Yes they are - I just created that image as an example as my actual pdf's contact personally identifiable information.Turn
I have problem with pisaContext instance has no attribute 'seek'Vange
T
2

I found a solution. My code was fine - I just had to change how I generated the original PDF files.

Instead of creating the PDF using PdfCreator & Photoshop, I copy and pasted my photoshop image into MS Word 2007, and then used it's export feature to create the PDF file for page1. It now works great!

So, PdfCreator must producing PDF files that are not compatible with pyPdf.

Turn answered 19/5, 2011 at 1:19 Comment(0)
T
5

You can transform the page while you're merging it into another page. I defined this function to rotate the page around a point while being merged:

def mergeRotateAroundPointPage(page, page2, rotation, tx, ty):
    translation = [[1, 0, 0],
                   [0, 1, 0],
                   [-tx,-ty,1]]
    rotation = math.radians(rotation)
    rotating = [[math.cos(rotation), math.sin(rotation),0],
                [-math.sin(rotation),math.cos(rotation), 0],
                [0,                  0,                  1]]
    rtranslation = [[1, 0, 0],
                   [0, 1, 0],
                   [tx,ty,1]]
    ctm = utils.matrixMultiply(translation, rotating)
    ctm = utils.matrixMultiply(ctm, rtranslation)

    return page.mergeTransformedPage(page2, [ctm[0][0], ctm[0][1],
                                             ctm[1][0], ctm[1][1],
                                             ctm[2][0], ctm[2][1]])

Then you call it like this:

mergeRotateAroundPointPage(page1, page2, 
                page1.get('/Rotate') or 0, 
                page2.mediaBox.getWidth()/2, page2.mediaBox.getWidth()/2)
Tabling answered 30/6, 2013 at 17:10 Comment(2)
UPDATE: I'm happy to say that this code has been merged into the mainline pyPDF2 repository, so no more copy-pasting, just call it!Tabling
UPDATE 2: It's name in PyPDF2 is now mergeRotatedTranslatedPage. We found the documentation for this in PyPDF unclear but understanding it as "rotate around point" makes sense.Platypus
T
2

I found a solution. My code was fine - I just had to change how I generated the original PDF files.

Instead of creating the PDF using PdfCreator & Photoshop, I copy and pasted my photoshop image into MS Word 2007, and then used it's export feature to create the PDF file for page1. It now works great!

So, PdfCreator must producing PDF files that are not compatible with pyPdf.

Turn answered 19/5, 2011 at 1:19 Comment(0)
D
0

You can make use of the rotateClockwise or rotataeCounterClockwise function in the page object.

page2 = input2.getPage(0).rotateCounterClockwise(90)
Darwin answered 18/5, 2011 at 7:35 Comment(1)
Yes, I had already tried doing this. However, it doesn't work! The two pages are still 90 degrees different. I'm starting to think that there is either a bug in pyPdf or there is something funky happening in my page1 pdf file.Turn
C
0

Since you're using pyPdf, this should do the trick for rotating pages:

output.addPage(input1.getPage(1).rotateClockwise(90))
Cropdusting answered 24/11, 2011 at 20:44 Comment(0)
H
0

I would like to add that I used Photoshop to save the PDF but as version 1.4 compatible. This made a huge PDF file but it worked.

So it is pyPDF not reading it right.

Haggi answered 30/3, 2013 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.