PDFPipe

Numbers, tables and totals / Chart block

Putting a chart in a PDF so it stays sharp and keeps its meaning

A plotted chart placed in the document flow, which has to survive being printed at a size it was not drawn for and in colours that may not be there.

Why this block is harder than it looks

A chart is the one graphic in most documents that carries information rather than decoration, so every failure is a loss of meaning rather than of polish. Rasterise it and the axis labels go soft at print resolution. Rely on colour to separate the series and the whole thing collapses into identical grey shapes on a monochrome printer. Size it in viewport units and it comes out at a size unrelated to the page. And a chart drawn for a screen carries a font size chosen for a screen, which at print scale is either unreadably small or comically large.

The decisions that make it work

Reasons rather than a description of the code. Each of these is a choice that has a wrong answer, and the wrong answer is usually the default.

  • Embed as SVG, inline or as a data URI. Vector stays vector inside the document and is drawn at the printer's resolution rather than at whatever the bitmap happened to be.
  • Differentiate series by dash pattern or marker shape as well as by colour, so the chart survives greyscale and colour becomes an enhancement rather than the mechanism.
  • Set the chart's own font size in points, matching the document's small-text size, and check it in the rendered output rather than in a preview.
  • Size the container in millimetres and let the drawing scale to it through a viewBox. Percentages of an unknown container and viewport units both give you a chart at a size you did not choose.
  • Label the series directly where there are few enough of them, and keep a legend only when direct labels would collide. A legend costs the reader a lookup on every glance.
  • Wrap chart and caption together with break-inside: avoid, the same as any figure.

The fragment

Markup and the CSS it needs, and nothing else. It expects a document that already has a page rule and a base stylesheet, so paste it into one rather than opening it on its own.

html
<figure class="chart">
  <svg viewBox="0 0 480 260" role="img"
       aria-label="Monthly volume, January to June 2026">
    <!-- each series carries both a colour and a dash pattern -->
    <polyline points="20,220 100,180 180,150 260,120 340,90 420,60"
              fill="none" stroke="#1d1812" stroke-width="2"/>
    <polyline points="20,240 100,225 180,210 260,205 340,190 420,175"
              fill="none" stroke="#d23a1d" stroke-width="2"
              stroke-dasharray="6 4"/>
  </svg>
  <figcaption>Monthly volume. Solid: billed. Dashed: quoted.</figcaption>
</figure>

<style>
  .chart {
    break-inside: avoid;
    width: 150mm;                 /* a real measurement, not a percentage */
    margin: 14pt auto;
  }
  .chart svg {
    width: 100%;
    height: auto;
    font-size: 8pt;               /* points, so it prints at a known size */
  }
  .chart figcaption {
    break-before: avoid;
    margin-top: 5pt;
    font-size: 8.5pt;
    color: #444;
  }
</style>

What breaks when it is built the obvious way

Sending a screenshot of a chart. It is the fastest route and it fails on every axis: the labels blur, the aspect ratio is wrong for the space, the background is whatever the screen had, and the file is larger than the vector would have been. If the charting library can emit SVG, use it.

How to prove it survived pagination

Print a page at actual size and read the smallest axis label from normal reading distance. Then photocopy that page in black and white and confirm you can still tell the series apart. Two physical checks, both quick, and both catch things a preview does not.

Where it sits in a finished document

This page is one block. The document it belongs to has a page rule, a base stylesheet, a header and everything else around it, and repeating all of that here would make thirty pages that say the same thing. The multi-page report template is a complete file with this block already in it, so take that and change the fragment rather than assembling one from parts. The property doing most of the work here is covered on its own page, with the support caveats that belong there rather than here.

Frequently asked

Will this fragment work on its own?

Not as a whole document. It has no page rule, no margins and no base type, because those belong to the document rather than to the block, and duplicating them in every fragment would mean thirty copies to keep in step. Paste it into a template that already has them.

Why does it look right in a browser and wrong in the PDF?

Because a browser window is one continuous surface and a document is a stack of fixed rectangles. Nothing in a scrolling view exercises a page boundary, so every break rule in the fragment is inert until the content is paginated. Render the real thing with enough content to cross two or three boundaries, then look.

Do I need a special option on the render request for this?

No. Everything on this page is CSS and markup, which is your side of the boundary. What the render has to give you is a real page size with real margins, and after that the layout is decided by the stylesheet.

Other parts of a document

The blocks that sit next to this one, and one from the next group along so you are not sealed inside a single kind of problem.

Paste the fragment into the playground inside a page rule and see what it does at a real page size. That is the only way any of this gets confirmed.