Spindrift PDF renderer
spindrift is a from-scratch Rust HTML/CSS to PDF renderer inspired by
WeasyPrint. It exposes a crate API and a CLI, parses HTML with html5ever,
parses explicit/XML-declared XML input with xml5ever, parses CSS syntax with
cssparser, matches selectors with Servo's selectors crate, performs
paged layout, and emits deterministic PDF files with Parley-measured text,
embedded system fonts, and Type 0 Unicode text resources.
Examples
Convert an HTML file to a PDF file:
CLI
The invocation mirrors WeasyPrint's basic syntax:
Once installed as a binary:
Enable debug logs, including PDF generation timing, with:
Print host information useful in bug reports without rendering a document:
Use --full-fonts to embed complete font programs when PDF embedding permits
it, matching WeasyPrint's opt-out of font subsetting:
Use --pdf-profile to select Spindrift's PDF writer policy. The default is
pdf; pass --pdf-profile pdf/a-1b to opt into the current PDF/A-oriented
writer behavior. Supported values are pdf and pdf/a-1b. These profiles select the implemented header,
metadata, and font-planning behavior, but do not yet guarantee PDF/A
conformance. --pdf-variant and --pdf-type remain aliases.
Use --uncompressed-pdf to omit /FlateDecode from every generated PDF
stream. This is primarily useful when inspecting PDF syntax while debugging;
it substantially increases output size:
Spindrift follows HTTP redirects by default and treats every external resource
failure as fatal. Use --no-http-redirects to reject HTTP redirects, or
--allow-fetch-errors to skip failed optional subresources and continue:
External stylesheets are supported:
Command-line stylesheets are user-origin stylesheets, so they provide print
preferences without overriding an explicit author @page declaration. The
library API has the same behavior when a stylesheet is marked with
Css::with_user_origin(); Html::with_stylesheet() remains the author-origin
API for document styles.
Set document page geometry in CSS rather than command-line or crate options:
Generate shell completions with:
Library
use ;
let mut pdf = Vecnew;
from_string
.write_pdf
.await?;
# Ok::
Rendering an HTML file from Rust uses the same library path:
use ;
use File;
let mut output = create?;
from_file
.await?
.write_pdf
.await?;
# Ok::
Benchmarking
cargo bench has a few useful benchmarks.
When profiling using instruments, you may need to add an entitlement to allow Instruments to inspect the process:
<!-- debug.entitlements -->
com.apple.security.get-task-allow
Web Platform Tests
There is an external runner for web platform tests in ../spindrift-wpt; there is a checkout of the tests there at ../spindrift-wpt/third_party/wpt.