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):
| Format | Handler | Engine |
|---|---|---|
| HTML | export_html in crates/sb-server/src/routes/data_management.rs | Built-in layout rendering (layout, theme query params) |
export_pdf in data_management.rs; build_pdf_document() / build_pdf_for_document() | typst (typst-as-lib + typst-pdf), markdown translated with pulldown-cmark | |
| DOCX | export_docx in data_management.rs; crates/sb-server/src/routes/docx_export.rs (build_docx_for_entity() / build_docx_for_document()) | rdocx + pulldown-cmark |
| Markdown | export_markdown in data_management.rs | Entity 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:
| Method | Path | Notes |
|---|---|---|
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/html | layout (default gdd_standard), theme (default default); returns an attachment |
POST | /api/export/:entity_type/:entity_id/pdf | include_toc, include_cover (default false) |
POST | /api/export/:entity_type/:entity_id/docx | include_toc, include_cover (default false) |
POST | /api/export/:entity_type/:entity_id/markdown | Markdown attachment (text/markdown; charset=utf-8) |
POST | /api/export, GET /api/export/download/:filename | Bulk export |
POST | /api/entities/:id/export/gdrive | Google 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 incrates/sb-server/Cargo.tomlfollowing thepdf/docxpattern). 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; checklist_export_layoutsandcrates/sb-server/src/portable/data_management.rsfor the current set. - Add tests next to the handler.
docx_export.rshas unit tests forbuild_docx_for_entityandbuild_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.