Anfaaa
Anfaaa

Reputation: 63

Inserting PDF Page on Existing PDF

I'm trying to insert two pdfs, the first pdf is the main pdf and the second is the one I'd like to take and append to the first pdf, under the same name. The goal is to open the pdf and append and that's it.

import fitz
doc1 = fitz.open("test1.pdf")
doc2 = fitz.open("test2.pdf")
doc1.insertPDF(doc2)
doc2.save("test2.pdf")

I get the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\*****\AppData\Roaming\Python\Python37\site-packages\fitz\fitz.py", line 3537, in save
    raise ValueError("save to original must be incremental")
ValueError: save to original must be incremental

If there's any other way to do this please let me know, through Python

Upvotes: 4

Views: 5509

Answers (1)

Vishal Singh
Vishal Singh

Reputation: 6234

If you just need to append your modifications to the existing pdf file (incremental save) you can use saveIncr() method which internally calls the save method with the correct arguments.

saveIncr() saves the document incrementally by calling

doc.save(doc.name, incremental=True, encryption=PDF_ENCRYPT_KEEP)

Upvotes: 7

Related Questions