May 10, 2026

CSS Print to PDF Cheatsheet: Copy-Paste Snippets by Topic

CSSPDF generationprint stylesheetcheatsheettutorial

HTML looks perfect in the browser, then falls apart as a PDF. This cheatsheet organizes common CSS print properties by category, with snippets ready to copy. It targets Chromium-based engines such as Puppeteer, Playwright, and FUNBREW PDF's quality engine, with notes where support depends on the target environment. FUNBREW PDF's fast engine uses wkhtmltopdf.

For deeper explanations, see the CSS Print Stylesheet Complete Guide.


1. Page Size and Orientation (@page)

/* A4 portrait */
@page { size: A4; }

/* A4 landscape */
@page { size: A4 landscape; }

/* US Letter */
@page { size: letter; }

/* Custom dimensions (width × height) */
@page { size: 210mm 297mm; }

Gotcha: Handling of CSS custom properties (var()) inside @page depends on the engine and version. Use literal values if you need consistent output across target environments.


2. Page Margins

/* Uniform margins */
@page { margin: 20mm; }

/* Per-side margins */
@page {
  margin-top: 25mm;
  margin-bottom: 20mm;
  margin-left: 15mm;
  margin-right: 15mm;
}

/* No margin on the first page */
@page :first { margin: 0; }

/* Mirror margins for booklet binding */
@page :left  { margin-left: 25mm; margin-right: 15mm; }
@page :right { margin-left: 15mm; margin-right: 25mm; }

3. Page Break Control

/* Force a page break before this element */
.chapter {
  break-before: page;
  page-break-before: always; /* optional fallback for older engines */
}

/* Force a page break after */
.section-end {
  break-after: page;
  page-break-after: always;
}

/* Prevent a break inside a box */
.card {
  break-inside: avoid;
  page-break-inside: avoid;
}

/* Keep table rows intact */
tr {
  break-inside: avoid;
  page-break-inside: avoid;
}

Gotcha: If break-inside does not behave as expected inside flex or grid layout, try wrapping the target in a block element and inspect the output.


4. Print Background Colors and Images

/* Force background on a specific element */
.highlighted {
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
  background-color: #fef9c3;
}

/* Apply globally */
* {
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

FUNBREW PDF's generation API has no options.printBackground. Include the CSS above in your HTML. exact requests color preservation, but user settings can take precedence.


5. Repeating Table Headers Across Pages

thead { display: table-header-group; }
tfoot { display: table-footer-group; }

/* Prevent a break inside the header row */
thead tr { break-inside: avoid; }

Your HTML must use an explicit <thead> element. Bare <tr> elements inside <table> won't repeat.


6. Page Numbers, Headers, and Footers

/* Page number at the bottom center */
@page {
  @bottom-center {
    content: counter(page) " / " counter(pages);
    font-size: 10pt;
    color: #6b7280;
  }
}

/* Header text at the top center */
@page {
  @top-center {
    content: "FUNBREW PDF — Confidential";
    font-size: 9pt;
  }
}

Note: Chrome 131 added margin boxes (@bottom-center, etc.). With FUNBREW PDF, select the quality engine and verify page numbers and margins in your environment. The generation API has no headerTemplate or footerTemplate option.


7. @media print Basics

@media print {
  /* Hide navigation and sidebars */
  nav, aside, .no-print { display: none !important; }

  /* Switch to a print-friendly font */
  body { font-family: Georgia, serif; font-size: 11pt; }

  /* Append URL after each link */
  a[href]::after { content: " (" attr(href) ")"; }

  /* Only for external links */
  a[href^="http"]::after { content: " (" attr(href) ")"; }
}

8. Orphans and Widows

p {
  orphans: 3; /* minimum lines left at the bottom of a page */
  widows: 3;  /* minimum lines carried to the top of a page */
}

9. Named Pages (Mixed Page Sizes)

@page portrait-page  { size: A4 portrait; }
@page landscape-page { size: A4 landscape; }

.wide-chart {
  page: landscape-page;
  break-before: page;
}

10. Common Failures and Fixes

Symptom Cause Fix
Background color disappears Output-specific adjustment or user settings Add print-color-adjust: exact and inspect output
Page break is ignored Selector or engine support Check the target engine; add page-break-* for older engines
Table header doesn't repeat No <thead> element Add explicit <thead> in HTML
Page margins differ from intent Both CSS and API margins are set Check @page and options.margin-*
Page numbers don't appear Engine lacks margin-box support Use Chromium 131+; select quality with FUNBREW PDF
var() breaks inside @page Engine/version differences Replace with literal values and verify
Break rules fail inside flex Layout and engine combination Try wrapping the target in a block element

11. CSS vs. API Options in FUNBREW PDF

This table maps CSS controls to the options accepted by the generation API. API margin values are in millimeters.

Goal CSS API option
Paper size @page { size: A4; } options.page-size: "A4"
Margins @page { margin: 20mm; } options.margin-top / margin-right / margin-bottom / margin-left (0–50)
Background colors print-color-adjust: exact No corresponding option; use CSS in the HTML
Page numbers @page @bottom-center in Chromium 131+ No corresponding option; use CSS in the HTML
Header/footer @page @top-center / @bottom-center in Chromium 131+ headerTemplate / footerTemplate are not supported

See the full FUNBREW PDF API docs for all available options.


What to Read Next

Powered by FUNBREW PDF