shaynehansen
shaynehansen

Reputation: 1

Pdf Imposition using Python

I am trying to have the first page and second page of the pdf imposed on to page 1. The first page will be above the second page, imposed on the first page.

The issue is the pages are not triming, or merging. The last page imposes on the second to last and that is it.

from PyPDF2 import PdfFileReader, PdfFileWriter


output = PdfFileWriter()

file_name = '81plots.pdf'
file = PdfFileReader(open(file_name, 'rb'))


i = 1
for i in range(file.getNumPages()):
    page = file.getPage(i-1)
    page.trimBox.LowerLeft = (0, 395.28422)
    page.trimBox.LowerRight = (1459.75542, 395.28422)
    page.trimBox.UpperLeft = (0, 790.56844)
    page.trimBox.UpperRight = (1459.75542, 790.56844)
    page_step = file.getPage(i)
    page_step.trimBox.LowerLeft = (0,0)
    page_step.trimBox.LowerRight = (1459.75542, 0)
    page_step.trimBox.UpperLeft = (0, 395.28422)
    page_step.trimBox.UpperRight = (1459.75542, 395.28422)
    page.mergePage(page_step)
    output.addPage(page)


outfile = 'testfile.pdf'

with open(outfile, 'wb') as file:
    output.write(file)

Upvotes: 0

Views: 602

Answers (1)

Patrick Gallot
Patrick Gallot

Reputation: 625

The trim box is not really applicable to what you are trying to do.

I suggest that you start from a blank page and use PageObject class's mergeScaledTranslatedPage method to place the content of both pages on to the new page.

Upvotes: 0

Related Questions