So I have a multi page pdf that looks something like this
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?
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())