Skip to content

feat(pdf): PDF output without a browser, presentations first (#44, #46) - #57

Merged
pierreneter merged 5 commits into
p10node:mainfrom
mkroemer:feat/pdf-output
Nov 27, 2026
Merged

pierreneter merged 5 commits into
p10node:mainfrom
mkroemer:feat/pdf-output

Conversation

@mkroemer

Copy link
Copy Markdown
Contributor

Part of #44 and the first milestone, #46 (PPTX → PDF).

Stacked on #54 (font files, #45): the base is feat/font-files. Once #54 merges, this goes to main.

What it does

A presentation is written as a PDF file by the library itself. It needs no browser, no print dialog and no dependency, and it runs in the worker or on a server.

await doc.call('fonts.set', fonts)             // the font files text is set in (#54)
const pdf = await doc.call('pdf')              // Blob, application/pdf: every slide but the hidden ones
await doc.call('pdf', { slides: [0, 2], hidden: true, title: 'Q3' })
const svg = await doc.call('pptx.svg', 0)      // one slide as SVG, drawn as the PDF draws it

The design follows #44: layout produces pages of drawing commands, and a backend paints them as PDF or SVG.

  • src/core/draw holds the drawing layer:

    • pages of drawing commands: paths, glyph runs, pictures, groups with transforms, clips and opacity, and links;
    • SVG path parsing (arcs included), matrices and colors;
    • PNG and GIF unpacked, and JPEG read for its size;
    • Helvetica as the fallback font;
    • a text layout that breaks, aligns and spaces lines as the viewer's CSS does, measured in the host's font files through FontBook;
    • an SVG reader, used for the engine's own charts and for SVG pictures;
    • an SVG backend.
  • src/core/pdf/writer.ts writes the file:

    • fonts are embedded as subsets: TrueType as CIDFontType2, PostScript outlines as OpenType;
    • ToUnicode maps, with /ActualText where one glyph stands for different text;
    • JPEG is copied as it is; PNG and GIF get soft masks;
    • axial and radial shadings, and stroke patterns;
    • isolated transparency groups, and gradient alpha through luminosity soft masks;
    • link annotations, and an outline from the slide titles.
  • src/core/pptx/draw.ts turns a slide into a page, placed the way SlideView places it:

    • preset and custom outlines, fills, arrowheads, flips and rotation;
    • pictures with their crop and shape;
    • tables whose rows grow to fit their text;
    • charts, drawn through the viewer's own chart SVG;
    • text boxes with insets, anchors and vertical text.

    The preset geometry moves from src/react to core.

  • Licenses and limits:

    • Licenses (fsType): a font that is restricted or bitmap-only is set in Helvetica, with a warning. A font that may not be subset is embedded whole.
    • Limits: a picture is unpacked to at most 64 M pixels, and its PNG data to at most 1 MB past its pixels. An SVG draws at most 100k elements and 1M path segments, counted as they are drawn, use and clip paths included.
    • Failures: a damaged font or an unreadable picture falls back with a warning instead of failing the whole file.

Fixes to #54 found along the way

  • Node Buffer aliasing: Font.open kept a Node Buffer as it was. Its slice shares memory, so subsetFont wrote the subset's glyph count into the font's own hhea table and read every side bearing from the wrong place. Glyphs were then drawn off their advance. Browsers never hit this; a server reading fonts with readFileSync does.
  • Each font file read once: FontBook now reads a file declared for several families once, so it becomes one font and one subset.
  • CID subsets: a subset of a CID-keyed CFF font now declares Adobe-Identity-0, since its CIDs are renumbered.

Viewer change

A cropped picture that is also flipped is now flipped within its frame, as PowerPoint does: crop first, then mirror. The viewer used to mirror the whole picture about its own centre, which showed the other side of the crop. The PDF and the viewer now agree.

Review

Codex reviewed this branch in three rounds before the push. Everything they found is fixed and has a test.

  • Round 1, 15 findings. Fixed in 13f8175:
    • hostile SVG, PNG and GIF input;
    • copied text wrong when a line mixes two fonts;
    • fsType embedding flags;
    • quadratic layout in narrow boxes;
    • transparency;
    • URIs written as ASCII;
    • empty slide selection;
    • number syntax.
  • Round 2, the fixes re-checked, plus 8 new findings. Fixed in 857f05d:
    • clip paths count toward the SVG budget;
    • link addresses with a lone surrogate;
    • mask resource names;
    • form boxes;
    • long strings in a CFF font;
    • PNG checksum;
    • breaks after supplementary-plane ideographs.
  • Round 3, final check. Fixed in 186cacc:
    • matrices written to 6 significant digits, so boxes and patterns agree with what is drawn;
    • no stale text when a text command is reused;
    • zero-length gradients;
    • fill and stroke painted together again;
    • unique names for embedded fonts.

Tests and checks

  • test/pdf-output.test.ts has 48 tests. They cover:
    • PDF structure: xref, fonts, ToUnicode, links, outline, shadings, forms and masks;
    • layout;
    • pictures;
    • SVG input;
    • slides;
    • the engine methods;
    • every review finding.
  • bun run typecheck is clean.
  • Full suite: 1690 pass and 19 fail. The 19 fail on the base feat/font-files without this PR: encodings, plus ODF fixtures that need main's zip fix fix(core): part of a Blob read as a stream ends where it was asked to, on any runtime #52.
  • Checked by hand on the sample deck and three real-world decks (4–15 slides):
    • poppler renders every page and pdftotext extracts clean text;
    • pdffonts shows embedded subsets with Unicode maps;
    • Ghostscript reports no errors;
    • 50–330 ms per deck;
    • slide 1 of the sample matches the viewer's rendering.

Not yet

  • Shadows and other effects.
  • Pattern fills, which are drawn in their foreground color.
  • Tiled pictures, which are stretched.
  • Text columns.
  • Autofit that grows the shape to its text.
  • Notes pages.
  • SmartArt and OLE objects.
  • EMF, WMF, TIFF, BMP and WebP pictures, which are left out with a warning.
  • The browser comparison of SVG against the DOM in e2e that PDF output without a browser #44 describes.

Next

🤖 Generated with Claude Code

mkroemer and others added 5 commits November 25, 2026 18:01
A presentation is written as a PDF file by the library itself, in the
worker or on a server, with no browser and no dependency (p10node#44, first
milestone p10node#46):

- src/core/draw: pages of drawing commands (paths, glyph runs, pictures,
  groups with transforms, clips and opacity, links); SVG path data and
  transforms; PNG and GIF unpacked, JPEG read for its size; a text layout
  that breaks, aligns and spaces lines as the viewer's CSS does, measured
  in the host's font files (FontBook), Helvetica where none serves; an
  SVG reader for the engine's charts and SVG pictures; an SVG backend;
- src/core/pdf: the PDF writer: fonts embedded as subsets (TrueType as
  CIDFontType2, PostScript outlines as OpenType) with ToUnicode maps,
  Helvetica as the PDF's own font, JPEG copied, PNG / GIF with soft
  masks, axial and radial shadings, transparency, link annotations, an
  outline from the slides' titles;
- src/core/pptx/draw: a slide as a page, placed as SlideView places it:
  shapes and their outlines, fills, arrowheads, flips and rotation,
  pictures with crop and shape, tables whose rows grow to their text,
  charts, text boxes with insets, anchors and vertical text. The preset
  geometry moves to core;
- the engine: `pdf` (slides, hidden ones, title) and `pptx.svg`.

Fixes found on the way, in the font files of p10node#45:

- Font.open kept a Node Buffer as it is: its `slice` shares memory, so
  subsetFont wrote the subset's glyph count into the font's own hhea and
  read every side bearing from the wrong place (glyphs drawn off their
  advance in a PDF). Bytes are now a plain Uint8Array and tables are
  copied, not sliced;
- FontBook reads a file declared for several families once (one font,
  one subset in a PDF).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es, transparency

- SVG pictures count elements and path segments as they are drawn, `use`
  included, and a `use` of itself is drawn once: a small file cannot
  repeat itself into millions of commands;
- PNG data is unpacked up to what its pixels take, a GIF frame is checked
  before it is unpacked, 16-bit color keys compare all their bits;
- each glyph run stands for its own characters (a line in two fonts was
  copied with characters twice); a glyph that stands for other text in
  another place (a ligature and U+FB01) is given /ActualText;
- runs are cut where their glyphs are, by halving, not by reading every
  glyph for every line (a narrow box of long text was quadratic); lines
  are never broken inside a cluster; tabs move the pen and draw nothing;
- fonts' licenses: bitmaps only is not embedded, no subsetting embeds the
  whole font; a font that fails to embed, or a picture that cannot be
  read, falls back with a warning instead of failing the file;
- a subset keyed by CID says Adobe-Identity-0, as the PDF does;
- transparent groups are isolated transparency groups, a gradient whose
  alpha changes has a soft mask; hard stops make no zero-width interval;
  numbers are decimal and clamped; subset tags are unique; link
  addresses are ASCII;
- an empty selection of slides is refused instead of writing a file of
  no page.

A cropped and flipped picture is flipped in its frame, as PowerPoint
does: the PDF already did; the viewer flipped the whole picture about its
own centre and showed the other side of the crop, and now does the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- an SVG's clip paths count toward its segments;
- a link with half a surrogate pair is written (U+FFFD), not an error that
  takes the file down; a picture part that throws, or rejects with
  anything, leaves its picture out;
- what each glyph stands for is worked out once per run (the ActualText
  check made long runs quadratic); a line in two fonts finds its pieces
  by halving; a line can end after an ideograph of two UTF-16 units;
- soft masks have names of their own (they could take an alpha's name);
  a form's box is the page in the form's space, not a fixed square that
  left far-off drawings out; a gradient along a line is a shading pattern
  (with a soft mask where its alpha changes);
- gradient stops are rounded before their intervals are made, so /Bounds
  only grows; a font embedded whole has no subset tag;
- a CFF's strings are read byte by byte (one long string was past the
  arguments a call may take); a PNG's data is read to its end, so one cut
  short of its checksum is refused (at most 1 MB past its pixels is read);
- the docs say a CID-keyed font that may not be subset is set in
  Helvetica.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- transforms, pattern matrices and gradient stops are written to six
  significant digits, and the boxes and patterns worked out from them use
  the numbers as written: a scale of 0.00149 written as 0.001 left a
  form's box short of what it drew, two nested small scales made a
  pattern's matrix 0 and its stroke vanished;
- what a glyph stands for is worked out on each export, not kept: a text
  command changed between two exports gave the old text;
- a gradient of no length paints its last stop's color, as SVG says;
- a color fill and a color outline are painted together again (B), so
  translucent ones overlap as they did;
- two embedded fonts of one name keep apart (-2, -3…);
- a rejection that cannot be made a string is reported as an error;
- a PNG that unpacks to more than 1 MB past its pixels is refused;
- a line can end after an ideograph of CJK Extension H.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`pdf({ slides })` wrote a hidden slide it named: a list of pages (a
server's `nativePageRanges`) then showed what was hidden. Hidden slides
are now written only with `hidden: true`, whether `slides` names them or
not; a list of nothing but hidden slides is refused.

Found by the review of the convert package's PDF output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pierreneter
pierreneter changed the base branch from feat/font-files to main November 27, 2026 09:32
@pierreneter
pierreneter merged commit 2307e84 into p10node:main Nov 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants