Skip to content

C++ API

Milosz edited this page Oct 19, 2026 · 11 revisions

C++ API

The public native exporter API is intentionally small and lives in include/rayomd/tiny_pdf.h.

#include "rayomd/tiny_pdf.h"

#include <string>

std::string pdfBytes;
TinyPdf::PdfOptions options;
options.style = TinyPdf::PdfStyle::Modern;
options.margin = TinyPdf::PdfMargin::Normal();
options.sourcePath = "input.md";
options.enableUrlImages = false;
options.embedSource = false;  // opt in only for exact source recovery
options.pageNumbers = false;  // "N / M" at the foot of every page (since 3.1)

TinyPdf::BuildResult result = TinyPdf::BuildPdf(markdownText, options, pdfBytes);
if (!result) {
    return static_cast<int>(result.error);
}
if (result.missingCharacters != 0) {
    // No available font could show these characters; they were drawn as their base
    // letter or '?'. The PDF is still complete.
}

Set sourcePath for file-based conversions so relative local images resolve next to the input file. By default, image targets must remain inside that source directory after canonicalization. Enable unsafe local paths or URL fetching only for trusted documents.

Since 3.1, a missing system font no longer fails the build: the document is drawn in the PDF standard fonts and missingCharacters counts what they could not show. BuildResult also reports pages and failedImages (images shown as their alt text).

Options since 3.2

These PdfOptions fields are new in 3.2.0:

Field Effect
pageSize PdfPageSize::A4() (default), A3(), A5(), Letter(), Legal(), or any width and height in points
compress FlateDecode page content, fonts and CMaps
pdfa PDF/A-3b; see Archival and Accessible PDFs
pdfua Tagged PDF/UA-1
highlightCode true (default) colours fenced code in a language GitHub knows
toc, tocDepth A table of contents first, listing tocDepth levels (1 to 6, default 3)
theme A PdfTheme; see Themes

Since 4.1, imageCacheDirectory (a UTF-8 path, empty by default) keeps re-encoded PNGs with transparency between processes, named by the SHA-256 of the PNG: a later export of the same image reads the kept streams instead of decoding it, with the same PDF bytes. The library keeps nothing unless the field is set; the CLI and the Windows app set their user cache directory (see CLI Reference). An entry that fails its checks is ignored and rewritten.

Since 4.7, fastImages (default false) re-encodes those PNGs in a faster mode of the encoder, for image streams a few percent larger and the same pixels (--fast-images); its streams are cached under keys and files of their own.

Since 5.2:

Field Effect
pageSize.autoHeight One long page per document for screens: pageSize.width sets the width, height is not read (--page-size=WIDTHxauto)
footnotes FootnotePlacement::End (default, after the text) or FootnotePlacement::Page (at the foot of the page of the first reference)
blackText Black text, links and code, light grey fills and diagrams, for black-and-white printers
hideTags Obsidian's dialect: #tags left out
vaultRoot Obsidian's dialect: the folder (UTF-8) embedded notes must lie in and are found in by name; empty: the document's folder
embedsAsLinks Obsidian's dialect: ![[Note]] stays a link instead of printing the note
backlinks BuildBookPdf: each file ends with "Linked from:" and links to the files linking to it

Note embeds read files only for builds with a sourcePath. The engine's src/core/book.h also has RayoMd::Book::ReadLinkedBook (what --follow-links uses) for front ends that link the engine's sources (rayomd::engine).

pdfa and pdfua need a TrueType or OpenType font: without one, BuildPdf fails with BuildError::PdfaFontUnavailable. A theme font that cannot be read (fontPath, or since 4.5 fontBoldPath, fontItalicPath, fontBoldItalicPath) fails with BuildError::ThemeFontUnavailable.

Books

BuildBookPdf(const Book&, const PdfOptions&, std::string&) makes one PDF of several files (see Books). A Book has a title, a directory that local images must lie in, and chapters: each a BookChapter with the file's markdown and path, a title (a part's, or the file's from the book's list), the depth its bookmarks move down, and part for a part's title page. sourcePath and embedSource do not apply to books.

Incremental builds (since 5.2.5)

BuildSession::Build(markdown, options, pdfBytes) returns what BuildPdf returns for the same arguments, byte for byte, with options.sourceMap filled the same way. Consecutive builds of an edited document lay out only the top-level blocks the edit changed or moved and take the others from the session's last build: in 700 copies of tester.md, a typed letter takes about 2.5 times fewer cycles than BuildPdf. An editor's preview is the use; export with BuildPdf.

TinyPdf::BuildSession preview;   // one per open document, used by one thread
TinyPdf::BuildResult result = preview.Build(markdownText, options, pdfBytes);
preview.Clear();                 // when the document closes: frees what it keeps
  • A session keeps about the page content of its last build (25 MB for 3,500 pages).
  • Any option change other than cancel and sourceMap, or another font, starts it afresh.
  • Documents with footnotes, PDF/UA and empty documents are laid out whole; image and diagram blocks are laid out again on every build (their files may have changed).
  • A cancelled or failed build leaves the last successful one in place.
  • Only the engine library (rayomd::engine) has it; the command lines are built without it.

The header in the source tag you build against is authoritative for that release.

Clone this wiki locally