Skip to content

Instantly share code, notes, and snippets.

@godber
Last active December 24, 2025 23:36
Show Gist options
  • Select an option

  • Save godber/71c14a6298e003a96030d04a636c6153 to your computer and use it in GitHub Desktop.

Select an option

Save godber/71c14a6298e003a96030d04a636c6153 to your computer and use it in GitHub Desktop.
Generate a PDF with the specified number of pages.

Download the python file and run it like

uv run lorem-pdf.py 6

Or if you're in to running random strangers code fresh off the internet do this:

uv run https://gist.githubusercontent.com/godber/71c14a6298e003a96030d04a636c6153/raw/c3a5a2c69e1cff3946a415358efaf12ef37954fe/lorem-pdf.py 6

YOLO!

usage: lorem-pdfsNCmy4.py [-h] [--output OUTPUT] pages

Generate a Lorem Ipsum PDF with N pages.

positional arguments:
  pages                The number of pages to generate

options:
  -h, --help           show this help message and exit
  --output, -o OUTPUT  The output filename (default: lorem_ipsum.pdf)
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "reportlab",
# ]
# ///
import argparse
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak, Spacer
from reportlab.lib.styles import getSampleStyleSheet
# Standard Lorem Ipsum text to repeat
LOREM_IPSUM = (
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor "
"incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud "
"exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure "
"dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
"mollit anim id est laborum. "
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque "
"laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi "
"architecto beatae vitae dicta sunt explicabo."
)
def generate_pdf(filename, num_pages):
"""
Generates a PDF with the specified number of pages containing Lorem Ipsum.
"""
# 1. Create the PDF document template
doc = SimpleDocTemplate(
filename,
pagesize=letter,
rightMargin=72,
leftMargin=72,
topMargin=72,
bottomMargin=72
)
# 2. Get standard styles
styles = getSampleStyleSheet()
story = []
# 3. Build the content
for i in range(num_pages):
page_num = i + 1
# Add a Page Header
header_text = f"Page {page_num}"
story.append(Paragraph(header_text, styles['Heading1']))
story.append(Spacer(1, 12))
# Add some paragraphs to fill the page
paragraphs_per_page = 4
for _ in range(paragraphs_per_page):
p = Paragraph(LOREM_IPSUM, styles['Normal'])
story.append(p)
story.append(Spacer(1, 12))
# Add a Page Break (ensure we stop after the last page)
if page_num < num_pages:
story.append(PageBreak())
# 4. Build the PDF
print(f"Generating '{filename}' with {num_pages} pages...")
doc.build(story)
print("Done.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate a Lorem Ipsum PDF with N pages.")
parser.add_argument(
"pages",
type=int,
help="The number of pages to generate"
)
parser.add_argument(
"--output", "-o",
type=str,
default="lorem_ipsum.pdf",
help="The output filename (default: lorem_ipsum.pdf)"
)
args = parser.parse_args()
generate_pdf(args.output, args.pages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment