pmaupin / pdfrw

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

How to create a new PDF, add new pages with some text? #236

Open barseghyanartur opened 1 year ago

barseghyanartur commented 1 year ago

An example would be appreciated.

sl2c commented 10 months ago
from pdfrw import PdfWriter, IndirectPdfDict, PdfDict, PdfName, PdfArray
pdf = PdfWriter()
page = IndirectPdfDict(
    Type = PdfName.Page,
    MediaBox = PdfArray([0,0,100,100]),
    Resources = PdfDict(
        Font = PdfDict(
            MyFont = IndirectPdfDict(
                Type = PdfName.Font,
                Subtype = PdfName.Type1,
                BaseFont = PdfName('Helvetica')
            )
        )
    ),
    Contents = IndirectPdfDict(
        stream = '/MyFont 12 Tf (Hello, World!) Tj'
    )
)
pdf.addPage(page)
pdf.write('hello.pdf')

UPD: corrected version