pdfrw icon indicating copy to clipboard operation
pdfrw copied to clipboard

How to add a blank page to a PDF

Open tisimst opened this issue 6 years ago • 3 comments

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?

tisimst avatar Aug 30 '19 19:08 tisimst

Same issue.

clp1 avatar Jun 18 '20 11:06 clp1

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)

gvellut avatar Dec 09 '21 20:12 gvellut

@gvellut you forgot to import PdfName.

majuss avatar Jul 07 '22 18:07 majuss