Skip to content

feat(convert): PDF output for presentations: CLI, HTTP service, Gotenberg's route (#43, #44) - #58

Draft
mkroemer wants to merge 14 commits into
p10node:mainfrom
mkroemer:feat/convert-pdf
Draft

mkroemer wants to merge 14 commits into
p10node:mainfrom
mkroemer:feat/convert-pdf

Conversation

@mkroemer

Copy link
Copy Markdown
Contributor

This wires the engine's PDF output (#57) into @p10node/convert (#56). Gotenberg's /forms/libreoffice/convert now answers with a PDF for presentations instead of 501.

Draft: depends on #54 → #57 and on #56. This branch merges both of them, so until they land the diff against main includes them. To review only this PR's own changes: 4fa0ce6, e4d1659 and 135d50f, or compare feat/pdf-output…feat/convert-pdf restricted to packages/convert, test/convert-service.test.ts and the one-line engine.readFont hook in src/core/worker/engine.ts.

What it does

office-convert deck.pptx deck.pdf --slides 1-3
curl -F files=@deck.pptx http://localhost:3000/forms/libreoffice/convert -o deck.pdf                  # as Gotenberg
curl -F files=@deck.pptx -F to=pdf -F slides=2 http://localhost:3000/convert -o slide2.pdf

Formats. .pptx, .ppt, .odp, Keynote and Visio files are written as PDF; all of them open as presentations. A document or a workbook asked for as PDF is refused as not-implemented (501), with a pointer to #48 and #47. When a file's bytes show it is one, the refusal comes before the worker pool is asked, so a busy server still answers 501, not 503.

Fonts

  • The system's font folders are scanned once for each file's family, weight and style, plus any font files or folders given with --fonts / FONTS, which come first. Each file is declared by its path.
  • The engine reads a file from disk only when text needs it, through a new engine.readFont hook. Files read lately stay in a 256 MB cache.
  • Fallback order:
    1. the document's own font;
    2. its metric-compatible stand-in: Carlito for Calibri, Caladea for Cambria, Liberation for Arial, Times New Roman and Courier New;
    3. sans-serif, serif or monospace, mapped to Liberation, Arial or DejaVu;
    4. fonts for other scripts, such as Noto Sans CJK;
    5. Helvetica, with a warning that names the missing font.
  • The pool scans fonts once, in the calling thread, and sends the list to each worker thread once.
  • serve finishes the scan before it starts listening.

Gotenberg's route

  • nativePageRanges picks the slides, and exportHiddenSlides includes hidden ones. Hidden slides are written only when asked for, even if a page range names them.
  • These fields are answered 400 instead of being silently ignored: merge of several files, pdfa, pdfua, userPassword, ownerPassword, splitMode and password. A request for a password-protected PDF never gets an unprotected one back.
  • Boolean fields are parsed as Go's ParseBool parses them, and a field sent twice is read by its first value, as Gotenberg does.
  • Several files come back as a ZIP of deck.pptx.pdf entries. A file that can't be read gets 400.

Command line. New flags: --slides 1-3,5, --hidden, --fonts <path> (can be repeated) and --no-system-fonts.

Docker image. Now ships Carlito, Caladea, Liberation and DejaVu. --build-arg EXTRA_FONTS=fonts-noto-cjk adds Chinese, Japanese and Korean fonts. Your own fonts can be mounted: -v ./fonts:/fonts -e FONTS=/fonts.

Limits

  • Page ranges are capped at 100,000 pages.
  • Font folders are read entry by entry, with at most 5,000 files and 6 levels deep.
  • Font files over 64 MB are skipped, even one that grows while it is read. A pipe named like a font doesn't block the scan.
  • Jobs waiting for the font list count as queued work: they count toward the queue and byte limits and can be cancelled or closed.

Review

Codex reviewed this branch twice before the push; every finding is fixed and has a test.

Round 1 (8 findings, fixed in e4d1659):

  • page-range bounds;
  • password fields;
  • hidden slides;
  • fonts for other scripts with bold and italic text;
  • the pool's handling of jobs waiting for fonts;
  • a file's name overriding its content;
  • bounds on font discovery and the cache;
  • Gotenberg's booleans, ZIP names and error codes.

Round 2 (re-check, fixed in 135d50f):

  • the early 501 decided by the file's bytes only;
  • a field sent twice is read by its first value;
  • trailing spaces in booleans;
  • single-file names;
  • leading zeros in page numbers;
  • a configured font list is copied when the converter is created;
  • the byte budget and releasing the upload while waiting for fonts;
  • the queue check counts free threads;
  • multi-file coverage families;
  • pipes in font folders, and files that grow while being read.

The hidden-slide rule also lives in the engine, so it went to #57 as 81502b0.

Tests and checks

  • Suite: full on Bun 1.4.5 (CI uses 1.4.4): 1767 pass, 0 fail. test/convert-service.test.ts has 38 tests, including the PDF, PDF: review and PDF: re-check blocks.
  • Bun 1.3.6: it segfaults intermittently in worker-thread tests, on the base branch as well; 1.4.5 ran clean six times in a row.
  • Typecheck and package build:
    • bun run typecheck is clean;
    • bun scripts/build-packages.ts convert builds, and its type declarations still refer only to the package's own files.
  • Node 26: the built dist/cli.js converts decks on worker threads.
  • Docker, by hand:
    • the 5-slide sample converts in 0.13 s, with Calibri set in Carlito;
    • Word files get 501, password fields 400, and two files a ZIP;
    • a Keynote file uploaded as .docx converts;
    • /health is up.

Next

  • XLSX → PDF #47 (XLSX → PDF) and DOCX → PDF #48 (DOCX → PDF). The Gotenberg route then answers for every format.
  • Gotenberg's merge, which needs a multi-document writer.

🤖 Generated with Claude Code

pierreneter and others added 14 commits November 23, 2026 17:03
…ures, line metrics, subsets

src/core/font reads TTF, OTF (CFF, also CID-keyed), TTC, WOFF and WOFF2
files with no DOM and no dependency, for a layout that breaks lines the
same everywhere (#30) and for PDF output (p10node#44):

- Font: character map, glyph widths, metrics, line metrics as browsers and
  as Word take them, names, what the font says about itself;
- layoutText: a run of text as glyphs, with GPOS pair and single
  adjustments (else the kern table) and the single, multiple and ligature
  substitutions browsers apply by themselves, text cut where its script
  changes. What is not applied (contextual rules, marks, Apple's tables,
  scripts that need shaping) is detected: the run then has exact false
  and the reason, or is handed to the host's Shaper;
- FontBook: the FontSource list of DocViewer's fonts, the file for a text
  chosen as CSS chooses (family list, style, weight, unicode-range, each
  character in the first file that has it), fetched when first asked for;
- subsetFont and toUnicodeCMap: the glyphs used, renumbered, as a font
  file of its own (TrueType, or CFF as OTF and as the bare table), and
  the text of each glyph for a PDF;
- in the worker: fonts.set, fonts.measure, the font:error event.

WOFF2 is unpacked by the runtime's DecompressionStream('brotli'): Node,
Bun and Firefox have it, Chromium 156 does not and is told so in words.

Checked against HarfBuzz on 796 font faces (scripts/font-check.ts): of
10,094 runs called exact none differs; 997 subsets have their font's
widths and draw the same picture.

Fixes p10node#45

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…CLI, HTTP service with Gotenberg's routes)

First part of p10node#43. The engine runs in Node or Bun on worker threads and converts
files without LibreOffice:

- convert(bytes, { to }): opens any format the viewer opens, waits until it is
  read whole (docx.start / sheet loading hidden), writes DOCX / XLSX / PPTX, or
  the document as text, Markdown, HTML, CSV / TSV
- Converter: a pool of worker threads; a file past its time limit or whose
  caller gave up ends with its thread, a full queue is refused
- office-convert: files on the command line, several at once, size and time
  limits; `office-convert serve` for the HTTP service
- createHandler: POST /convert (multipart or the file as the body), Gotenberg's
  /forms/libreoffice/convert (501 until PDF, p10node#44), /health, /version,
  Gotenberg-Output-Filename and Gotenberg-Trace, a ZIP for several files
- Dockerfile: built with Bun, runs on node:26-slim with the bundle only

Core: XlsxWorkbook.text() waits for the values computed after reading, so a CSV
written right after opening has the formulas the file gave no value for (it
had them empty before, also in the browser's Download as CSV).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nputs never overwritten

- pool: the bytes are copied before they move to a thread (a Node Buffer can share
  its memory: posting it failed and left the thread busy); a failed dispatch
  rejects its conversion and frees the thread; input bytes running and waiting
  have a budget (`bytes`, 1 GB); a queued conversion's abort listener is removed
  once it leaves the queue
- convert: `maxOutput` (1 GB; a sparse sheet is refused before its CSV is
  written) and `maxUnpacked` (gzip inputs, 1 GB, through a new `maxUnpacked`
  open parameter of the engine); a sheet index must be a whole number
- HTTP: Gotenberg's route answers 501 before the body is read or the pool asked;
  a file of a request failing gives up the others; ZIP names unique without
  their case; `too-large` is 413
- CLI: stdout is written whole before the process ends (exitCode, not exit);
  an output that is one of the inputs under any name (a link) is refused, and
  without --force a file is only created (wx); stdin is refused once past
  --max-size; files are read and converted as threads become free; `in out` with
  an existing output is refused instead of read as two inputs
- types: relative imports of the .d.ts files with .js, for NodeNext

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ds, held bodies

- CSV / TSV: the size limit is applied by the exporter (xlsx.text takes a
  limit): the cells in use are counted, not the sheet's extent, so a blank
  formatted cell far away no longer refuses a small export, and the text is
  counted while it is written
- pool: a thread that cannot start fails its conversion and gives its bytes back
- HTTP: request bodies held at once have a budget (`maxPending`, 1 GB): past it
  503 with Retry-After, before the pool is asked
- CLI: named files are read up to --max-size too (a pipe or a device has no
  size); an output is checked on the file opened, before it is truncated, against
  the inputs and stdin when it is a file; no byte budget (files go one per
  thread); `a.doc b.md` with both existing converts both again (only --force
  with an existing second name is refused)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
…rvice and Gotenberg's route

The engine's PDF output (p10node#44) wired into @p10node/convert:

- convert(…, { to: 'pdf' }) writes presentations (.pptx, .ppt, .odp,
  Keynote, Visio) as PDF; `slides` (indexes) and `hidden` choose the
  slides. Documents and workbooks answer `not-implemented`, naming p10node#48 and
  p10node#47;
- fonts: the system's font folders (and --fonts / FONTS before them) are
  read once for their families, weights and styles, and declared by their
  paths; the engine reads a file from the disk when text first needs it
  (`engine.readFont`), from a cache of the files read lately. The generic
  families map to Liberation, Arial or DejaVu, then to fonts of other
  scripts. The pool finds them once in the calling thread and hands the
  list to its threads;
- Gotenberg's /forms/libreoffice/convert answers with PDF for
  presentations, `nativePageRanges` choosing the slides; documents and
  spreadsheets are answered 501 before the pool is asked; `merge` of
  several files and `pdfa` / `pdfua` are answered 400, not ignored.
  /convert takes `to=pdf`, `slides` and `hidden`;
- office-convert: deck.pptx deck.pdf, --slides, --hidden, --fonts,
  --no-system-fonts; `serve` reads the fonts at start and says how many
  families it found;
- the Docker image has Carlito, Caladea, Liberation and DejaVu (the
  widths of Calibri, Cambria, Arial, Times New Roman and Courier New);
  EXTRA_FONTS adds packages (fonts-noto-cjk).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s fields, fonts

- page ranges name at most 100,000 pages and numbers of six digits: a list
  past that is refused whole, not cut short, and cannot keep a loop going;
- hidden slides are written only when asked for (`hidden`,
  `exportHiddenSlides`), also when a list of slides names them: a page
  range does not show what was hidden;
- Gotenberg's route refuses what it is asked for and does not do yet
  (`userPassword`, `ownerPassword`, `splitMode`, `password`, `pdfa`,
  `pdfua`, `merge` of several files) instead of answering with a file
  that is not what was asked for; booleans are read as Go reads them;
  the PDFs of a ZIP are named after their files (deck.pptx.pdf); a file
  that cannot be read is 400, as Gotenberg answers;
- PDF of a document is refused before the pool only when the file's name
  says so and its bytes do not say it is a presentation;
- conversions waiting for the font list are queued work: counted in
  `load`, held to the queue's limit, given up with their caller or when
  the converter closes; a font list that cannot be found is a rejection
  (and looked for again next time); a thread is sent the list once;
  `serve` finds it before it listens;
- fonts of other scripts are declared at the weights and styles of the
  family before them, so bold and italic text falls back to them too;
- font folders are read entry by entry, font files are read whole only
  when they can be one (64 MB), and the cache keeps no file larger than
  itself and counts only what was read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…its counted

- PDF of a document is refused before the pool only when the file's bytes
  say it is a document or a workbook (an Office or OpenDocument package,
  an old Office file, RTF): a presentation under any name, a Keynote file,
  or a damaged file go to the engine, which decides (400 on Gotenberg's
  route for one it cannot read);
- a form field given twice is read by its first value, as Gotenberg reads
  it (a password field followed by an empty one is still refused); its
  booleans are read as they are, ` true ` is not true; /convert's
  `hidden` reads true, 1 and yes in any case, as before;
- a single PDF from Gotenberg's route keeps its name (deck.pdf); only the
  files of a ZIP are named deck.pptx.pdf;
- page numbers may have leading zeros again;
- a font list given to the Converter is taken as it is then (every thread
  has the same), and with it no conversion waits; a conversion waiting for
  a list being found counts toward the bytes the pool takes and lets go of
  its input when it is given up; a free thread is not a full queue;
- every file of the face of a family of other scripts serves its
  characters; a font file is opened without waiting (a pipe named like a
  font does not hold the scan up) and read up to the limit also when it
  grows while it is read.

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>
@mkroemer
mkroemer marked this pull request as draft November 25, 2026 18:56

This branch has not been deployed

No deployments
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