@formepdf/svelte
v0.25.0
Published
Svelte-to-JSON serializer for Forme PDF engine
Readme
@formepdf/svelte
Svelte components for Forme PDF generation.
Install
npm install @formepdf/svelte @formepdf/coreRequires Svelte 5 (^5.30.0) as a peer dependency.
@formepdf/core is an optional peer: it is only needed to render PDF bytes locally (renderDocument, the preview helper).
Serializing for the hosted API works without it.
Usage
Templates are ordinary .svelte files - {#each}, {#if}, snippets, and text interpolation all work.
Invoice.svelte:
<script lang="ts">
import { Document, Page, Text } from '@formepdf/svelte';
let { name = 'World' }: { name?: string } = $props();
</script>
<Document>
<Page size="Letter" margin={54}>
<Text style={{ fontSize: 24, fontWeight: 700, marginBottom: 12 }}>Hello {name}</Text>
<Text style={{ fontSize: 10, lineHeight: 1.6 }}>Page breaks that actually work.</Text>
</Page>
</Document>Render it in a SvelteKit endpoint:
import { renderDocument } from '@formepdf/svelte';
import Invoice from '$lib/Invoice.svelte';
export async function GET() {
const pdf = await renderDocument(Invoice, { props: { name: 'Forme' } });
return new Response(pdf, { headers: { 'Content-Type': 'application/pdf' } });
}Components
The same components with the same props as @formepdf/react.
Layout
Document- Root container (fonts, metadata, tagged PDF, PDF/A, certification)Page- A page with size, margins, and orientationView- Flex container (like div)Text- Text content with font stylingH1-H6- Semantic headings with per-level default styling and tagged-PDF rolesStrong,Em,Code,Link- Inline formatting insideTextand headingsOrderedList,UnorderedList,ListItem- Numbered and bulleted listsImage- JPEG, PNG, and WebP imagesFixed- Fixed headers and footersPageBreak- Explicit page break
Tables
Table,Row,Cell- Tables with automatic header repetition across pages
Graphics
Svg- SVG rendering viacontentstringQrCode- Vector QR codesBarcode- 1D barcodes (Code 128, Code 39, EAN-13, EAN-8, Codabar)Canvas- Arbitrary vector drawing via callback API (thedrawcallback runs during serialization and must be synchronous and pure)Watermark- Rotated text behind page content
Charts
BarChart- Vertical bar charts with grouped seriesLineChart- Line charts with multiple seriesPieChart- Pie and donut chartsAreaChart- Filled area chartsDotPlot- Dot plot with grouped data points
Form Fields
TextField- Text input fieldCheckbox- Checkbox with labelDropdown- Select dropdown with optionsRadioButton- Radio button with group support
API
serialize(Template, { props })- template to Forme document objectrender(Template, { props })- template to Forme JSON stringrenderToObject(Template, { props })- alias ofserializemirroring the react APIrenderDocument(Template, { props, ...renderOptions })- template to PDF bytes (requires@formepdf/core); forwards core render options likeembedDataandflattenFormsrenderDocumentWithLayout(Template, options)- PDF bytes plus layout info for overlaysformePreview(Template, options)(from@formepdf/svelte/preview) - GET handler for a SvelteKit catch-all route serving the live preview UIFont.register(),StyleSheet.create()- identical to the react adapterPAGE_NUMBER,TOTAL_PAGES- page-number placeholder constants ({{pageNumber}}cannot be typed literally in Svelte markup)
Compiled templates (forme build --template) are TSX-only today.
Docs
Full documentation at docs.formepdf.com
