Fabric.js Document Engine

Search documentation

Find a page or section

中文

Everything in fabricjs-document-engine/pdf.

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): PageLayout

Where a canvas of { width, height } pixels lands on a page, in points.

The exportPdf options

NameTypeDefaultDescription
modePdfMode"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.
dpinumber300Resolution of everything drawn as a picture.
fontsPdfFont[][]TrueType (.ttf) files for the families the canvas uses. Text in these families stays real, selectable text.
missingFonts"rasterize" | "substitute""rasterize" in hybrid modeText in a family with no file: rasterize draws it as a picture, substitute uses the closest built-in PDF font.
backgroundExportBackground"keep"As for export.
metadata{ title?: string; author?: string; subject?: string; keywords?: string; creator?: string; }none{ title, author, subject, keywords, creator }.
signalAbortSignalnoneCancels with EXPORT_ABORTED.
customObjectsCustomObjectDefinition[][]Used for saved documents passed as sources.
assetsAssetOptions{}Used for saved documents passed as sources.
limitsContentLimitsdefaultsUsed for saved documents passed as sources.
pagePdfPageSize"canvas"A3, A4, A5, Letter, Legal, Tabloid, canvas for a page the size of the canvas, or [width, height] in points.Inherited from PageLayoutOptions.
orientationPdfOrientation"auto"portrait, landscape, or auto to turn named pages to match the drawing.Inherited from PageLayoutOptions.
marginnumber | Partial<PdfMargin>0Points, as one number or { top, right, bottom, left }.Inherited from PageLayoutOptions.
fitPdfFit"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:

NameTypeDefaultDescription
pagePdfPageSize"canvas"A3, A4, A5, Letter, Legal, Tabloid, canvas for a page the size of the canvas, or [width, height] in points.
orientationPdfOrientation"auto"portrait, landscape, or auto to turn named pages to match the drawing.
marginnumber | Partial<PdfMargin>0Points, as one number or { top, right, bottom, left }.
fitPdfFit"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:

NameTypeDefaultDescription
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.
stylePdfFontStyle"normal"normal or italic.

The result

NameTypeDefaultDescription
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.

exportPdf options | Fabric.js Document Engine