Structured documents via ReportLab: reports, proposals, contracts, white papers, financial analysis, data tables. Also covers ATS-friendly resumes.
🚨 EMOJI CHECK - STOP HERE IF CONTENT HAS EMOJI
ReportLab renders emoji (📊🎯🔥💡 etc.) as □ tofu squares. This is unfixable.
If the user's content contains intentional emoji → STOP. Do NOT use this brief. Route to
briefs/creative.mdinstead (Playwright renders emoji natively).This applies even if the document is a "report" - emoji + ReportLab = broken output.
1. BRIEF → Confirm document type, audience, page count, outline
2. DESIGN → Run `pdf.py palette.generate --title "..."` → copy-paste output into script
3. EDIT → Content transformation (extract data, define typographic roles)
4. COVER → Read typesetting/cover.md → render ReportLab cover via scripts/cover_render.py (default ON for reports ≥ 3 pages; skip only for resumes/letters/memos/forms)
5. BUILD → Write ReportLab code (fonts → styles → content → tables → charts)
6. PREFLIGHT → code.sanitize → execute → meta.brand → font.check → toc.check → pages.clean → pdf_qa.py
7. DELIVER → Merge cover + body → final PDF
Typesetting assets (load when you reach that step):
typesetting/palette.md - color system, typography rules, anti-patternstypesetting/cover.md - 7 cover layouts with variants, typography scale, bounding box rulestypesetting/charts.md - chart styling, anti-stacking rules, axis/grid/legend treatmentCover is DEFAULT ON for reports. Skip cover only for: resumes, letters, memos, forms, checklists, or documents ≤ 2 pages.
Before writing any ReportLab code, you MUST generate the color palette via the palette.generate command. This is not optional. Do NOT hardcode hex colors. Do NOT pick colors by feel.
Run this command FIRST. Copy-paste its output directly into your Python script:
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" palette.generate --title "<document title>" --mode minimal
The command auto-derives the design intent from the document title, computes a mathematically harmonious palette, and outputs ready-to-paste ReportLab Python code:
# ━━ Color Palette (auto-generated by pdf.py palette.generate) ━━
# Intent: neutral | Mode: minimal | Harmony: split_complementary
# Contrast: text:bg=14.12 | accent:bg=3.06
from reportlab.lib import colors
ACCENT = colors.HexColor('#2f97b9')
TEXT_PRIMARY = colors.HexColor('#252422')
TEXT_MUTED = colors.HexColor('#8d8981')
BG_SURFACE = colors.HexColor('#dfdad2')
BG_PAGE = colors.HexColor('#f5f4f3')
SURFACE_RGBA = 'rgba(0,0,0,0.03)'
# ReportLab table colors
TABLE_HEADER_COLOR = ACCENT
TABLE_HEADER_TEXT = colors.white
TABLE_ROW_EVEN = colors.white
TABLE_ROW_ODD = BG_SURFACE
Options:
--mode minimal (default, recommended for 50%+ of documents) | dark | pastel | jewel | light--harmony auto (default, recommended) | complementary | split_complementary | analogous | triadic | monochrome--format python (default) | json | css--seed <int> - for reproducible palettes across regenerations⚠️ FORBIDDEN:
colors.HexColor('#xxxxxx') with any hex value you chose yourselfcolors.red, colors.blue, or any ReportLab named color for design elements✅ The ONLY acceptable way to get colors: Run palette.generate, copy the output.
| Element | Color Source | Notes |
|---|---|---|
| Table headers | ACCENT (bg) + white (text) | High contrast required |
| Table odd rows | BG_SURFACE at 50% opacity | Subtle striping |
| Section titles | ACCENT or TEXT_PRIMARY | Match heading hierarchy |
| Body text | TEXT_PRIMARY | Never use pure #000000 |
| Muted/meta text | TEXT_MUTED | Dates, captions, footnotes |
| Horizontal rules | ACCENT at 30% opacity | Thin, unobtrusive |
| Chart colors | Derive from ACCENT (see charts.md) | Consistent with palette |
#1F4E79, #555555, #888888 in codecolors.red, colors.blue or any ReportLab named color for design elementsException: Semantic colors for data visualization (green for positive, red for negative) are acceptable but should be muted variants derived from the palette when possible.
Before writing any code, restructure raw text into visual roles. This is the most impactful quality step - skipping it produces "Word document with a PDF extension".
Parse raw text and categorize content into visual roles:
| Role | ReportLab Mapping | Description |
|---|---|---|
| Hero / Display | Cover title (36-42pt) or section opener | 1-5 words, the emotional hook |
| Kicker / Eyebrow | Subtitle or section intro (10-12pt, muted) | Tiny context line above main heading |
| Data Sculpture | CalloutBox or bold stat block | Extract impactful numbers (97%, $4.2M, -45ms) |
| Pull Quote | BlockQuote (italic + left indent 24pt + accent border) | Most provocative sentence, standalone |
| Body | Standard paragraph | Everything else |
Rule: Before writing any body paragraph, scan for numbers with units. Every metric like "revenue grew by 12%" or "latency dropped to 45ms" MUST be extracted into a CalloutBox or metrics table - never buried in paragraph prose. See §Data-to-Ink Ratio Rules below.
Three rules for safe character handling in ReportLab PDFs:
a) Superscripts and subscripts: Use <super>/<sub> tags, never raw Unicode superscript/subscript characters (e.g., \u00b2, \u2082).
b) Emoji: ReportLab cannot render emoji. If content contains emoji, use Creative brief (HTML + Playwright).
c) Font fallback for mixed CJK/Latin text: After registering fonts, call install_font_fallback() once. This automatically wraps missing-glyph characters in <font> tags inside every Paragraph(). No manual <font name="..."> wrapping needed for mixed Chinese-English text.
Mathematical/relational operators (×, ÷, ±, ≤, √, ∑, ≅, ∫, π, ∠, Δ, etc.) are safe to use as literal characters in Paragraph() - both SimHei and Times New Roman have these glyphs.
| Need | Correct Method | Correct Example |
|---|---|---|
| Superscript | <super> tag in Paragraph() |
Paragraph('10<super>2</super> × 10<super>3</super> = 10<super>5</super>', style) |
| Subscript | <sub> tag in Paragraph() |
Paragraph('H<sub>2</sub>O', style) |
| Bold | <b> tag in Paragraph() |
Paragraph('<b>Title</b>', style) |
| Math operators | Literal char in Paragraph() |
Paragraph('AB ⊥ AC, ∠A = 90°, and ΔABC ≅ ΔDCF', style) |
| Scientific notation | Combined tags in Paragraph() |
Paragraph('1.2 × 10<super>8</super> kg/m<super>3</super>', style) |
from reportlab.platypus import Paragraph
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_JUSTIFY
body_style = ParagraphStyle(
name="ENBodyStyle",
fontName="Times New Roman",
fontSize=10.5,
leading=18,
alignment=TA_JUSTIFY,
)
# Superscript: area unit
Paragraph('Total area: 500 m<super>2</super>', body_style)
# Subscript: chemical formula
Paragraph('The reaction produces CO<sub>2</sub> and H<sub>2</sub>O', body_style)
# Scientific notation
Paragraph('Speed of light: 3.0 × 10<super>8</super> m/s', body_style)
# Combined
Paragraph('E<sub>k</sub> = mv<super>2</super>/2', body_style)
# Bold heading
Paragraph('<b>Chapter 1: Introduction</b>', header_style)
# Math symbols
Paragraph('When ∠ A = 90°, AB ⊥ AC and ΔABC ≅ ΔDEF', body_style)
Pre-generation check - before writing ANY string, ask:
"Does this string contain a character outside basic CJK or Mathematical/relational operators?" If YES → it MUST be inside a
Paragraph()with the appropriate tag. If it is a superscript/subscript digit in raw unicode escape form → REPLACE with<super>/<sub>tag.
NEVER rely on post-generation scanning. Prevent at the point of writing.
Encoding safety - before writing ANY content text:
"Does this string contain Japanese kana (の, が, は etc.) or rare Unicode symbols?" If YES → REPLACE with safe plain Chinese equivalents. Japanese kana (Hiragana U+3040-U+309F, Katakana U+30A0-U+30FF) frequently corrupt to U+FFFD (�) when code passes through LLM output, heredoc, or terminal encoding layers. Common safe replacements:
活の真鲷→活缔真鲷,盐烤の鲭鱼→盐烤鲭鱼,烤の鸡串→炭烤鸡串. If the character is genuinely needed, verify it survives a full write→read round-trip withopen(file, encoding='utf-8').
You MUST ONLY use the following registered fonts. Using ANY other font is STRICTLY FORBIDDEN.
| Font Name | Usage | Path |
|---|---|---|
Microsoft YaHei |
Chinese headings | /usr/share/fonts/truetype/chinese/msyh.ttf |
SimHei |
Chinese body text | /usr/share/fonts/truetype/chinese/SimHei.ttf |
SarasaMonoSC |
Chinese code blocks | /usr/share/fonts/truetype/chinese/SarasaMonoSC-Regular.ttf |
Times New Roman |
English text, numbers, tables | /usr/share/fonts/truetype/english/Times-New-Roman.ttf |
Calibri |
English alternative | /usr/share/fonts/truetype/english/calibri-regular.ttf |
DejaVuSans |
Formulas, symbols, code | /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf |
FORBIDDEN fonts (DO NOT USE):
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFontFamily
# Chinese fonts
pdfmetrics.registerFont(TTFont('Microsoft YaHei', '/usr/share/fonts/truetype/chinese/msyh.ttf'))
pdfmetrics.registerFont(TTFont('SimHei', '/usr/share/fonts/truetype/chinese/SimHei.ttf'))
pdfmetrics.registerFont(TTFont("SarasaMonoSC", '/usr/share/fonts/truetype/chinese/SarasaMonoSC-Regular.ttf'))
# English fonts
pdfmetrics.registerFont(TTFont('Times New Roman', '/usr/share/fonts/truetype/english/Times-New-Roman.ttf'))
pdfmetrics.registerFont(TTFont('Calibri', '/usr/share/fonts/truetype/english/calibri-regular.ttf'))
# Symbol/Formula font
pdfmetrics.registerFont(TTFont("DejaVuSans", '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf'))
# CRITICAL: Register font families to enable <b>, <super>, <sub> tags
registerFontFamily('Microsoft YaHei', normal='Microsoft YaHei', bold='Microsoft YaHei')
registerFontFamily('SimHei', normal='SimHei', bold='SimHei')
registerFontFamily('Times New Roman', normal='Times New Roman', bold='Times New Roman')
registerFontFamily('Calibri', normal='Calibri', bold='Calibri')
registerFontFamily('DejaVuSans', normal='DejaVuSans', bold='DejaVuSans')
For Chinese PDFs:
SimHei or Microsoft YaHeiMicrosoft YaHei (MUST use for Chinese headings)SarasaMonoSCDejaVuSansSimHeiFor English PDFs:
Times New RomanTimes New Roman (MUST use for English headings)DejaVuSansTimes New RomanFor Mixed Chinese-English PDFs:
install_font_fallback() once after registering fonts - it automatically wraps characters in <font> tags so you don't need to do it manually.If you still want manual control, you can use <font name='...'> tags, but the automatic fallback handles most cases.
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'scripts'))
from pdf import install_font_fallback
# 1. Register fonts (as above)
# 2. Install fallback - one line, handles all mixed-language text
install_font_fallback()
# 3. Just write naturally - no manual <font> wrapping needed!
en_style = ParagraphStyle(name="EN", fontName="Times New Roman", fontSize=10.5, leading=18)
cn_style = ParagraphStyle(name="CN", fontName="SimHei", fontSize=10.5, leading=18)
Paragraph('MySQL、PostgreSQL、Redis', en_style) # ✅ CJK comma auto-fallback to SimHei
Paragraph('Beijing (北京) has 21M people', en_style) # ✅ CJK chars auto-fallback
Paragraph('价格:¥2,200~5,800/月', en_style) # ✅ all CJK chars handled
Paragraph('《巴黎协定》签署于2015年', en_style) # ✅ CJK book title marks handled
How it works: install_font_fallback() monkey-patches Paragraph.__init__ to scan each character against the font's charToGlyph table. Characters missing from the base font are automatically wrapped in <font name="FallbackFont">. The fallback chain is: English fonts → SimHei, Chinese fonts → Times New Roman. For aesthetic optimization, Cyrillic text in SimHei is automatically routed to Times New Roman (serif looks better for Cyrillic).
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
Run fc-list to get more fonts. Font files are typically located under:
/usr/share/fonts/truetype/chinese//usr/share/fonts/truetype/english//usr/share/fonts/Follow the document type strategy defined in SOUL.md Rule 1.
Structural breaks (always - MANDATORY):
Content breaks (by document type):
PageBreak() before section headings. This is an iron rule.PageBreak() before each H1 is acceptable (only if user explicitly requests academic formatting)Anti-tear (mandatory — but with height limit):
from reportlab.platypus import KeepTogether
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
# Maximum height for KeepTogether blocks: 40% of page height
# Blocks taller than this should NOT be kept together — they cause
# the preceding page to be mostly empty when the block gets pushed down.
MAX_KEEP_HEIGHT = A4[1] * 0.4 # ~336pt ≈ 12cm
def safe_keep_together(elements):
"""Wrap elements in KeepTogether only if their total height is reasonable.
If too tall, keep only the FIRST TWO elements together (e.g. title + table/image header)
to prevent title-content separation, while letting the rest paginate naturally.
"""
total_h = 0
for el in elements:
w, h = el.wrap(A4[0] - 2*inch, A4[1])
total_h += h
if total_h <= MAX_KEEP_HEIGHT:
return [KeepTogether(elements)]
elif len(elements) >= 2:
# Keep at least title + first element together to prevent title orphaning
return [KeepTogether(elements[:2])] + list(elements[2:])
else:
return list(elements)
# Heading stays with first paragraph
story.extend(safe_keep_together([
heading_paragraph,
first_body_paragraph,
]))
# Image + caption: keep together ONLY if image is small
story.extend(safe_keep_together([image, caption_paragraph]))
# Table title + table: keep together ONLY if table is short
if len(table_data) <= 15:
story.extend(safe_keep_together([table_title, table]))
else:
# Long table: just keep title with the table start
story.extend(safe_keep_together([table_title]))
story.append(table)
⚠️ KEY INSIGHT: KeepTogether is the #1 cause of “empty page with chart on next page”.
When a KeepTogether block is taller than the remaining space on the current page,
ReportLab pushes the entire block to the next page, leaving the current page mostly empty.
Always use safe_keep_together() to cap the maximum block height.
⚠️ Note: Do NOT use KeepWithNext - it is unreliable in ReportLab 4.x.
Spacer(1, 18) after preceding textSpacer(1, 6) before table captionSpacer(1, 18) before next contentSpacer(1, 12) (~1 line)Spacer(1, 12)Spacer(1, 18) (~1.5 lines)Spacer(1, 24) (~2 lines)Spacer(1, X) where X > 24, except for H1 major breaks or cover page elements⚠️ Report route covers are generated with ReportLab via scripts/cover_render.py, using the template system defined in typesetting/cover.md (templates 01–05 for the Report route). Pure Python — no HTML/Playwright/Chromium required.
Pipeline (Report route):
cover_render.py (pick a template 01–05 per typesetting/cover.md Part 2; pass content + palette).Why ReportLab covers now? The cover engine reproduces the previous HTML designs in pure ReportLab, so the Report route no longer depends on Playwright/Chromium. Fonts are auto-detected from the host machine and chosen by cover style (see
cover.mdPart 3). One cover engine, one visual standard.
⚠️ Cover page is DEFAULT ON for all Report-route documents ≥ 3 pages.
The Report route handles formal, structured documents. Readers expect a professional cover. Always generate a cover unless the document type is explicitly excluded below.
ALWAYS add a cover page (default behavior):
NEVER add a cover page (explicit exclusions only):
Rule of thumb: If it's a report, analysis, proposal, or any formal document ≥ 3 pages → add a cover. When in doubt, add the cover. It's easier to remove a cover the user didn't want than to miss one they expected.
Build the ReportLab document starting directly with TOC or first section. Do NOT include any cover page in the story[] list.
# Body PDF - starts with TOC or first section, NO cover
story = []
# story.append(toc) # if applicable
# story.append(PageBreak())
# story.append(first_section_content)
# ...
doc.build(story)
# Output: body.pdf (no cover)
# Pick a template 01-05 per typesetting/cover.md Part 2. Content via JSON.
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/cover_render.py" 03 --out cover.pdf --content cover_content.json
Or from Python:
import sys, os
sys.path.insert(0, os.path.join(PDF_SKILL_DIR, "scripts"))
from cover_render import render_cover, detect_fonts
fonts = detect_fonts() # host-detected, style-driven; raises if no embeddable CJK font
content = {
"kicker": "2025 年度可持续发展报告 · ANNUAL ESG REPORT",
"hero": "青松环境科技", # entity/company name = Hero
"summary": "本报告系统披露……", # 2-4 lines; auto-fill if omitted
"meta": "发布单位:青松环境科技集团 · 2026 年 3 月",
"footer": "QINGSONG GREENTECH · DOC 2026-ESG-001",
"year": "2026", "word": "REPORT",
}
# palette: map pdf.py palette.cascade --format json into these keys
palette = {"primary": "#1e3a5f", "text": "#1a1a2e", "muted": "#6b7280", "bg": "#ffffff"}
render_cover("03", content, "cover.pdf", palette=palette, fonts=fonts)
Insertion script (single output PDF):
from pypdf import PdfReader, PdfWriter, Transformation
A4_W, A4_H = 595.28, 841.89 # A4 in points
def normalize_page_to_a4(page):
"""Scale a page to A4 if its dimensions don't match."""
box = page.mediabox
w, h = float(box.width), float(box.height)
if abs(w - A4_W) > 2 or abs(h - A4_H) > 2:
sx, sy = A4_W / w, A4_H / h
page.add_transformation(Transformation().scale(sx=sx, sy=sy))
page.mediabox.lower_left = (0, 0)
page.mediabox.upper_right = (A4_W, A4_H)
return page
def insert_cover(cover_pdf, body_pdf, output_pdf):
"""Insert cover as first page of body PDF → single output file."""
writer = PdfWriter()
# Cover as page 1
cover_page = PdfReader(cover_pdf).pages[0]
writer.add_page(normalize_page_to_a4(cover_page))
# Body pages follow
for page in PdfReader(body_pdf).pages:
writer.add_page(normalize_page_to_a4(page))
writer.add_metadata({'/Title': 'Report Title', '/Author': 'Z.ai', '/Creator': 'Z.ai'})
with open(output_pdf, 'wb') as f:
writer.write(f)
⚠️ Size note:
cover_render.pyoutputs an exact A4 page (595.28×841.89pt), so it merges cleanly with the ReportLab body.normalize_page_to_a4()remains a harmless safety check. Do not use PIL Image.save('x.pdf') for any PDF conversion — DPI mapping causes severe size errors.
→ Full cover spec: typesetting/cover.md — read it before designing any cover (templates 01–09, selection matrix, font strategy).
Cover design rules (summary — see cover.md for details):
scripts/cover_render.py on an A4 canvas (bottom-left origin).cover.md Part 3.Cover Constitution (templates 01–09):
typesetting/cover.md Part 2 matching the document intent. No global default — every selection is a deliberate design decision. Template 05 (Solid Sidebar) for government/bidding/proposal.The cover page must ALWAYS be on its own page. Cover content and subsequent content (TOC, body text, etc.) must NEVER appear on the same page. This is a hard rule.
cover_render.py PDF inserted as page 1 via pypdf — isolation is inherent in the merge pipeline<section> with page-break - isolation is handled by CSSIf a generated PDF shows cover + TOC on the same page, it is a critical bug - regenerate immediately.
TA_LEFT + 2-char indent. Headings: no indent.rowHeights in Table(). Use TOPPADDING / BOTTOMPADDING to control row spacing. Fixed rowHeights cause content overflow clipping - the text renders but gets invisibly cut off.TA_JUSTIFY only when ALL of:TA_LEFTwordWrap='CJK' to ParagraphStylespaceBefore=0, spaceAfter=6-12spaceBefore=12-18, spaceAfter=6-12<b></b> tags in ParagraphspaceBefore=3, spaceAfter=6, alignment=TA_CENTERwordWrap='CJK' to ParagraphStyleColors MUST come from the palette generated in Step 2. The values below are examples - replace them with your actual palette output.
# Colors from design_engine.py palette (Step 2) - NEVER hardcode these
TABLE_HEADER_COLOR = ACCENT # From palette --c-accent
TABLE_HEADER_TEXT = colors.white # White text for header (fixed)
TABLE_ROW_EVEN = colors.white # White for even rows
TABLE_ROW_ODD = BG_SURFACE # From palette --c-mid (light stripe)
header_fill), white bold textSpacer(1, 18) → Table → Spacer(1, 6) → Caption → Spacer(1, 18)Every table MUST be horizontally centered on the page. No exceptions.
# ReportLab: ALWAYS set hAlign='CENTER'
table = Table(data, colWidths=col_widths, hAlign='CENTER')
# Width rule: table total width should be 85-100% of available content width
available_width = doc.width # or page_width - left_margin - right_margin
table_width = sum(col_widths)
if table_width < available_width * 0.85:
# Scale up columns proportionally to fill space
scale = (available_width * 0.90) / table_width
col_widths = [w * scale for w in col_widths]
# ❌ WRONG — no hAlign (defaults to LEFT, table drifts left)
table = Table(data, colWidths=[100, 200, 150])
# ✅ RIGHT — explicit centering
table = Table(data, colWidths=[100, 200, 150], hAlign='CENTER')
LaTeX equivalent:
\begin{table}[htbp]
\centering % MANDATORY
\begin{tabular}{...}
...
\end{tabular}
\end{table}
ALL text content in table cells MUST be wrapped in Paragraph(). NO EXCEPTIONS.
❌ PROHIBITED - Plain strings:
data = [
['<b>Header</b>', 'Value'], # Bold won't render!
['Pressure', '1.01 × 10<super>5</super>'], # Superscript won't work!
]
✅ REQUIRED - All text in Paragraph:
data = [
[Paragraph('<b>Header</b>', header_style), Paragraph('Value', header_style)],
[Paragraph('Pressure', cell_style), Paragraph('1.01 × 10<super>5</super>', cell_style)],
]
The ONLY exception: Image() objects can be placed directly in table cells.
W/m2, kg/m3, m/s2 (plain text exponents)Paragraph('W/m<super>2</super>', style), Paragraph('kg/m<super>3</super>', style)Paragraph('-1.246 × 10<super>8</super>', style) not -124600000Paragraph('2.5 × 10<super>-3</super>', style) not 0.0025from reportlab.platypus import Table, TableStyle, Paragraph, Image
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT, TA_JUSTIFY
header_style = ParagraphStyle(
name='TableHeader', fontName='Times New Roman', fontSize=11,
textColor=colors.white, alignment=TA_CENTER
)
cell_style = ParagraphStyle(
name='TableCell', fontName='Times New Roman', fontSize=10,
textColor=colors.black, alignment=TA_CENTER
)
data = [
[Paragraph('<b>Parameter</b>', header_style),
Paragraph('<b>Unit</b>', header_style),
Paragraph('<b>Value</b>', header_style)],
[Paragraph('Temperature', cell_style),
Paragraph('°C', cell_style),
Paragraph('25.5', cell_style)],
[Paragraph('Pressure', cell_style),
Paragraph('Pa', cell_style),
Paragraph('1.01 × 10<super>5</super>', cell_style)],
[Paragraph('Density', cell_style),
Paragraph('kg/m<super>3</super>', cell_style),
Paragraph('1.225', cell_style)],
]
table = Table(data, colWidths=[120, 80, 100])
# ⚠️ Above uses hardcoded widths - OK for this 3-col example (120+80+100=300 < available ~460pt).
# For real tables, ALWAYS calculate from available_width. See "Table Width Management" below.
table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), ACCENT), # Header: palette accent
('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
('BACKGROUND', (0, 1), (-1, 1), colors.white),
('BACKGROUND', (0, 2), (-1, 2), BG_SURFACE), # Odd row: palette surface
('BACKGROUND', (0, 3), (-1, 3), colors.white),
('GRID', (0, 0), (-1, -1), 0.5, TEXT_MUTED), # Grid: palette muted
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
('LEFTPADDING', (0, 0), (-1, -1), 8),
('RIGHTPADDING', (0, 0), (-1, -1), 8),
('TOPPADDING', (0, 0), (-1, -1), 6),
('BOTTOMPADDING', (0, 0), (-1, -1), 6),
]))
Most common table bug: columns overflow the right page margin because colWidths are hardcoded without checking available space.
Iron Rule: The sum of all colWidths MUST NOT exceed available_width.
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
page_width = A4[0] # 595.28pt
left_margin = 1.0 * inch # 72pt (adjust to your doc's actual margins)
right_margin = 1.0 * inch # 72pt
available_width = page_width - left_margin - right_margin # ≈ 451pt
# ─── Method 1: Proportional widths (RECOMMENDED) ───
# Define column ratios, auto-scale to fit available_width
col_ratios = [0.25, 0.40, 0.20, 0.15] # must sum to 1.0
col_widths = [r * available_width for r in col_ratios]
table = Table(data, colWidths=col_widths)
# ─── Method 2: Fixed + flex columns ───
# Some columns have fixed width (numbers, dates), rest fills remaining space
fixed_cols = {0: 80, 3: 60} # col 0 = 80pt, col 3 = 60pt
fixed_total = sum(fixed_cols.values())
flex_count = 4 - len(fixed_cols) # total columns minus fixed
flex_width = (available_width - fixed_total) / flex_count
col_widths = []
for i in range(4):
col_widths.append(fixed_cols.get(i, flex_width))
table = Table(data, colWidths=col_widths)
# ─── Method 3: Let ReportLab auto-calculate (simplest, less control) ───
# Pass colWidths=None - ReportLab auto-sizes based on content
# ⚠️ Risk: may still overflow if content is too wide
table = Table(data) # no colWidths
Checklist before adding any table:
available_width from your actual page size and marginsavailable_widthCommon mistake - plain strings don't wrap:
# ❌ Plain string: will overflow if text is long
data = [['Policy Name', 'Very long description that keeps going and going']]
# ✅ Paragraph: wraps within column width
from reportlab.platypus import Paragraph
from reportlab.lib.styles import getSampleStyleSheet
styles = getSampleStyleSheet()
cell_style = styles['Normal']
data = [[Paragraph('Policy Name', cell_style),
Paragraph('Very long description that keeps going and going', cell_style)]]
Do you need auto-TOC?
├─ YES → Use TocDocTemplate + doc.multiBuild(story)
└─ NO → Use SimpleDocTemplate + doc.build(story)
| Requirement | DocTemplate | Build Method |
|---|---|---|
| Multi-page with TOC | TocDocTemplate |
multiBuild() |
| Single-page or no TOC | SimpleDocTemplate |
build() |
| With Cross-References (no TOC) | SimpleDocTemplate |
build() |
| Both TOC + Cross-References | TocDocTemplate |
multiBuild() |
⚠️ CRITICAL:
multiBuild() is ONLY needed when using TableOfContentsbuild() with TocDocTemplate = TOC won't workmultiBuild() without TocDocTemplate = unnecessary overheadTo use <b>, <super>, <sub> tags, you must:
registerFont()registerFontFamily() to link normal/bold/italic variantsParagraph() objectsCRITICAL: These tags ONLY work inside Paragraph() objects. Plain strings will NOT render them.
# Use non-breaking space to prevent breaking
text = Paragraph("Professors (K.G.\u00A0Palepu) proposed...", style)
# Use wordWrap='CJK' for proper Chinese typography
styles.add(ParagraphStyle(name='BodyStyle', fontName='SimHei', fontSize=10.5,
leading=18, alignment=TA_LEFT, wordWrap='CJK'))
# Use <br/> for intentional line breaks (NOT \n)
text = Paragraph("Line 1<br/>Line 2<br/>Line 3", style)
NEVER manually create TOC with hardcoded page numbers.
Unless the user explicitly requests no cover, if a document has a Table of Contents, it MUST have a cover page. Structure: Cover (page 1) → TOC (page 2) → Content (page 3+). Do not generate a TOC without a preceding cover page.
from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak, Spacer
from reportlab.platypus.tableofcontents import TableOfContents
from reportlab.lib.utils import simpleSplit
import hashlib
class TocDocTemplate(SimpleDocTemplate):
def afterFlowable(self, flowable):
if hasattr(flowable, 'bookmark_name'):
level = getattr(flowable, 'bookmark_level', 0)
text = getattr(flowable, 'bookmark_text', '')
key = getattr(flowable, 'bookmark_key', '')
# MUST pass key as 4th element - without it, TOC entries won't have clickable links
self.notify('TOCEntry', (level, text, self.page, key))
doc = TocDocTemplate("document.pdf", pagesize=letter)
story = []
toc = TableOfContents()
toc.levelStyles = [
ParagraphStyle(name='TOCHeading1', fontSize=14, leftIndent=20, fontName='Times New Roman'),
ParagraphStyle(name='TOCHeading2', fontSize=12, leftIndent=40, fontName='Times New Roman'),
]
story.append(Paragraph("<b>Table of Contents</b>", styles['Title']))
story.append(toc)
story.append(PageBreak())
def add_heading(text, style, level=0):
# Generate a unique bookmark key for this heading
key = 'h_%s' % hashlib.md5(text.encode()).hexdigest()[:8]
p = Paragraph('<a name="%s"/>%s' % (key, text), style)
p.bookmark_name = text
p.bookmark_level = level
p.bookmark_text = text
p.bookmark_key = key # This key enables clickable TOC links
return p
# ⚠️ Orphan Heading Prevention (NOT a page break rule)
# If an H1 heading would appear in the bottom 15% of the page with no room
# for at least a few lines of content, push it to the next page.
# This is NOT "every H1 starts a new page" — it only triggers when the heading
# would be orphaned at the very bottom.
available_height = A4[1] - top_margin - bottom_margin
H1_ORPHAN_THRESHOLD = available_height * 0.15 # ~100pt ≈ 3.5cm — enough for heading + 2 lines
def add_major_section(text, style):
"""Add an H1-level heading with orphan prevention (NOT forced page break)."""
return [
CondPageBreak(H1_ORPHAN_THRESHOLD), # Only break if heading would be orphaned at page bottom
add_heading(text, style, level=0),
]
story.extend(add_major_section("Chapter 1: Introduction", styles['Heading1']))
# ... content ...
doc.multiBuild(story) # MUST use multiBuild for TOC
⚠️ CRITICAL TOC LINK RULES:
afterFlowable MUST pass key as the 4th element in the notify tuple - without it, TOC entries have no clickable linksadd_heading MUST set bookmark_key AND embed <a name="key"/> in the Paragraph text - this creates the link destination⚠️ WARNING: This manual approach creates a visual-only TOC. It has NO clickable links and NO auto-updated page numbers. Use the auto-generated TOC above (TocDocTemplate + multiBuild) whenever possible. Only use this leader-dots approach if you need very specific visual styling that the auto TOC cannot provide - and even then, prefer the auto approach.
For a professional TOC with leader dots connecting titles to page numbers:
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import inch
# Setup
doc = SimpleDocTemplate("report.pdf", pagesize=A4,
leftMargin=0.75*inch, rightMargin=0.75*inch)
styles = getSampleStyleSheet()
styles['Heading1'].fontName = 'Times New Roman'
styles['Heading1'].textColor = colors.black
story = []
# Calculate dimensions
page_width = A4[0]
available_width = page_width - 1.5*inch
page_num_width = 50 # Fixed width for page numbers
dots_column_width = available_width - 200 - page_num_width
optimal_dot_count = int(dots_column_width / 4.5) # ~4.5pt per dot at 7pt font
# Define styles
toc_style = ParagraphStyle('TOCEntry', parent=styles['Normal'],
fontName='Times New Roman', fontSize=11, leading=16)
dots_style = ParagraphStyle('LeaderDots', parent=styles['Normal'],
fontName='Times New Roman', fontSize=7, leading=16)
# Build TOC
toc_data = [
[Paragraph('<b>Table of Contents</b>', styles['Heading1']), '', ''],
['', '', ''],
]
entries = [('Section 1', '5'), ('Section 2', '10')]
for title, page in entries:
toc_data.append([
Paragraph(title, toc_style),
Paragraph('.' * optimal_dot_count, dots_style),
Paragraph(page, toc_style)
])
toc_table = Table(toc_data, colWidths=[None, dots_column_width, page_num_width])
toc_table.setStyle(TableStyle([
('GRID', (0, 0), (-1, -1), 0, colors.white),
('LINEBELOW', (0, 0), (0, 0), 1.5, colors.black),
('ALIGN', (0, 0), (0, -1), 'LEFT'),
('ALIGN', (1, 0), (1, -1), 'LEFT'),
('ALIGN', (2, 0), (2, -1), 'RIGHT'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('LEFTPADDING', (0, 0), (-1, -1), 0),
('RIGHTPADDING', (0, 0), (-1, -1), 0),
('TOPPADDING', (0, 2), (-1, -1), 3),
('BOTTOMPADDING', (0, 2), (-1, -1), 3),
('TEXTCOLOR', (1, 2), (1, -1), TEXT_MUTED), # From palette --c-muted
]))
story.append(toc_table)
story.append(PageBreak())
doc.build(story)
MANDATORY Leader Dots Rules:
| Rule | Requirement |
|---|---|
| Column widths | ✅ MUST use fixed values. Percentage-based widths are STRICTLY FORBIDDEN. |
| Dot count | ✅ MUST calculate dynamically: int(dots_column_width / 4.5). Hard-coded counts are STRICTLY FORBIDDEN. |
| Page number column | ✅ MUST be at least 40pt wide. |
| Dot font size | ✅ MUST NOT exceed 8pt. |
| Dot alignment | ✅ MUST be LEFT-aligned (visual flow from title). |
| Padding | ✅ MUST be exactly 0 between columns. |
| Bold text | ✅ MUST use Paragraph('<b>Text</b>', style). Plain strings like '<b>Text</b>' are STRICTLY FORBIDDEN. |
| Indentation | Use leading spaces for hierarchy (e.g., " 1.1 Subsection"). |
After generating any PDF with a Table of Contents, run:
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/toc_validate.py" check-pdf output.pdf
If any errors are returned, fix the code and regenerate. Common issues:
TOC_ALL_SAME_PAGE → You used build() instead of multiBuild(), so all page numbers are stuck at 1.TOC_NO_ENTRIES → Headings are missing bookmark_name/bookmark_level attributes.TOC_PAGES_INVALID → A TOC entry references a page beyond the document's total page count.Pre-register all figures, tables, and references BEFORE using them in text.
class CrossReferenceDocument:
def __init__(self):
self.figures = {}
self.tables = {}
self.refs = {}
self.figure_counter = 0
self.table_counter = 0
self.ref_counter = 0
def add_figure(self, name):
if name not in self.figures:
self.figure_counter += 1
self.figures[name] = self.figure_counter
return self.figures[name]
def add_table(self, name):
if name not in self.tables:
self.table_counter += 1
self.tables[name] = self.table_counter
return self.tables[name]
def add_reference(self, name):
if name not in self.refs:
self.ref_counter += 1
self.refs[name] = self.ref_counter
return self.refs[name]
Figures are block-level Flowables. Every image/diagram in ReportLab MUST be appended to story as a standalone Image() or wrapped in KeepTogether([image, caption]). Never simulate floating layouts by placing images inside Paragraph text or multi-column Table cells alongside body text.
Complex diagrams (>12 nodes): Decompose into a ReportLab Table (details) + a simplified diagram image (overview). The diagram gives the big picture; the table carries precision. See SKILL.md "Complex Diagram Strategy".
Diagram quality: When generating flowcharts/diagrams for PDF embedding (via Playwright screenshot or ReportLab Drawing), follow the diagram content quality rules in SKILL.md § "Diagram Content Quality Rules". Key points: connectors must not pass through nodes, no high-saturation large fills, multi-arrow convergence must use merge pattern, font sizes must be legible at final embedded size (see context-specific minimums in SKILL.md).
Cross-brief diagram pipeline: For flowcharts/architecture diagrams, generate via Playwright+CSS (HTML nodes + CSS layout + SVG connectors), screenshot at 2× device scale factor for 300dpi print quality, then embed with Image(). See SKILL.md § "Diagram Generation Strategy" for the full pipeline.
🚫 FORBIDDEN: TikZ standalone → PNG pipeline in Report brief. Report uses ReportLab, not LaTeX. Going through TikZ requires a LaTeX compiler, adds compilation steps, and models frequently produce broken TikZ code. Use Playwright+CSS instead - it's native to the existing cover rendering engine.
# Playwright+CSS diagram → PNG → ReportLab embedding
# 1. LLM generates diagram.html (CSS grid/flexbox nodes + SVG arrows)
# 2. Playwright screenshots at 2× scale (300dpi equivalent)
# 3. Embed:
from reportlab.platypus import Image
img = Image('diagram.png', width=450) # auto height via aspect ratio
story.append(img) # block-level Flowable
⚠️ CRITICAL: Preserve aspect ratio - NEVER hardcode both width and height without reading actual image dimensions.
⚠️ CRITICAL: Always constrain to available space - NEVER insert at original size if it exceeds container width/height.
Pie charts become ellipses, radar charts become diamonds, photos get stretched if you set arbitrary width/height.
→ Full overflow prevention spec: typesetting/overflow.md - read for the complete bounding box system.
from PIL import Image as PILImage
from reportlab.platypus import Image
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
def embed_image(path: str, max_width: float = None, max_height: float = None) -> Image:
"""Embed image with preserved aspect ratio, constrained to container.
ALWAYS use this pattern. Never hardcode both width and height.
If max_width is None, defaults to available_width (page - margins).
"""
if max_width is None:
max_width = A4[0] - 2.0 * inch # Default: A4 width minus 1" margins each side
if max_height is None:
max_height = A4[1] * 0.35 # ~294pt ≈ 10cm — leaves room for caption + surrounding text on same page
pil_img = PILImage.open(path)
orig_w, orig_h = pil_img.size
# Scale to fit within BOTH max_width and max_height
ratio_w = max_width / orig_w if orig_w > max_width else 1.0
ratio_h = max_height / orig_h if orig_h > max_height else 1.0
ratio = min(ratio_w, ratio_h)
return Image(path, width=orig_w * ratio, height=orig_h * ratio)
# Usage:
available_width = A4[0] - left_margin - right_margin
img = embed_image('chart.png', max_width=available_width, max_height=300)
story.append(img)
# ❌ NEVER do this - distorts the image:
img = Image('chart.png', width=400, height=250) # arbitrary height breaks aspect ratio
doc = SimpleDocTemplate(
pdf_filename,
pagesize=letter,
title=os.path.splitext(pdf_filename)[0],
author='Z.ai',
creator='Z.ai',
subject='Document purpose description'
)
After doc.build(story) completes:
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" meta.brand output.pdf
After metadata branding, remove any accidental blank pages:
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" pages.clean output.pdf -o output_clean.pdf
If blank pages were found, rename output_clean.pdf → output.pdf.
After writing PDF generation code and BEFORE executing it, sanitize using:
# Step 0: Set skill root path (see SKILL.md § Script Path Setup)
PDF_SKILL_DIR="<skill_directory>"
# Step 1: Write code to .py file
cat > generate_pdf.py << 'PYEOF'
# ... your PDF generation code ...
PYEOF
# Step 2: Sanitize (MUST run before execution)
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" code.sanitize generate_pdf.py
# Step 3: Execute
"$(command -v zcode-python || command -v python3)" generate_pdf.py
# Step 4: Add metadata (MUST run after PDF creation)
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" meta.brand output.pdf
# Step 5: Check for missing glyphs (RECOMMENDED)
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" font.check output.pdf
# Step 6: Check TOC quality (if document has TOC)
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" toc.check output.pdf
# Step 7: PDF quality assurance scan (MUST run after all other checks)
"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf_qa.py" output.pdf
FORBIDDEN patterns:
# ❌ PROHIBITED: "$(command -v zcode-python || command -v python3)" -c with inline code
"$(command -v zcode-python || command -v python3)" -c "from reportlab... doc.build(story)"
# ❌ PROHIBITED: heredoc without saving to file first
"$(command -v zcode-python || command -v python3)" << 'EOF'
from reportlab...
EOF
# ❌ PROHIBITED: executing .py without sanitizing
"$(command -v zcode-python || command -v python3)" generate_pdf.py # Missing sanitization!
→ Full overflow prevention spec: typesetting/overflow.md - read it for the complete bounding box system, two-pass rendering, fallback strategies, and all three route implementations.
from reportlab.platypus import HRFlowable
from reportlab.lib.colors import red
# Visualize spacing during development - insert between elements
story.append(table)
story.append(HRFlowable(width="100%", color=red, thickness=0.5, spaceBefore=0, spaceAfter=0))
story.append(Spacer(1, 6))
story.append(HRFlowable(width="100%", color=red, thickness=0.5, spaceBefore=0, spaceAfter=0))
story.append(caption)
# Red lines create visual markers to see actual spacing - remove before final build
Single-column, clean layout optimised for Applicant Tracking Systems (ATS). No graphics, no sidebars, no colour blocks - just well-structured text that parses perfectly by HR software.
When to use this template:
When NOT to use (use Academic brief instead):
left=1.5cm, right=1.5cm, top=1.5cm, bottom=1.5cmHRFlowable)• with tight spacing-) - this is standard typographic practice (e.g., experi-\nence, develop-\nment). ReportLab supports wordWrap='CJK' only for CJK content; for English use default paragraph wrapping with allowWidows=0, allowOrphans=0.| or ·. Each segment must stay on one line - if too long, move to next line at the separator, not mid-segment.spaceBefore / spaceAfter on section headers (from 10pt up to 18pt)leading (line height) on body text (from 14pt up to 18pt)fontSize by 0.5-1pt (from 10pt up to 11.5pt max)from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm, mm
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_LEFT, TA_CENTER
from reportlab.lib import colors
from reportlab.platypus import (
SimpleDocTemplate, Paragraph, Spacer, HRFlowable, Table, TableStyle
)
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFontFamily
# ── Font Registration ──
pdfmetrics.registerFont(TTFont('TimesNewRoman', '/usr/share/fonts/truetype/english/Times-New-Roman.ttf'))
registerFontFamily('TimesNewRoman', normal='TimesNewRoman', bold='TimesNewRoman')
# For Chinese resumes, also register SimHei:
# pdfmetrics.registerFont(TTFont('SimHei', '/usr/share/fonts/truetype/chinese/SimHei.ttf'))
# registerFontFamily('SimHei', normal='SimHei', bold='SimHei')
# ── Styles ──
# ACCENT must come from palette.generate (Step 2)
# Run: "$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" palette.generate --title "Resume" --mode minimal
ACCENT = colors.HexColor('<accent from palette>') # Replace with palette output
name_style = ParagraphStyle(
'ResumeName', fontName='TimesNewRoman', fontSize=24,
leading=28, alignment=TA_CENTER, spaceAfter=2
)
contact_style = ParagraphStyle(
'ResumeContact', fontName='TimesNewRoman', fontSize=10,
leading=14, alignment=TA_CENTER, textColor=TEXT_MUTED, # From palette --c-muted
spaceAfter=8
)
section_title_style = ParagraphStyle(
'ResumeSectionTitle', fontName='TimesNewRoman', fontSize=13,
leading=16, spaceBefore=10, spaceAfter=4,
textColor=ACCENT
)
job_title_style = ParagraphStyle(
'ResumeJobTitle', fontName='TimesNewRoman', fontSize=11,
leading=14, spaceAfter=1
)
job_meta_style = ParagraphStyle(
'ResumeJobMeta', fontName='TimesNewRoman', fontSize=10,
leading=13, textColor=TEXT_MUTED, spaceAfter=4 # From palette --c-muted
)
bullet_style = ParagraphStyle(
'ResumeBullet', fontName='TimesNewRoman', fontSize=10,
leading=14, leftIndent=14, bulletIndent=0,
spaceBefore=1, spaceAfter=1
)
body_style = ParagraphStyle(
'ResumeBody', fontName='TimesNewRoman', fontSize=10,
leading=14, spaceAfter=2
)
# ── Helpers ──
def section_header(title):
"""Section title + thin rule separator."""
return [
Paragraph(f'<b>{title}</b>', section_title_style),
HRFlowable(width='100%', thickness=0.8, color=ACCENT,
spaceBefore=0, spaceAfter=6),
]
def experience_entry(title, company, dates, location, bullets):
"""One work experience block."""
elements = [
Paragraph(f'<b>{title}</b>', job_title_style),
Paragraph(f'{company} | {dates} | {location}', job_meta_style),
]
for b in bullets:
elements.append(Paragraph(f'• {b}', bullet_style))
elements.append(Spacer(1, 4))
return elements
def education_entry(degree, school, dates, details=None):
"""One education block."""
elements = [
Paragraph(f'<b>{degree}</b>', job_title_style),
Paragraph(f'{school} | {dates}', job_meta_style),
]
if details:
elements.append(Paragraph(details, body_style))
elements.append(Spacer(1, 4))
return elements
def skills_row(categories):
"""
Skills as compact label: value pairs.
categories = [('Programming', 'Python, Java, C++'), ('Tools', 'Git, Docker')]
"""
elements = []
for cat, vals in categories:
elements.append(Paragraph(f'<b>{cat}:</b> {vals}', body_style))
return elements
# ── Build Document ──
doc = SimpleDocTemplate(
'resume.pdf', pagesize=A4,
leftMargin=1.5*cm, rightMargin=1.5*cm,
topMargin=1.5*cm, bottomMargin=1.5*cm,
title='Resume - Your Name',
author='Z.ai', creator='Z.ai'
)
story = []
# Header
story.append(Paragraph('<b>YOUR NAME</b>', name_style))
story.append(Paragraph(
'email@example.com | +86 138-0000-0000 | Shanghai, China | github.com/yourname',
contact_style
))
# Summary
story.extend(section_header('PROFESSIONAL SUMMARY'))
story.append(Paragraph(
'Results-driven software engineer with 5+ years of experience in backend systems, '
'distributed computing, and cloud-native architectures. Led a team of 8 engineers '
'delivering a real-time data pipeline processing 2M+ events/sec.',
body_style
))
# Experience
story.extend(section_header('WORK EXPERIENCE'))
story.extend(experience_entry(
'Senior Software Engineer', 'Tech Company Inc.', 'Jan 2022 - Present', 'Shanghai',
[
'Designed and deployed a microservices architecture serving 10M daily active users',
'Reduced API latency by 40% through query optimisation and caching strategies',
'Mentored 3 junior engineers; established code review standards adopted team-wide',
]
))
story.extend(experience_entry(
'Software Engineer', 'Startup Co.', 'Jul 2019 - Dec 2021', 'Beijing',
[
'Built real-time recommendation engine using collaborative filtering (CTR +25%)',
'Implemented CI/CD pipeline reducing deployment time from 2 hours to 15 minutes',
]
))
# Education
story.extend(section_header('EDUCATION'))
story.extend(education_entry(
'M.Sc. Computer Science', 'Tsinghua University', '2017 - 2019',
'GPA: 3.8/4.0 | Thesis: Distributed Graph Processing on Heterogeneous Clusters'
))
story.extend(education_entry(
'B.Eng. Software Engineering', 'Zhejiang University', '2013 - 2017'
))
# Skills
story.extend(section_header('SKILLS'))
story.extend(skills_row([
('Languages', 'Python, Java, Go, SQL, TypeScript'),
('Frameworks', 'Spring Boot, FastAPI, React, Kubernetes, Kafka'),
('Tools', 'Git, Docker, Terraform, AWS (EC2/S3/Lambda), PostgreSQL, Redis'),
]))
doc.build(story)
| Type | Description | Notes |
|---|---|---|
| h1 | 22pt + accent underline rule | KeepTogether with rule |
| h2 | 15pt, dark | No rule, no accent |
| h3 | 11.5pt bold | No accent |
| body | 10.5pt justified, 17pt leading | Supports <b> <i> <font> |
| bullet | Body size with • prefix |
Unordered list |
| numbered | Body size with N. prefix | Counter auto-resets |
| callout | Accent left border 4px + light tint bg | Max one per section |
| table | Accent header + alternating rows + outer box only | Supports fractional col_widths |
| code | Courier 8.5pt + accent left border | Optional language label |
| divider | Accent 1.2pt rule | Use sparingly |
| caption | 8.5pt muted, centered | Below images/tables |
| chart | matplotlib figure saved as PNG → Image() flowable |
Generate chart in-script, fig.savefig() → embed. Set plt.rcParams['font.sans-serif']=['SimHei'] for CJK. Always tight_layout(). Must follow typesetting/charts.md rules: delete top/right spines, dashed grid 20% opacity, donut default for pie, anti-stacking labels. |
| quote | Body italic + left indent 24pt + muted accent left border 2px | For blockquotes / testimonials |
| bibliography | Hanging indent (firstLineIndent=-24pt, leftIndent=24pt) | GB/T 7714 or APA format per language |
| math | Rendered via <super> <sub> tags in Paragraph |
For inline math; complex equations → use academic brief instead |
| Task | Best Tool | Command/Code |
|---|---|---|
| Create PDF (ReportLab) | reportlab | Canvas or Platypus |
| Fill PDF forms | Process brief | "$(command -v zcode-python \|\| command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" form.fill or annotation workflow |
| Merge PDFs | Process brief | "$(command -v zcode-python \|\| command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" pages.merge |
| Extract text | Process brief | "$(command -v zcode-python \|\| command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" extract.text |
| Extract tables | Process brief | "$(command -v zcode-python \|\| command -v python3)" "$PDF_SKILL_DIR/scripts/pdf.py" extract.table |
Key elements:
Stamp/seal placeholder (empty circle or rect with "Seal Here" text)
# Invoice table pattern
data = [['Item', 'Qty', 'Unit Price', 'Amount']]
data += [[item['name'], item['qty'], item['price'], item['total']] for item in items]
data.append(['', '', 'Total:', f'¥{total}'])
Key elements:
Page numbers mandatory
# Signature block pattern
sig_data = [
['Party A (Seal): ________________', 'Party B (Seal): ________________'],
['Date: ____/____/________', 'Date: ____/____/________'],
]
sig_table = Table(sig_data, colWidths=[250, 250])
For documents with 10+ pages:
TableOfContents() from reportlab.platypusCondPageBreak(H1_ORPHAN_THRESHOLD) before H1 headings (NOT PageBreak() — never force page breaks between chapters)"$(command -v zcode-python || command -v python3)" "$PDF_SKILL_DIR/scripts/toc_validate.py" output.pdf to verify TOC linksbookmarks=True for PDF outline navigationExam papers have unique layout requirements. These rules are MANDATORY when generating any test/quiz/exam document.
一、选择题(每小题 3 分,共 30 分) ← Section header (宋体/黑体 14pt Bold)
1. 以下哪个不是 Python 内置数据类型? ← Question stem (12pt)
A. int ← Options: indented 24pt (2em)
B. float ← Each option on new line or 2×2 grid
C. array
D. str
2. 以下表达式的值为? ← Next question
A. True B. False ← Short options: inline 2×2 grid OK
C. None D. Error
# ReportLab styles for exam papers
question_style = ParagraphStyle(
'Question', fontSize=12, leading=16,
leftIndent=0, firstLineIndent=0,
spaceBefore=12, spaceAfter=4,
)
option_style = ParagraphStyle(
'Option', fontSize=12, leading=16,
leftIndent=24, # ← MANDATORY: 24pt indent from question stem
firstLineIndent=0,
spaceBefore=2, spaceAfter=2,
)
def format_options(options):
"""
Decide option layout based on content length.
Short options (≤4 chars each, ≤4 options) → 2×2 grid table
Long options or >4 options → vertical list, one per line
"""
max_len = max(len(opt) for opt in options)
if len(options) <= 4 and max_len <= 6: # CJK: 4 chars; Latin: 6 chars
# 2×2 grid using Table
grid = Table(
[[options[0], options[1]], [options[2], options[3]]],
colWidths=[200, 200], hAlign='LEFT'
)
grid.setStyle(TableStyle([
('LEFTPADDING', (0,0), (-1,-1), 24), # Match option indent
('VALIGN', (0,0), (-1,-1), 'TOP'),
]))
return grid
else:
# Vertical list
return [Paragraph(f'{opt}', option_style) for opt in options]
Every question MUST have adequate answer space. No exceptions.
| Question Type | Minimum Space | Implementation |
|---|---|---|
| Multiple choice | 0 extra (options are the answer) | Just question + options |
| Fill-in-the-blank | Inline underline, min 80pt width | <u> </u> or ReportLab HRFlowable |
| Short answer | 40-60pt (2-3 lines) | Spacer(1, 50) |
| Calculation / math work | 120-200pt (6-10 lines) | Spacer(1, 160) with light guide lines |
| Essay / long answer | 200-400pt (10-20 lines) | Spacer(1, 300) with light guide lines |
| Proof / derivation | 160-250pt (8-12 lines) | Spacer(1, 200) |
# Answer line helper — light dashed lines for handwriting
def add_answer_lines(story, num_lines=8, line_spacing=20):
"""Add light guide lines for handwritten answers."""
for i in range(num_lines):
story.append(Spacer(1, line_spacing))
story.append(HRFlowable(
width='90%', thickness=0.3,
color=colors.Color(0.8, 0.8, 0.8), # Light gray
dash=[4, 4], # Dashed
spaceAfter=0, spaceBefore=0
))
spaceBefore=12pt between questions minimumspaceBefore=24pt before section headers (一、二、三)1. (分值: 5分) or 1. [5 pts]CRITICAL: Do NOT write long paragraphs to describe data trends. You MUST extract metrics and trends into structured visual elements.
Before writing ANY body paragraph, scan the text. If you find any of these patterns, extract them:
| Raw text pattern | ❌ FORBIDDEN | ✅ REQUIRED visual form |
|---|---|---|
| "revenue grew 12% to $4.2M" | Bury in paragraph prose | CalloutBox with bold +12% + $4.2M |
| "latency dropped from 120ms to 75ms" | Long explanatory sentence | CalloutBox or metrics table row |
| "Q1→Q2→Q3: 10%→25%→40%" | Inline numbers in text | Chart (matplotlib PNG → Image()) |
| "first...second...third..." steps | Paragraph with ordinal words | Numbered Table or process list |
| "compared to last year / vs Q2" | Nested comparisons in prose | Side-by-side comparison table |
| "accounted for 60% of total" | Percentage in paragraph | Pie chart or stacked bar chart |
Use this pattern for extracted metrics - it's visually clean and takes only 5 lines:
stat_style = ParagraphStyle(
name='StatBig', fontName='Times New Roman', fontSize=22,
leading=26, textColor=ACCENT, alignment=TA_CENTER # From palette
)
label_style = ParagraphStyle(
name='StatLabel', fontName='Times New Roman', fontSize=9,
leading=12, textColor=TEXT_MUTED, alignment=TA_CENTER # From palette
)
callout = Table(
[[Paragraph('<b>+12%</b>', stat_style)],
[Paragraph('Revenue Growth vs Q2', label_style)]],
colWidths=[160]
)
callout.setStyle(TableStyle([
('BACKGROUND', (0,0), (-1,-1), BG_SURFACE), # From palette --c-mid
('BOX', (0,0), (-1,-1), 1, ACCENT), # From palette --c-accent
('TOPPADDING', (0,0), (-1,-1), 10),
('BOTTOMPADDING', (0,0), (-1,-1), 10),
('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
]))
story.append(KeepTogether(callout))
Before submitting code, verify:
registerFontFamily() called for all used fontsinstall_font_fallback() called after font registration (auto handles <font> wrapping)Paragraph() objectsParagraph()<super> tagswordWrap='CJK' in ParagraphStyle<br/> not \n<b> tagspdf.py meta.brand called after buildpdf.py pages.clean called after buildpdf.py font.check run to verify no missing glyphspdf.py toc.check run if document has TOC (entries, pages, links)pdf_qa.py run to verify page consistency, CJK punctuation, overflow, margins, table centering, font embedding, metadata