Referencing some previous material on PyPDF2 functionality, I was able to add one page to another pdf using the below code. However, when doing so, the Table of Contents now shows images of the pages instead of the properly structured table view originally available in the "existing_pdf"...any suggestions on how to resolve would be greatly appreciated. I've also tried carrying over the metada, in hopes of the Table of Contents displaying properly (which it didn't), and will include that portion as well.
Code to generate add page to PDF:
# Import PyPDF2
# Open the existing PDF file
with open('existing_file.pdf','rb') as file:
existing_pdf = PyPDF2.PdfFileReader(file)
#Create a new PDF file
new_pdf = PyPDF2.PdfFileWriter()
#Copy over the existing PDF's metadata - which doesn't fix TOC issue
new_pdf._info.getObject().update(existing_pdf.getDocumentInfo().extract())
#Add all the existing pages to the new PDF
for page_num in range(existing_pdf.numPages):
page = existing_pdf.getPage(page_num)
new_pdf.addPage(page)
#Add the new page
new_page = PyPDF2.pdf.PageObject.createBlankPage(None,612,792)
new_pdf.addPage(new_page)
#Save modified PDF
with open('modified_file.pdf','wb') as output_file:
new_pdf.write(output_file)