The single source of truth for how a CREATE / REFORMAT HTML page must be authored so it survives Chromium's print snapshot. Every template's
skeleton.htmlis wired up to follow this spec; if you write a fresh HTML from scratch, follow this checklist before invokingbash make.sh render.
The pipeline is fixed: HTML (file or URL) → Playwright Chromium →
page.pdf(), driven by scripts/render_html.cjs.
There is no token pipeline, no ReportLab, no LaTeX. If a primitive cannot be
expressed in HTML+CSS+JS, it does not belong in this skill.
@page { size: A4 portrait; margin: 14mm 12mm; } /* default for reports */
@page { size: A4 landscape; margin: 12mm 14mm; } /* dashboards / wide tables */
@page { size: Letter portrait; margin: 0.75in 0.6in; } /* US-only audience */
A4 is the default — design every HTML against the A4 aspect ratio.
The renderer (render_html.cjs) defaults to --format A4, and Chromium's
page.pdf({ format: 'A4' }) overrides any CSS @page { size: 8.5in 11in }
you may have inherited from a web template. Mismatches between the CSS you
wrote-for and the format the renderer prints-to are the most common source
of "huge margins / clipped content / 16:9 layout that looks tiny on the
page" complaints.
Concrete numbers to design against (A4 portrait, 14×12 mm margins):
Hard rules:
@page. Without it Chromium picks 8.5×11 with 1-inch
margins, and your layout drifts.8.5in 11in or Letter unless the user is US-only. It
will be silently overridden to A4 by the renderer's --format A4
default; the visual result is "your CSS thinks Letter, the PDF is A4"
and content overflows or gets cropped. Either author against A4 in CSS,
or pass --format Letter to the renderer to make CSS and PDF match.mm / in, not px. Pixels render at 96 DPI in Chromium and
look fine on screen but wrong in print. vw / vh / 100% height
are also off-limits at the page level — you will get one "screen" per
page instead of A4 pages.--margin "14mm 12mm" and --format A4|Letter;
CSS @page takes precedence over the CLI flags only when both are set.height: 268mm (A4 portrait
297 − 2×14 mm − 1 mm bleed). For other margin schemes, recompute:
height = 297 - top_margin - bottom_margin - 1mm. Don't use 100vh —
it expands with viewport, not with the printed page.max-width: 100% (relative to content area
186 mm, not viewport). Set explicit mm heights on .chart-wrap
rather than relying on aspect-ratio (Chromium's print pipeline
computes aspect-ratio from screen, then prints; the 1.414 page ratio
isn't applied).open page.html look like the PDFThe @page rule is ignored on screen, so opening the source HTML in
a browser shows one giant horizontal page that looks nothing like the
A4 PDF. Every skeleton must include this @media screen block so
the browser preview reads as a stack of A4 sheets — the agent (and the
human reviewer) gets the same geometry in both views, no surprise at
PDF time.
@media screen {
html { background: #d0d0d0; }
body { margin: 0; padding: 20px 0; }
/* Each top-level <section> renders as one A4 sheet on screen */
body > section {
width: 210mm;
min-height: 297mm; /* full A4; cover's height: 268mm is overridden up */
margin: 0 auto 20px;
background: #fff;
box-shadow: 0 4px 24px rgba(0,0,0,0.18);
box-sizing: border-box;
overflow: hidden;
}
}
Notes:
@page, page-breaks, and Chromium's PDF
pipeline are unaffected..cover linear-
gradient, dark conclusion blocks) keep their styling because class
selectors out-rank the type selector body > section.page-break-inside: avoid
and Chart.js render timing only run during PDF generation. For
pixel-perfect screen parity, opt in to a paged-media polyfill
(Paged.js); not required by default.* { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
body. Gradient KPI cards,
cover gradients, and dark conclusion blocks all desaturate without this.printBackground: true. Pass --no-print-background
only when you want a transparent body (rare).page-break-inside: avoid (use .avoid-break { page-break-inside: avoid }).page-break-after: always on a wrapper element to force a split (e.g.
cover page, end of a chapter). Use sparingly — every forced split is a
page that can become half-empty.page-break-after: avoid (don't strand a title at
the bottom of a page).flex / grid and adjust the mm sizing.A protected element (chart card, table, conclusion block) that doesn't fit
in the remaining space gets pushed to the next page, leaving a fat blank
gap above it. Same for page-break-before: always on a section that comes
right after a short subsection. This is the #1 cause of "amateurish"
PDFs. Inspect every page and fix any blank gap > 1/3 of the page height.
Estimate before authoring. Rough capacity for A4 portrait at 14×12 mm margins, body 10.5 pt / line-height 1.55:
| Element | Vertical cost (approx.) |
|---|---|
| Section header (eyebrow + h2 + rule) | 28 mm |
| Lead paragraph (3 lines) | 18 mm |
| KPI 4-grid (one row) | 28 mm |
Chart card (default .chart-wrap) |
100 mm |
Chart card (.tall) |
130 mm |
| Data table (10 rows) | 70 mm |
| Insight callout (1 paragraph) | 18 mm |
| Recommend triple (3-up cards) | 70 mm |
| Versus pair (2-up cards) | 65 mm |
| Body line | ~5 mm |
A4 content height ≈ 268 mm (297 − 2×14 mm margin). Add up the elements on a page, check whether they fit. If a chart card (100 mm) lands on a page that already has 200 mm filled, don't let it spill — either pull something forward (a small insight / takeaway) to fill the gap on the current page, or re-order so the chart card starts the next page.
Three fixes when a page has > 1/3 blank (from cheapest to most-invasive):
Three things you must NOT do:
<br> / / .spacer { height: 50mm } to fake fullness.@page margin: 8mm) to force-fit content.page-break-before discipline: only <h1> / cover / chapter headers
should carry page-break-before: always. H2 and H3 must not — that's
how short subsections create huge gaps. Sections that flow naturally fill
better than sections that hard-split.
The check is iterative. After the first render, open the PDF and look at every page bottom. Fix the worst gap, re-render, repeat. Don't declare done until the worst-page-gap is ≤ 1/3.
Headers/footers are part of the document contract, not decoration. For any formal or multi-page output (contracts, proposals, reports, forms, manuals, translated documents), decide and implement them before the first serious render.
Decision rules:
Implementation pattern for hand-authored HTML:
@page { size: A4 portrait; margin: 18mm 20mm; }
.page {
position: relative;
min-height: 261mm; /* 297 - top/bottom @page margins */
padding-top: 10mm; /* reserved header area */
padding-bottom: 12mm; /* reserved footer area */
break-after: page;
}
.cover { padding-top: 0; padding-bottom: 0; }
.running-header {
position: absolute; top: 0; left: 0; right: 0; height: 7mm;
border-bottom: 0.5pt solid #bdbdbd;
font-size: 8.5pt; color: #555; line-height: 7mm;
}
.running-header .left { float: left; }
.running-header .right { float: right; }
.running-footer {
position: absolute; bottom: 0; left: 0; right: 0; height: 8mm;
border-top: 0.5pt solid #bdbdbd;
font-size: 8.5pt; color: #555; line-height: 8mm; text-align: center;
}
<section class="page">
<div class="running-header"><span class="left">Document title</span><span class="right">Section</span></div>
<!-- page content -->
<div class="running-footer">Page 1</div>
</section>
Verification:
pdftotext should expose running text/page numbers, but visual raster is the real gate..page bottom padding or split/reflow the protected element; do not shrink margins below the document standard just to fit it.Every generated multi-page PDF must include a visible table of contents or index that links to the corresponding section pages. This is part of the PDF navigation contract, not optional decoration.
Author HTML TOCs with internal anchors and stable target IDs:
<nav class="toc" aria-label="Table of contents">
<a class="toc-row" href="#sec-summary">
<span>Executive summary</span><span class="page-ref">2</span>
</a>
<a class="toc-row" href="#sec-methodology">
<span>Methodology</span><span class="page-ref">5</span>
</a>
</nav>
<section class="page" id="sec-summary">
<h2>Executive summary</h2>
...
</section>
Rules:
id.<a href="#..."> links, not inert <div> rows styled to
look like links./Link annotations so the visible TOC remains clickable.Verification:
pdfinfo out.pdf and/or a short pypdf probe to confirm link annotations
or outlines exist; pdftotext alone is insufficient because it cannot prove
clickability.body {
font-family:
-apple-system, BlinkMacSystemFont, "SF Pro Display",
"PingFang SC", /* Simplified Chinese — keep for CN content */
"Hiragino Sans", /* Japanese fallback */
"Apple SD Gothic Neo",/* Korean — REQUIRED for hangul, Hiragino doesn't cover it */
"Helvetica Neue", Arial, sans-serif;
font-size: 10.5pt;
line-height: 1.55;
-webkit-font-smoothing: antialiased;
}
Hiragino Sans before
PingFang SC, simplified-Chinese characters render in JP forms.@font-face from a
local file under assets/fonts/ and rely on
page.waitForFunction(() => document.fonts.ready) (the renderer already
does this).#007AFF) for non-Apple-themed reports — overused.| Class | Background | Text | Meaning |
|---|---|---|---|
pill-bad |
#ffebee |
#c62828 |
hard fail |
pill-warn |
#fff3e0 |
#e65100 |
risky / borderline |
pill-ok |
#e8f5e9 |
#2e7d32 |
passes baseline |
pill-good |
#e3f2fd |
#1565c0 |
comfortably exceeds |
pill-best |
#f3e5f5 |
#6a1b9a |
top-tier / recommended |
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script>
Chart.defaults.animation = false; /* CRITICAL — see below */
Chart.defaults.font.family = '-apple-system, "PingFang SC", sans-serif';
Chart.defaults.font.size = 10;
Chart.defaults.color = '#424245';
// … new Chart(...)
</script>
Chart.defaults.animation = false is non-negotiable. With animation on,
Chromium frequently snapshots mid-frame and you get a half-drawn bar.--wait 15000 (15 s settle window) when the page has Chart.js,
lazy images, or any other late-loading JS. Default --wait 800 is enough
for static HTML.<canvas> in .chart-card.avoid-break so a chart never splits
across pages.scales.x.type:
'logarithmic' (or y.type) and call out the log scale in the chart's
sub-title.position: 'right', grid: { display: false }
on the secondary axis.templates/data-viz-report/skeleton.html:
dual-axis bar+line, horizontal grouped bar, grouped vertical bar, scatter
matrix, stacked bar, log-axis bar, radar, dual-axis bar (price/power).
Copy from there before writing fresh init code.border-collapse: collapse, zebra nth-child(even) row, padding: 7px 10px.page-break-inside: avoid on small tables; for large tables, allow split
but repeat headers via <thead>.()、:、/). Full-width
CJK punctuation forces line-wrap in narrow columns.pill-* classes.<svg>…</svg> direct or
data:image/svg+xml;base64,….data:image/png;base64,…) or
absolute file:// paths. http:// is fragile in headless mode.<img loading="lazy"> may not paint in time — pre-load via
<link rel="preload" as="image" href="..."> or remove the attribute.max-width: 100%; height: auto; on every body image.3nm⊃2; (intent: 3nm²)
prints the literal ⊃2;. Paste real unicode: ² ³ ⁺ ⁻ ™ ° µ." into “ ” and ' into ‘ ’,
producing invalid markup that Chromium parses as a malformed attribute /
selector. Symptoms: attributes get dropped (class= becomes inert),
font-family falls back, charts won't render. Always type ASCII
" / ' in:
class="kpi blue", <a href="..."> — never
class=“kpi blue”font-family: "PingFang SC" — never "PingFang SC"'use strict' — never ‘use strict’<p>他说"你好"</p>. The rule is structural-syntax-only.;(;vs ;),
:(:vs :), ,(,vs ,), ()(()vs ()), and ——(em-dash
used as JS minus). Same rule: full-width OK in prose, ASCII-only in code.& in HTML attributes you've already escaped. Use a
raw & inside CDATA / template literals.<meta charset="UTF-8"> in <head>; without it, Chromium
guesses Latin-1 and CJK becomes mojibake.--wait for CDN-loaded scripts but cannot recover
from a hard 4xx. If the agent runs offline (corporate VPN, sandbox), vendor:
chart.js@4.4.0/dist/chart.umd.min.js → templates/<slug>/vendor/@font-face woff2 files → templates/<slug>/assets/fonts/
Reference them with relative paths from the HTML file.Run these every time before reporting "PDF ready":
# 1. The PDF actually exists and is non-trivial
ls -lh out.pdf
[[ $(stat -f%z out.pdf 2>/dev/null || stat -c%s out.pdf) -gt 50000 ]] || echo "FAIL: too small"
# 2. Page count matches expectation
pdfinfo out.pdf | grep Pages
# 3. Body text is extractable (not rasterized) — first 40 lines should be readable
pdftotext -layout out.pdf - | head -40
# 4. Visual eyeball check — open EVERY page, look at the bottom of each
open out.pdf # macOS — confirm charts drew, no blank canvases, fonts correct
# AND no page has > 1/3 blank below the last element (see §3.2)
Before committing the source HTML, confirm:
<!-- PLACEHOLDER --> tokens replaced (search for <!--).@page { size: A4 portrait } matches the renderer's --format
(default A4). No stray 8.5in 11in / Letter in the source unless
you also pass --format Letter. Cover heights computed from page
size minus margins, not 100vh.@media screen block (see §1) so
open page.html in a browser shows the same A4 sheet stack as the
PDF. If the browser preview is one wide horizontal page, the block
is missing — the agent and reviewer will both be misled by the
wrong geometry. Don't trust browser preview without it.⊃2; / &sup / & artifacts in the output." ' ; : , () allowed in code; full-width is
fine inside prose between tags).<canvas> parent has .avoid-break.Chart.defaults.animation = false is set.pdftotext alone does not prove navigation works.node scripts/render_html.cjs \
--in page.html --out report.pdf \
[--wait 15000] # extra settle (ms); 15000+ for Chart.js
[--format A4|Letter]
[--margin "14mm 12mm"] # CSS margin shorthand (1/2/3/4 values)
[--landscape]
[--scale 1] # 0.1–2.0
[--no-print-background]
[--header <html>] # page-header template (uses Playwright tokens like {pageNumber})
[--footer <html>]
The renderer:
playwright (local node_modules first, then global npm root -g).page.goto(url, { waitUntil: 'networkidle', timeout: 60000 }).page.waitForFunction(() => document.fonts.ready) (best-effort, 15 s cap).page.waitForTimeout(--wait) for late JS / charts.page.pdf(opts) then writes --out.| Don't | Why |
|---|---|
Use pixels for layout |
Print at 96 DPI — your margins drift |
Use vw / vh / 100vh for cover height |
Bound to viewport, not the printed page → cover shrinks to single screen |
Set @page { size: 8.5in 11in } while renderer prints A4 |
CSS thinks Letter, output is A4 → content overflows or massive empty bands |
| Design hero sections at 16:9 / 4:3 | A4 is 1:1.414 (taller) — 16:9 hero leaves the bottom 1/3 blank |
Skip Chart.defaults.animation = false |
Mid-frame snapshots → blank charts |
Ship CDN webfonts without document.fonts.ready |
Body text falls back to default sans |
display: none instead of visibility: hidden for print-only hides |
Chromium drops the element from layout calc, breaking page-break math |
| Force-fit a thin last page by shrinking margins | Looks cramped, wastes white space — extend the content instead |
Default to blue (#007AFF) for every report |
Reports look identical, defeats the point of HTML flexibility |
| Inline JSON content > 1 MB in the HTML | Chromium parse + GC cost exceeds your --wait budget |