Skip to content

Coming from pdfkit ​

This guide covers what differs from pdfkit:

  • Not provided
  • Refused where pdfkit writes anything
  • Different output

PdfDocument takes pdfkit 0.20's methods and options, and lays text out the way pdfkit does, line for line. A document written for pdfkit runs on vellum once its import changes:

diff
- import PDFDocument from 'pdfkit'
+ import { PdfDocument as PDFDocument } from '@c9up/vellum'

vellum's tests hold three such documents — a quote, an invoice carrying a Swiss QR-bill drawn by @c9up/prism/swissqrbill/pdf, and a three-page contract (examples/documents.ts) — and render them with both libraries: the same words on the same lines of the same pages, and under 1 % of the pixels apart. What differs is the glyphs' drawing: pdfkit leaves Helvetica to the PDF reader, vellum embeds Liberation Sans in its place, at Helvetica's widths and kerning.

The rest of this page lists every place where vellum behaves differently, each with its reason.

Not provided ​

pdfkitWhy
pattern()It takes a string of raw PDF operators, which would be written into the page unchecked
addNamedJavaScript()A script the document runs when it opens; the only JavaScript vellum writes is the Acrobat format calls it builds from a field's checked parameters
ref()A raw object written as given, past the escaping and the PDF/A and PDF/UA checks
Symbol and ZapfDingbatsNo free font carries their glyphs at their code points; a font that has the characters draws them
pdfVersion: '1.3', '1.7ext3'The engine writes 1.4 to 2.0; the default is 1.7
RC4 and AES-128 encryptionOnly AES-256 is offered (PDF 2.0): RC4 is broken, and PDF 2.0 deprecates both
Writing pages as they are madeThe document is kept until end(), which writes it at once; flushPages() closes pages to switchToPage and frees nothing
A default exportvellum has named exports only: import { PdfDocument } from '@c9up/vellum'

Refused where pdfkit writes anything ​

WhatvellumWhy
Link URLshttp, https, mailto and tel onlyA javascript: or file: link runs code or reads the reader's disk
addContentPath operators (m l c v y h re) onlyAny other operator bypasses the checks on text, images and state
annotateText, Highlight, Underline, StrikeOut, Square, Circle, FreeTextpdfkit writes any key it is given, JavaScript actions included
Form formatParameters checked, raw AA not takenpdfkit writes the parameters into the field's script as given
A colour it cannot readAn errorpdfkit ignores it and keeps drawing in the previous colour
A length it cannot read ('12 pt')An error, and a sign is honouredpdfkit reads it as 0
restore() without save()An errorpdfkit writes an unbalanced restore into the page
Images, fonts, SVGBounded before they are read; an SVG reads no file and no networkA file someone sent can claim gigabytes, or name a file on the server
Under PDF/A and PDF/UAAnnotations without an appearance and form fields refused; the document checked against its standard at end()pdfkit declares the standard and checks nothing

Different output ​

Whatvellumpdfkit
Standard fontsEmbedded (Liberation Sans, Serif and Mono at the standard metrics)Referenced, drawn by the reader
Text WinAnsi cannot encode, in a standard fontDrawn with the embedded fontDrawn as missing glyphs
Ÿ (U+0178) in a standard fontMeasured as ŸMeasured as ÿ
Right-to-left textLaid out right to left, mixed text in Unicode bidirectional order, read back in logical orderDrawn in the order given
Default alignRight for a right-to-left paragraphAlways left
A word wider than the lineCut between graphemesCut between UTF-16 units, which can split a character
boundsOfStringNever adds a page; a justified paragraph measures what it fillsCan add pages while measuring
quadraticCurveToA quadratic curvePDF's v operator, a cubic that bends differently
image with coverWhat overflows the box is cut offDrawn past the box
Graphics state over a page breakColours, opacities and stroke style carry overEach page restarts from the PDF defaults while pdfkit's state says otherwise
addPage(options)Merged with the document's optionsReplaces them: addPage({ margin: 10 }) drops the size
Outline itemsOpen at left, top on their pageOpen the whole page; left measured from the right edge
addNamedDestination(name, 'XYZ', left, top, zoom)The zoom is not written: the reader keeps its ownWritten
lineAnnotationIts rectangle is the line's boxThe end point taken as a width and a height
file() nameA path's base nameThe path as given, the server's directories with it
file() optionsLeft as they areWritten into: the name and relationship are added
Table borderColor: [255, 0, 0]One colourThree sides
Table border colours per sideKeptOne rectangle in the top colour when the four widths agree
rowStyles: 30Every row's heightIgnored
Form fieldsWritten with their appearance; fillForm draws the field's background and border under the valueNeedAppearances, left to the reader
Marked contentDoes not nest; the last paragraph's structure ends with text()Nests; the last paragraph's stays open
Content marked and never attached to a structure elementDrawn untaggedWritten with an identifier the structure does not hold
Heading elements (H1…H6) from structTypeTake their title from the textNo title