DeveloperExport System Architecture

Export System Architecture

This page describes how entity and document export works today. The export code lives in the open-core studiobrain-core repo (the sb-server crate and the Next.js frontend); StudioBrain Cloud consumes it as a pinned dependency. Per the repo boundary rules, change it through a core PR, not from the cloud repo.

The earlier Python implementation (FastAPI, Playwright, Jinja2, DocumentExporter, .html.jinja layout files) was removed on 2026-04-12 (SBAI-2303). It is no longer documented here; do not look for those classes or templates.

Implementation

Export is implemented in Rust in sb-server (core repo):

FormatHandlerEngine
HTMLexport_html in crates/sb-server/src/routes/data_management.rsBuilt-in layout rendering (layout, theme query params)
PDFexport_pdf in data_management.rs; build_pdf_document() / build_pdf_for_document()typst (typst-as-lib + typst-pdf), markdown translated with pulldown-cmark
DOCXexport_docx in data_management.rs; crates/sb-server/src/routes/docx_export.rs (build_docx_for_entity() / build_docx_for_document())rdocx + pulldown-cmark
Markdownexport_markdown in data_management.rsEntity markdown as stored

The pdf Cargo feature enables the full Typst renderer. Without it, the native route uses a basic PDF fallback for ordinary entities and a placeholder for document-type output. DOCX rendering requires the docx feature, and gdrive enables it. Neither pdf nor docx is included in the default feature set.

HTTP routes

Registered in data_management.rs:

MethodPathNotes
GET/api/export-layouts (alias /api/export/layouts)Lists available HTML layouts
GET/api/export/:entity_type/:entity_id/formats (also /api/:entity_type/:entity_id/formats)Formats available for an entity
POST/api/export/:entity_type/:entity_id/htmllayout (default gdd_standard), theme (default default); returns an attachment
POST/api/export/:entity_type/:entity_id/pdfinclude_toc, include_cover (default false)
POST/api/export/:entity_type/:entity_id/docxinclude_toc, include_cover (default false)
POST/api/export/:entity_type/:entity_id/markdownMarkdown attachment (text/markdown; charset=utf-8)
POST/api/export, GET /api/export/download/:filenameBulk export
POST/api/entities/:id/export/gdriveGoogle Drive export (gdrive_export.rs, gdrive feature)

The same handlers are also mounted without the /export prefix as /api/:entity_type/:entity_id/html|pdf|docx.

⚠️

SaaS Worker coverage: sb-server-worker (api.studiobrain.ai) currently routes only export-layouts, export/layouts and the formats lookup locally. Per-entity HTML, PDF, and DOCX render routes return the hosted Worker’s route-level 404. These renderers run on native sb-server (self-hosted, desktop, or LAN web UI). Verify against packages/cloudflare-worker/sb-server-worker/src/router.rs before documenting a format as available on api.studiobrain.ai.

Frontend export menu (document options)

ExportMenu (frontend/src/components/ExportMenu.tsx in core) takes an optional templateCategory prop. When it is "document", the menu shows two checkboxes:

  • Include Table of Contents (includeToc; defaults to checked in the menu)
  • Include Cover Page (includeCover)

DynamicEntityEditPage derives the category with getEntityTypeConfig(entityType)?.category and passes it down through EntityEditTabs to ExportMenu. Non-document entity types never get "document", so the toggles stay hidden.

The export calls in frontend/src/lib/api-client.ts append include_toc=true / include_cover=true to the PDF and DOCX requests only when the toggles are on; the server treats a missing parameter as false.

Adding a format or layout

  • A new output format means a new handler and route in data_management.rs (and, for heavy dependencies, an optional Cargo feature in crates/sb-server/Cargo.toml following the pdf / docx pattern). Keep new dependencies compatible with the wasm32 build if the route is meant to run in the Worker; otherwise it stays origin-only.
  • Layout names for HTML export come from GET /api/export-layouts; check list_export_layouts and crates/sb-server/src/portable/data_management.rs for the current set.
  • Add tests next to the handler. docx_export.rs has unit tests for build_docx_for_entity and build_docx_for_document, including the TOC and cover options.

Verse inventory export

This page previously documented a Verse converter (services::verse_converter, /api/items/import/verse, /api/items/export/verse). Those symbols are not present in the current core or cloud code, so that section was removed. Do not rely on those endpoints.

See Also