Export
Export is where "one layout for everything" pays off. The PDF writer doesn't re-flow anything: it draws the exact pages the editor shows, fragment by fragment.
PDF in progress
@nextgensoftwares/folio-export-pdf writes a PDF from a DocumentLayout:
import { exportPdf } from '@nextgensoftwares/folio-export-pdf';
const bytes = await exportPdf(editor.layout, {
fonts: async (font) => fontBytesFor(font.family, font.weight, font.style), // bytes of one face
shaper: fonts, // the FontEngine layout used (same face selection)
images: async (src) => ({ data: await fetchBytes(src), type: 'png' }),
title: 'Physics I',
onProgress: (done, total) => setProgress(done / total),
});- Every fragment is drawn at its laid-out position with HarfBuzz-shaped glyphs (so Arabic shaping and ligatures match the screen) in embedded, subset fonts with a ToUnicode map, so text stays selectable and searchable.
- Pages are streamed: each page's lazy fragments are read, drawn, compressed and dropped before the next one, and the writer yields to the event loop every 25 pages (
yieldEvery) so a browser UI stays responsive. Long exports can be cancelled (signal). - Links become PDF link annotations (http, https, mailto, tel and ftp only).
- Math is drawn from a host typesetter (
math), images fromimages(default:data:URLs), SVG through a host rasterizer (rasterizeSvg).customfragments are drawn bypainterskeyed by fragment type. - In the browser,
pdfExportPlugin({ faces })adds an Export ▸ PDF toolbar entry that exports the current layout and downloads it.
See Exporters for the plugin API and @nextgensoftwares/folio-export-pdf for every option.
DOCX in progress
@nextgensoftwares/folio-export-docx (DOCX export from Folio documents) is being built. DOCX is an export of the model, not of the pages: Word re-flows the document itself, so the mapping targets Word's structure (styles, paragraph properties such as keepNext / keepLines / pageBreakBefore, numbering, tables, headers and footers).
@nextgensoftwares/folio-docx is the Word importer (see Import). The older @nextgensoftwares/folio-pdf package is a stub whose functions throw "not implemented".
Floating media
Media placement (wrap, float, alignment, distT/B/L/R, offsetX/Y, anchor) is read with the same placementOf the layout uses, so both exports agree with the editor:
- PDF draws the laid-out pages, so floats come for free: text lines are already narrowed beside square floats, and each page lists
behindobjects first andfrontobjects last, so drawing in order stacks them correctly. - DOCX writes Word anchors (
wp:anchor): square →wrapSquarewith text on the free side (wrapText="right"for a left float), top-and-bottom →wrapTopAndBottom, behind / in front →wrapNonewithbehindDoc1 / 0. Square and top-and-bottom are aligned left/center/right in the column; behind/front useposOffset(aligned x +offsetX; y =offsetYfrom the paragraph, or from the page margin whenanchor: 'margin'). Distances map todistT/B/L/Rin EMU. Folio's floated object is its own block, so the anchor sits in an empty 1pt paragraph (keepNext) right before the text it wraps: Word wraps the following paragraphs the same way and keeps the object with them across page breaks. Inline media stayswp:inline; floats nested in lists, quotes or table cells stay inline, as in layout. Video/audio/file cards export through their print fallback and are not anchored yet.
Folio in exported files
The "Made with Folio" credit stays in Folio's apps (app bar, status bar and pages on screen); exported files carry none on their pages. Folio is named in their metadata only: PDFs name Folio as their Producer (Folio (https://folio.nextgen-softwares.com)) and the host app as their Creator (creator, default "Folio"); Word files and PowerPoint decks name Folio as the Application (docProps/app.xml). Title and author stay yours.
Files exported by Folio before 0.57 carried the credit on every page or slide. Folio's Word and PowerPoint importers leave it out, and re-saving such a deck drops it from its slides. The credit option of exportPdf, exportFlattenedPdf, exportDocx, exportPptx, exportSlideImage, pptxFile and the export plugins is still accepted, and ignored.
Plugins and export
Plugins contribute export mappings through FolioPlugin.exporters, keyed by exporter name and then node or fragment type. They are opaque to the core; each exporter defines what it expects. For example @nextgensoftwares/folio-plugin-media provides print fallbacks for video, audio and file cards (a poster or title, a detail line and the link), because a static page can't play a video.
Server-side export
Layout is DOM-free, so the whole pipeline (fonts, layout, PDF) runs in Node. Load the document's fonts with the same LoadFamily the editor and reader use (ensureDocumentFonts(loader, doc, theme), see Fonts), lay out with the same theme, and pass the layout to exportPdf with fonts: engineFontBytes(engine) and fontReport (substituted and missing families become warnings). exportDocx takes the same fonts and fontReport. Only math and SVG need host typesetting / rasterizing, which you can provide with any Node library.
A server export kit
Build the expensive parts once per process (the font engine, the loader, the composed plugins) and share them between Word and PDF exports, so every export gets the same fonts, renderers and plugin exporters without passing them by hand each time:
import { composePlugins, type FolioPlugin } from '@nextgensoftwares/folio-editor';
import { FontEngine, createFontLoader, ensureDocumentFonts, engineFontBytes, type LoadFamily } from '@nextgensoftwares/folio-fonts';
import { layoutDocument, mergeTheme, joinDocuments } from '@nextgensoftwares/folio-layout';
import { standardSchema, type FolioDocument } from '@nextgensoftwares/folio-model';
import { exportDocx } from '@nextgensoftwares/folio-export-docx';
import { exportPdf } from '@nextgensoftwares/folio-export-pdf';
export async function createExportKit(loadFamily: LoadFamily, plugins: FolioPlugin[]) {
const engine = await FontEngine.create({ fallback: ['Noto Sans Arabic', 'Geist'] });
const fonts = createFontLoader({ engine, loadFamily });
const composed = composePlugins(standardSchema, plugins); // the editor's plugins, in its order
const theme = mergeTheme();
const layout = composed.wrapLayout((doc: FolioDocument) => layoutDocument(doc, { measurer: engine, theme, renderers: composed.renderers }));
const common = { fonts: engineFontBytes(engine) };
return {
async docx(doc: FolioDocument, o: { fetchImage?: (src: string) => Promise<Uint8Array | null> } = {}) {
await ensureDocumentFonts(fonts, doc, theme);
return exportDocx(doc, { ...common, ...o, theme, exporters: composed.exporters.docx ?? {} });
},
async pdf(doc: FolioDocument) {
await ensureDocumentFonts(fonts, doc, theme);
return exportPdf(layout(doc), { ...common, shaper: engine, painters: composed.exporters.pdf ?? {} });
},
};
}A book stored as one document per chapter exports as one file with joinDocuments(chapters.map((doc, i) => ({ doc, id: ids[i] })), { cover }) (@nextgensoftwares/folio-layout): every chapter becomes a "next page" section keeping its own header and footer, page numbers restarting at 1, and the sections a chapter defines inside itself come along under <chapter id>:<section id>. When no chapter has headers or footers, the joined document has none either, so its pages hold what each chapter's pages hold when laid out alone; a chapter without them among chapters with them shows empty ones. exportDocx streams the body (bounded memory) by default, sections included.
Math in Node
@nextgensoftwares/folio-math typesets KaTeX without a DOM: its measurer gives the boxes a browser measures (same line and page breaks), and its drawings are vector (KaTeX's fonts embedded as subsets, radicals and delimiters as paths):
import { katexMathMeasurer, fallbackText } from '@nextgensoftwares/folio-math';
import { katexMathNode } from '@nextgensoftwares/folio-math/node';
const fallback = fallbackText(engine, 'Noto Sans Arabic'); // Arabic in \text{}
const layout = layoutDocument(doc, { measurer: engine, theme, math: katexMathMeasurer({ fallback }) });
const pdf = await exportPdf(layout, {
shaper: engine,
fonts,
math: katexMathNode({ fallback, fallbackFont: () => ({ family: 'Noto Sans Arabic', data: notoArabic }) }),
});katexMathNode reads KaTeX's fonts from the installed katex package; in a browser or worker use katexMath({ fonts: katexFontsFromUrl('/fonts/katex/'), fontkit }). Server-side page images (@nextgensoftwares/folio-render-node) use KaTeX by default.