|
# /// 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) |