Generating Pdfs

Creates PDF documents from scratch (reports, invoices, one-pagers) or from HTML content. Use whenever the deliverable needs to be a PDF, or when a polished printable document is expected instead of markdown.

aaravriyer193 Updated

File contents

Generating PDFs

Never hand-write .pdf with write_file — build it with a real library.

From scratch (structured document) — use reportlab:

pip install reportlab
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet

styles = getSampleStyleSheet()
doc = SimpleDocTemplate("/home/user/report.pdf", pagesize=letter)
story = [Paragraph("Title", styles["Title"]), Spacer(1, 12), Paragraph("Body text.", styles["Normal"])]
doc.build(story)

From HTML/CSS (richer layout) — use weasyprint:

pip install weasyprint
from weasyprint import HTML
HTML(string="<h1>Title</h1><p>Body.</p>").write_pdf("/home/user/report.pdf")

weasyprint gives real CSS layout (columns, fonts, page breaks via page-break-after), which is easier for anything visually complex than assembling reportlab flowables by hand — prefer it for anything more than a simple text report.

Present the .pdf with present_file, never the generating script.

aaravriyer193/skills/tree/main/generating-pdfs commit d6f148f1a6

Frequently asked questions

npx skillmds@latest add aaravriyer193/generating-pdfs