Skip to content

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

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

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

Conversation

@mkroemer

@mkroemer mkroemer commented Nov 25, 2026 •

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.

#54, #56 and #57 are merged, and main (v0.20.0) is merged into this branch, so the diff against main is this PR's own: packages/convert, test/convert-service.test.ts and the 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.

  • Fields that change what the PDF shows or claims are answered 400 instead of being silently ignored:

    • merge of several files;
    • PDF/A and PDF/UA: pdfa, pdfua, generateTaggedPdf;
    • passwords: password, userPassword, ownerPassword;
    • splitting: splitMode, splitSpan, splitUnify;
    • watermarks and stamps: nativeWatermarkText, nativeTiledWatermarkText, watermark…, stamp…;
    • rotateAngle, and metadata other than {} or null;
    • a permission turned off (allowCopying=false…): it can't be kept without encryption;
    • notes: exportNotes, exportNotesPages, exportOnlyNotesPages, exportNotesInMargin;
    • attachments: embeds, addOriginalDocumentAsStream, Factur-X.

    Every time such a field is sent counts, in the form or the query, so a watermark sent after an empty one is still refused. A request for a password-protected or watermarked PDF never gets a plain one back. Files sent as embeds, watermark or stamp aren't treated as files to convert.

  • Hints are left as they are: image quality, bookmarks, viewer preferences, and Writer's or Calc's own options.

  • Boolean fields are parsed as Go's ParseBool parses them, and a field sent twice is read by its first value, as Gotenberg does. /convert keeps reading a repeated field by its last value, as before.

  • A PDF file is answered 400 on both routes: since feat(pdf): PDF files open view only, drawn by pdf.js, their text selectable with the mouse #69 the engine opens PDFs view-only, and there's nothing to convert.

  • 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 take free threads first, then places in the queue. They count toward the byte limit, can be cancelled or closed, and have the conversion's time limit, so a font list that never arrives fails them in time.
  • What a file's own bytes already refuse (a PDF input, or a document or workbook to PDF) is answered before anything waits for fonts or a thread. The kind is read as the engine reads it, without parsing the content: an old Office file's document stream, an OpenDocument mimetype, or the Office main part named by the package's relationships and content types. That check runs on the server's thread, so it's bounded: 4,096 ZIP entries, 1 MB per part it reads, 32 MB for old Office files.
  • The font list is searched once for all the PDFs waiting. Those PDFs hold their threads, so a conversion queued after them can't take one; a PDF that gives up hands its thread to the queue. A search no one waits for any more is started again for the next PDF, and none starts after close().

Review

Codex reviewed this branch six times before pushing; 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.

Round 3 (after merging main with #69's PDF files, fixed in 1f0c91b):

  • a PDF whose text names word/document.xml, or a deck that carries a document part, was answered 501 as a Word file;
  • Gotenberg's watermark and other content-changing fields were silently ignored;
  • a font list that never arrives left PDF jobs pending, and its failure hid the real refusal;
  • a font list given as a failing promise was an unhandled rejection;
  • free threads were counted twice while fonts loaded;
  • the first-value rule had also changed /convert.

Round 4 (re-check, fixed in 4cb6fd3):

  • the early check still guessed from part names: it now reads the package the way the engine does;
  • a conversion without fonts could take the thread a font-waiting PDF was counted on;
  • a stalled font search was never started again, and every waiting PDF left a callback on it;
  • metadata={} was refused; allow*=false and splitUnify were dropped silently;
  • a refused field sent twice, or in the query, could slip through;
  • a failing font promise from another JavaScript realm was unhandled.

Round 5 (re-check, fixed in 8fa481d):

  • the early check could unpack large parts on the server's thread: it now reads at most 4,096 ZIP entries, the parts that name the kind up to 1 MB each, and old Office files up to 32 MB, and otherwise leaves the decision to the engine in a thread;
  • a conversion queued after a font-waiting PDF could still take its thread;
  • a shared font result, found or failed, could reach a file before its own check;
  • an aborted last waiter left a stalled search in place, and a search could start after close();
  • merge sent twice, whitespace-only metadata, and a one-shot thenable font list.

Round 6 (final check, fixed in 68fbccf):

  • PDFs waiting for fonts now keep their arrival order for the threads they hold;
  • a worker that fails to start hands its held thread to the queue;
  • an OpenDocument manifest over 1 MB is left to the engine;
  • a thenable font list's then is read once.

One trade-off is deliberate: a document or workbook too large for the bounded early check isn't refused before the font wait. It waits its turn and the engine refuses it in a thread, so with a stalled font list it can get 503 instead of 501.

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

Tests and checks

  • Suite: with main merged, on Bun 1.4.4 (CI's version): 1811 pass, 0 fail. test/convert-service.test.ts has 54 tests, including the PDF, PDF: review, PDF: re-check, PDF: with PDF files opened, PDF: re-check after main and PDF: final check blocks. Each test for rounds 3 to 6 fails on the code before its fix.
  • Bun 1.3.6: it segfaults intermittently in worker-thread tests, on main as well, and fails 7 code-page tests that pass on 1.4.4.
  • 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.5: the built dist/cli.js converts decks on worker threads and refuses a Word file to PDF with the pointer to DOCX → PDF #48 (re-run after round 3).
  • Docker, by hand (checked before round 3):
    • 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

mkroemer and others added 5 commits November 25, 2026 19:05
…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>
@mkroemer
mkroemer marked this pull request as draft November 25, 2026 18:56
mkroemer and others added 5 commits December 6, 2026 20:03
…font wait

- Only a ZIP, an old Office file or RTF says what it holds: a PDF that names
  word/document.xml is a PDF (400), a deck carrying a document part is a deck.
- What the bytes refuse (a PDF, a document or workbook to PDF) is answered
  before the pool waits for the font list, and is not masked by it failing.
- Gotenberg's fields that change the PDF (watermarks, stamps, rotation,
  metadata, notes pages, tagged PDF, attachments, Factur-X) are refused with
  400, the files they upload are not taken as files to convert.
- The wait for the font list has the conversion's time limit on threads;
  a font list given as a failing promise is not left unhandled.
- PDFs waiting for fonts take the free threads, then the queue; with no
  workers there is no queue.
- /convert reads a field sent twice by its last value again; only
  Gotenberg's route reads the first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t wait, every field counted

- A file's kind before conversion is read as the engine reads it: an old
  Office file's document stream, an OpenDocument mimetype, an Office main
  part from the package's relationships and content types. A part more
  (word/ in a deck, custom/ppt/ in a document) no longer changes it.
- PDFs waiting for the font list hold their threads: a conversion that
  needs no fonts does not take one, and a waiter, once admitted, is not
  refused when the list comes.
- The font list is searched once for all waiters (no callbacks left on a
  stalled search); one that every waiter timed out on is searched again.
- A font list given as any thenable that fails is handled up front.
- Gotenberg's route: `metadata` of `{}` or `null` asks for nothing, a
  permission turned off is refused, `splitUnify` is refused, and every
  occurrence of a refused field counts, in the form or the query.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… search given up

- The early kind check runs on the server's thread, so it reads only a
  little: at most 4,096 ZIP entries, the parts that name the kind up to
  1 MB each, old Office files up to 32 MB. Past that the engine decides,
  in a thread with its time limit.
- A thread held for a PDF waiting for the font list is taken by no other
  conversion: not when queued after it, not when a thread is released;
  a waiter given up hands it to the queue.
- A PDF's own check comes before the font list reaches it, found or
  failed; a PDF file is refused at once, busy or not.
- A font search no one waits for any more (aborted, timed out) or that
  the converter closed on is given up, and never starts after close.
- A thenable font list is taken once, in the constructor.
- Gotenberg's route: every `merge` sent counts; metadata of only spaces
  is refused as Gotenberg's JSON reader does. Field values are collected
  without copying.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…start hands the queue on

- PDFs waiting for the font list hold the free threads in the order they
  came: the first takes its thread, a later one queues behind the work
  queued before it.
- A thread that cannot start for a PDF gives the thread it held to the
  queue.
- The early check leaves an OpenDocument manifest over 1 MB to the engine.
- A thenable font list's `then` is read once.
- README: the early check's limits, and what a larger file does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mkroemer
mkroemer marked this pull request as ready for review December 6, 2026 19:40

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.

1 participant