Skip to content

Instantly share code, notes, and snippets.

@villares
Last active January 26, 2024 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save villares/f99229fd8a3c7b8c51d8b480c93f19ad to your computer and use it in GitHub Desktop.
Save villares/f99229fd8a3c7b8c51d8b480c93f19ad to your computer and use it in GitHub Desktop.
Template for generating a multi-page PDF with py5 (and a bonus PDF "overlay" script)
"""
An example/template for generating a mutiple page PDF using py5.
Imported mode style - You'll need to use the thonny-py5mode plug-in or the sketch_runner tool
CC0 / Public Domain dedication - by Alexandre B A Villares
"""
NUM_PAGES = 10
s = 0.71 # scale factor 1 => 356 x 275 trying 252 x 195
rs = 1 # starting random seed
save_pdf = False
def setup():
global w, h
size(1760, 764) # 441mm x 191mm
def draw():
global save_pdf, rs
if save_pdf:
pdf_output.scale(s)
# YOU DRAWING GOES HERE
background(240)
random_seed(rs)
fill(0)
text(f'seed: {rs}', 35, 12)
for x in range(0, width, 50):
line(x, 50, x + random(-50, 50), height - 50)
# END DRAWING HERE
if save_pdf:
print(save_pdf)
save_pdf -= 1
if save_pdf:
rs += 1
pdf_output.next_page()
else:
end_record()
def key_pressed():
global save_pdf, pdf_output
if key == 's':
save_pdf = NUM_PAGES
pdf_output = create_graphics(int(width * s), int(height * s),
PDF, f'output-{NUM_PAGES}.pdf')
begin_record(pdf_output)
"""
An example/template for generating a mutiple page PDF using py5.
Module mode style - https://py5coding.org/content/py5_modes.html
CC0 / Public Domain dedication - by Alexandre B A Villares
"""
import py5
NUM_PAGES = 10
s = 0.71 # scale factor 1 => 356 x 275 trying 252 x 195
rs = 1 # starting random seed
save_pdf = False
def setup():
global w, h
py5.size(1760, 764) # 441mm x 191mm
def draw():
global save_pdf, rs
if save_pdf:
pdf_output.scale(s)
# YOU DRAWING GOES HERE
py5.background(240)
py5.random_seed(rs)
py5.fill(0)
py5.text(f'seed: {rs}', 35, 12)
for x in range(0, py5.width, 50):
py5.line(x, 50, x + py5.random(-50, 50), py5.height - 50)
# END DRAWING HERE
if save_pdf:
print(save_pdf)
save_pdf -= 1
if save_pdf:
rs += 1
pdf_output.next_page()
else:
py5.end_record()
def key_pressed():
global save_pdf, pdf_output
if py5.key == 's':
save_pdf = NUM_PAGES
pdf_output = py5.create_graphics(int(py5.width * s), int(py5.height * s),
py5.PDF, f'output-{NUM_PAGES}.pdf')
py5.begin_record(pdf_output)
py5.run_sketch()
#!/usr/bin/env python
# Requires pdfrw2 - https://pypi.org/project/pdfrw2/
import sys
from pdfrw import PdfReader, PdfWriter, PageMerge
args = sys.argv
if len(args) > 2:
try:
base_file, overlay_file = args[1:3]
if len(args) == 4:
output_file = args[3]
else:
output_file = 'output.pdf'
base_pages = PdfReader(base_file).pages
overlay_pages = PdfReader(overlay_file).pages
except:
print('Needs 2 PDF files as input arguments. Output filename is optional.')
exit()
writer = PdfWriter(output_file)
for i, page in enumerate(base_pages):
overlay = overlay_pages[i % len(overlay_pages)]
new_page = PageMerge() + page + overlay
writer.addpage(new_page.render())
writer.write()
else:
print(
'Usage:\n'
'<base file a> <overlay file b> [<output file>]\n'
'Note: If the overlay file is shorter, its pages will be used repeatedly'
)
#!/usr/bin/env python
# Requires pdfrw2 - https://pypi.org/project/pdfrw2/
import sys
from pdfrw import PdfReader, PdfWriter, PageMerge
# instalar pdfrw2 via pip ou no Thonny Tools > Manage packages
try:
base_file = 'exemplo-10.pdf'
overlay_file = 'overlay.pdf'
output_file = 'output.pdf'
base_pages = PdfReader(base_file).pages
overlay_pages = PdfReader(overlay_file).pages
except Exception as e:
print(e)
exit()
writer = PdfWriter(output_file)
for i, page in enumerate(base_pages):
overlay = overlay_pages[i % len(overlay_pages)]
new_page = PageMerge() + page + overlay
writer.addpage(new_page.render())
writer.write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment