4

So I have a multi page pdf that looks something like this

multipage

This currently has more than one page but I would like to concatenate. These two pages should be concatenated in a way such that it becomes one page. (Literally joining the two pages while getting rid of the grey area in between them.

gray area that needs to go away I have dug deep int pypdf2 and pdf2image but have not got any luck so far. I was wondering knew of any function that could help me achieve this?

skspawn
  • 53
  • 5
  • Please describe in more detail what the final output should be. Do you want a single JPEG with both pages? A 1-page PDF with 2 shrunken versions of the original pages? Do you want the new page to be in portrait or landscape? – Josh Dec 15 '19 at 22:38
  • A new pdf with with dimensions being the sum of the lengths with constant width – skspawn Dec 15 '19 at 23:03
  • That is good clarification. In general, it is best to put clarification in the original question, so that future readers/answers can read all of the information in one location, rather than having to dig through the comments to figure out the root question. This point may be moot, as it looks like you might already have your answer. – Josh Dec 15 '19 at 23:08
  • sorry about that, im new to the community – skspawn Dec 16 '19 at 16:29
  • No worries. I was given the same advice on my first question. – Josh Dec 17 '19 at 16:25

1 Answers1

3

You can create a new page object thats twice as long as the first(assuming both pages are of equal height) and put the pages one after other in the new page.

from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.pdf import PageObject


reader = PdfFileReader(open("file.pdf",'rb'))

page_1 = reader.getPage(0)
page_2 = reader.getPage(1)

#Creating a new file double the size of the original
translated_page = PageObject.createBlankPage(None, page_1.mediaBox.getWidth(), page_1.mediaBox.getHeight()*2)

#Adding the pages to the new empty page
translated_page.mergeScaledTranslatedPage(page_1, 1, 0, page_1.mediaBox.getHeight())
translated_page.mergePage(page_2)

writer = PdfFileWriter()
writer.addPage(translated_page)

with open('out.pdf', 'wb') as f:
    writer.write(f)

If they are of different heights, just do

translated_page = PageObject.createBlankPage(None, page_1.mediaBox.getWidth(), page_1.mediaBox.getHeight()+ page_2.mediaBox.getHeight())
3141bishwa
  • 133
  • 1
  • 10