Skip to the content
v1.0.1-beta.17

PDF export

Premium add-on. This page covers a premium add-on, included in Scriviamo Pro and Pro + Collaboration. See pricing

The pdf add-on exports documents as PDF, laid out on pages with a size, margins, a header and a footer, and page numbers.

import "scriviamo/dist/addons/scriviamo.pdf.js"

The Export PDF tool asks for the page setup, then opens the browser's print dialog with the document laid out on pages, where the user chooses Save as PDF. Browsers make the PDF themselves, so nothing is sent to a server, and text stays text: searchable, selectable and sharp at any size.

Web fonts the document uses load before the print dialog opens, so the PDF uses them.

Documents with form fields are the exception: browsers turn form fields into drawings when they print, so by default these are made into PDFs with fillable fields instead; see Fillable forms.

Page setup

SettingMeaning
pageSizeA3, A4, A5, Letter, Legal or Tabloid, or any CSS page size, such as "210mm 297mm"
orientation"portrait" or "landscape"
marginThe margin on every side, in millimetres
titleThe PDF's title, which browsers also suggest as its file name; "" uses the first heading
header, footerText at the top and bottom of each page. {title}, {date}, {page} and {pages} are filled in. The footer starts as {page} / {pages}.
printBackgroundPrints background colors and images, as the editor shows them
fillableFormsFor a document with form fields, makes a PDF with fillable fields instead of opening the print dialog. On by default; the dialog offers it only when the document has fields

Each editor remembers the setup last chosen for it. New editors start from PDFExport.defaults, which you can change:

import {PDFExport} from "scriviamo/dist/addons/scriviamo.pdf.js"

PDFExport.defaults = {...PDFExport.defaults, pageSize: "Letter", footer: "Page {page} of {pages}"}

Headers, footers and page numbers use CSS page margin boxes, which Chrome and Edge print. Firefox and Safari print the pages, with their size and margins, without them.

Headings stay with the text after them, and code, figures, tables, quotes, formulas and diagrams aren't split across pages where they fit on one. Page breaks from the blocks add-on start new pages.

In code

MemberDoes
PDFExport.showDialog(editor)Asks for the page setup and prints, or saves a fillable PDF; resolves false when cancelled or when the fillable PDF couldn't be made. Raises pdfExported with {setup, fillable}.
PDFExport.print(editor, setup)Prints with a setup, or the editor's own, without asking
PDFExport.html(editor, setup)The document as HTML laid out for printing, such as for a server that makes PDFs with a headless browser
PDFExport.setupFor(editor), setSetup(editor, setup)The editor's remembered setup
PDFExport.makesFillable(editor, setup)Whether exporting with that setup makes a fillable PDF rather than printing
PDFExport.saveFillable(editor, setup)Makes the fillable PDF and saves it as a file named after its title

The add-on also adds a "pdf" exporter to every editor: editor.exporters.item("pdf").exportToFile(editor, "report") opens the print dialog with "report" as the suggested title, or saves a fillable "report.pdf" for a document with form fields.

Fillable forms

Text fields, text areas, checkboxes, radio buttons and dropdowns from the forms add-on become real PDF form fields, which PDF readers let people fill in, save and print. Each starts with the field's value in the document, and keeps its name, so a filled-in PDF can be read back by name. Required fields stay required, and disabled or read-only fields can't be changed. Labels, buttons and everything else print as they look.

These PDFs are made in the browser rather than through the print dialog: each page is a picture of the document, laid out with the page setup, with the fields placed over it. So:

  • Text on the pages can't be selected or searched, and files are larger than printed PDFs.
  • Blocks aren't split across pages where they fit on one; a block taller than a page continues on the next. Page breaks start new pages.
  • The header and footer are drawn on every page, with {page} and {pages} filled in, in every browser.
  • Values in fields use the PDF standard font, which writes Western European text. A value it can't write, such as Arabic or Chinese, is left empty, and people can type it in a reader that has the font.
  • Pictures from other sites appear only if those sites allow it (CORS); others are left out.

The PDF is written with pdf-lib and the pages are drawn with html-to-image. Both load from jsDelivr the first time a fillable PDF is made. To load them from your own server, or to bundle them, use FillablePDF:

import {FillablePDF} from "scriviamo/dist/addons/scriviamo.pdf.js"

FillablePDF.pdfLibURL = "/vendor/pdf-lib.esm.min.js"
FillablePDF.htmlToImageURL = "/vendor/html-to-image.js"

// Or, with the packages installed:
import * as pdfLib from "pdf-lib"
import * as htmlToImage from "html-to-image"
FillablePDF.use({pdfLib, htmlToImage})
MemberDoes
FillablePDF.make(editor, setup)Makes the PDF and resolves with its bytes, a Uint8Array, such as to upload it rather than save it
FillablePDF.hasFields(editor)Whether the document has fields that would become PDF fields
FillablePDF.download(bytes, fileName)Saves bytes as a file
FillablePDF.pixelRatioPicture pixels per CSS pixel; 2 by default. Higher is sharper, and larger
FillablePDF.imageQualityThe JPEG quality of the pages, from 0 to 1; 0.92 by default

To turn fillable PDFs off, so documents with forms print like any other:

PDFExport.defaults = {...PDFExport.defaults, fillableForms: false}

Making PDFs on a server

For PDFs made without a print dialog, such as emailed reports, send PDFExport.html(editor) to a server and print it with a headless browser. With Playwright:

await page.setContent(html)
await page.pdf({path: "report.pdf", preferCSSPageSize: true})

preferCSSPageSize keeps the page size and margins from the setup, and the header, footer and page numbers print as they do from the dialog.