Fabric.js Document Engine

搜索文档

查找页面或章节

EN

fabricjs-document-engine/pdf 里的全部内容。

fabricjs-document-engine/pdf 入口需要可选的 jspdf 和 svg2pdf.js 两个包,只有生成 PDF 时才会加载。exportPdf 配置项怎样配合使用,见 PDF 导出指南。

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.

exportPdf 配置项

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.

页面大小、方向、页边距和缩放方式来自 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 的每一项都是一个 PdfFont。字体必须是 TrueType(.ttf)文件:

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.

返回结果

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.

每条警告都是 { code, message, page, objectIds, family?, url? },code 为 PDF_RASTERIZED、PDF_UNSUPPORTED、PDF_FONT_SUBSTITUTED 或 IMAGE_NOT_EMBEDDED。PDF_UNAVAILABLE、PDF_FAILED 等错误和 exportPdf 配置项相关的说明,列在错误码里。

exportPdf 配置项 | Fabric.js Document Engine