The fabricjs-document-engine/pdf entry needs the optional packages jspdf and svg2pdf.js, and loads them only when a PDF is made. How the exportPdf options fit together is explained in the PDF export guide.
exportPdf
function exportPdf(sources: PdfSource | readonly PdfSource[], options?: PdfExportOptions): Promise<PdfExportResult>Makes a PDF with one page per source: an engine, a Fabric canvas or a saved document. Needs jspdf, and svg2pdf.js for vector and hybrid mode.
function layoutPage(content: {
width: number;
height: number;
}, options?: PageLayoutOptions): PageLayoutWhere a canvas of { width, height } pixels lands on a page, in points.
The exportPdf options
| Name | Type | Default | Description |
|---|---|---|---|
| mode | PdfMode | "hybrid" | vector draws everything as PDF vectors and text, raster draws each page as one picture, and hybrid draws only what PDF vectors cannot show as a picture of that object. |
| dpi | number | 300 | Resolution of everything drawn as a picture. |
| fonts | PdfFont[] | [] | TrueType (.ttf) files for the families the canvas uses. Text in these families stays real, selectable text. |
| missingFonts | "rasterize" | "substitute" | "rasterize" in hybrid mode | Text in a family with no file: rasterize draws it as a picture, substitute uses the closest built-in PDF font. |
| background | ExportBackground | "keep" | As for export. |
| metadata | { title?: string; author?: string; subject?: string; keywords?: string; creator?: string; } | none | { title, author, subject, keywords, creator }. |
| signal | AbortSignal | none | Cancels with EXPORT_ABORTED. |
| customObjects | CustomObjectDefinition[] | [] | Used for saved documents passed as sources. |
| assets | AssetOptions | {} | Used for saved documents passed as sources. |
| limits | ContentLimits | defaults | Used for saved documents passed as sources. |
| page | PdfPageSize | "canvas" | A3, A4, A5, Letter, Legal, Tabloid, canvas for a page the size of the canvas, or [width, height] in points.Inherited from PageLayoutOptions. |
| orientation | PdfOrientation | "auto" | portrait, landscape, or auto to turn named pages to match the drawing.Inherited from PageLayoutOptions. |
| margin | number | Partial<PdfMargin> | 0 | Points, as one number or { top, right, bottom, left }.Inherited from PageLayoutOptions. |
| fit | PdfFit | "contain" | contain shows all of the canvas, cover fills the box and clips, none prints at real size (96 pixels to the inch).Inherited from PageLayoutOptions. |
Page size, orientation, margin and fit come from PageLayoutOptions:
| Name | Type | Default | Description |
|---|---|---|---|
| page | PdfPageSize | "canvas" | A3, A4, A5, Letter, Legal, Tabloid, canvas for a page the size of the canvas, or [width, height] in points. |
| orientation | PdfOrientation | "auto" | portrait, landscape, or auto to turn named pages to match the drawing. |
| margin | number | Partial<PdfMargin> | 0 | Points, as one number or { top, right, bottom, left }. |
| fit | PdfFit | "contain" | contain shows all of the canvas, cover fills the box and clips, none prints at real size (96 pixels to the inch). |
Fonts
Every entry of fonts is a PdfFont. Fonts must be TrueType (.ttf) files:
| Name | Type | Default | Description |
|---|---|---|---|
| family* | string | — | The family exactly as the canvas uses it. |
| source* | string | ArrayBuffer | Uint8Array | Blob | — | The .ttf file: its URL, or its bytes. |
| weight | "normal" | "bold" | number | "normal" | normal, bold or a number. Weights from 600 count as bold. |
| style | PdfFontStyle | "normal" | normal or italic. |
The result
| Name | Type | Default | Description |
|---|---|---|---|
| blob* | Blob | — | The PDF file. |
| pageCount* | number | — | How many pages it has. |
| warnings* | PdfWarning[] | — | PDF_RASTERIZED, PDF_UNSUPPORTED, PDF_FONT_SUBSTITUTED or IMAGE_NOT_EMBEDDED, each with the page and object ids. |
Each warning is { code, message, page, objectIds, family?, url? }, where code is PDF_RASTERIZED, PDF_UNSUPPORTED, PDF_FONT_SUBSTITUTED or IMAGE_NOT_EMBEDDED. The exportPdf options and errors such as PDF_UNAVAILABLE and PDF_FAILED are listed with the error codes.