| name | direct-reportlab-pdf-generation |
| description | Generate complex multi-page PDFs by running reportlab Python code directly via run_shell when shell_agent fails on document creation |
Direct ReportLab PDF Generation via run_shell
When to Use This Skill
Use this pattern when:
- You need to create a complex, multi-page PDF document with structured content
- Delegating PDF generation to
shell_agent fails or produces unreliable results
- You need fine-grained control over PDF layout, styling, and pagination
Core Technique
Instead of asking shell_agent to handle PDF creation, write inline Python code using the reportlab library and execute it directly via run_shell. This gives you deterministic control over the document structure.
Step-by-Step Instructions
Step 1: Prepare Your PDF Content
Organize your content into logical sections that will become pages or page groups:
- Title page
- Table of contents (optional)
- Main content sections
- Appendices or references
Step 2: Write the ReportLab Python Script
Create a Python script that uses these key reportlab components:
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak, Image
reportlab.lib.enums TA_CENTER, TA_LEFT, TA_JUSTIFY
():
doc = SimpleDocTemplate(filename, pagesize=letter,
rightMargin=, leftMargin=,
topMargin=, bottomMargin=)
story = []
styles = getSampleStyleSheet()
title_style = ParagraphStyle(
,
parent=styles[],
fontSize=,
textColor=colors.HexColor(),
spaceAfter=,
alignment=TA_CENTER
)
heading_style = ParagraphStyle(
,
parent=styles[],
fontSize=,
textColor=colors.HexColor(),
spaceBefore=,
spaceAfter=
)
body_style = ParagraphStyle(
,
parent=styles[],
fontSize=,
leading=,
alignment=TA_JUSTIFY
)
section content_data:
section[] == :
story.append(Paragraph(section[], title_style))
section[] == :
story.append(Paragraph(section[], heading_style))
section[] == :
story.append(Paragraph(section[], body_style))
story.append(Spacer(, ))
section[] == :
table = Table(section[], colWidths=section.get())
table.setStyle(TableStyle([
(, (, ), (-, ), colors.grey),
(, (, ), (-, ), colors.whitesmoke),
(, (, ), (-, -), ),
(, (, ), (-, ), ),
(, (, ), (-, ), ),
(, (, ), (-, ), ),
(, (, ), (-, -), colors.beige),
(, (, ), (-, -), , colors.black),
]))
story.append(table)
story.append(Spacer(, ))
section[] == :
story.append(PageBreak())
doc.build(story)