pmaupin / pdfrw

pdfrw is a pure Python library that reads and writes PDFs
Other
1.86k stars 271 forks source link

How to add a blank page to a PDF #175

Open tisimst opened 5 years ago

tisimst commented 5 years ago

I'm needing to take a file and add a blank page from scratch that's the same size as the rest of the pages in the file. What's the right way to go about doing this?

clp1 commented 4 years ago

Same issue.

gvellut commented 2 years ago

Just in case someone still has the issue (arrived here myself looking for the answer):

from pdfrw import PdfDict, PdfReader, PdfWriter

def blankPage(template):
    x = PdfDict()
    x.Type = PdfName.Page
    x.Contents = PdfDict(stream="")
    x.MediaBox = template.inheritable.MediaBox
    return x

writer = PdfWriter("book.pdf")
pages = PdfReader(base_pdf).pages
writer.addPage(blankPage(pages[0]))
writer.write()

The arg template is an existing page from which you want to copy the dimension ; Alternatively if you already know the dimension in points, MediaBox has this shape: [0, 0, 612.00000, 612.00000] (with 612 pt = 8.5 in)

majuss commented 2 years ago

@gvellut you forgot to import PdfName.