Creating multi-page PDFs in Python mode

Hello there intrepid Processing community!

I have a question about creating multi-page PDFs in Processing:

I’ve tried to modify the code from the Java implementation with limited success. I’ve created an empty PDF file with the code below, and a mostly blank PDF by using size(400,400,PDF,‘output.pdf’)

add_library('pdf')

pdf = createGraphics(100, 100, PDF, 'simple.pdf')
    

pdf.beginDraw()
pdf.background(100)
for index in range(1,10):
    pdf.beginDraw()
    pdf.stroke(255)
    pdf.line(5,5, index * .5, height-index)
    pdf.endDraw()
    pdf.nextPage()

I am able to create single page PDF files by declaring the PDF renderer in size(), but I want to create multi-page PDFs. I guess I’m not sure how to reference the pdf to call nextPage() if I don’t declare it explicitly like in this example.

Thanks for any insight you marvelous people might have!

This worked for me:

add_library('pdf')

pdf = beginRecord(PDF, 'simple.pdf')
    
for index in range(1,10):
    background(100)
    stroke(255)
    line(5,5, index * .5, height-index)
    if index < 9:
        pdf.nextPage()
    
endRecord()

I added that if statement to avoid a blank page. Maybe there’s a better solution.

3 Likes

NICE! I missed the importance of including the begin and endRecord functions. Works like a charm. Thanks hamoid, definitely going to check out some of your tutorial videos as well!

An additional question: If I wanted to poke around in the source for the PDF library to see what other methods were available, where would I find that?

Thanks again!

1 Like

GoToLoop dropping the knowledge again!

Thank you.

1 Like