Ferri PDF
An HTML and CSS to PDF renderer for print, written in pure Rust. No browser and no JavaScript.
It covers the CSS print stack: paged media, page-margin boxes and named pages, fragmentation, floats, flex, grid, tables, multi-column, text shaping and bidi, and SVG and raster images (PNG, JPEG, GIF, WebP, AVIF, HEIC).
Command line
Use - for stdin or stdout. Options:
| Option | Effect |
|---|---|
--image-dpi DPI |
Downsample raster images to this resolution |
--jpeg-quality 1..100 |
Re-encode opaque photos as JPEG at this quality |
--font-serif, --font-sans-serif, --font-monospace FAMILY |
Choose the family for a generic font |
--font-fallback FAMILY |
Family used for missing glyphs |
--system-fonts |
Let documents and the font options use the fonts installed on the system |
--font-dir DIR |
Also use the fonts in this folder and its subfolders (repeatable) |
--part INPUT.html |
Append another document to the same PDF (repeatable) |
--headings HEADINGS.json |
Write heading page numbers as JSON |
--trace |
Write the measured layout next to the PDF |
--allow-content-loss |
Write the PDF even when a resource limit left content out |
Invalid markup, unsupported CSS and missing resources never fail a render; they are recovered as the specifications say and printed as warnings. Input that exceeds a resource limit so that content is left out fails with the losses listed, unless --allow-content-loss is given.
Library
[]
= "0.1"
use ;
let options = RenderOptions ;
let output = render_html?;
write?;
Relative URLs resolve against base. HTTP(S) fetching is behind the default http feature. To control resource loading, pass your own resolver to render_with_resolver.
Besides @font-face web fonts, only the bundled Liberation and DejaVu fonts are used unless options.print.fonts.system_fonts is set or font_dirs lists folders; then font-family, local() and the generic and fallback preferences can also name the fonts installed there. Installed fonts are read only from the system font folders and the folders you give, never from a path the document chooses.
Recovered input is reported in output.diagnostics. When a resource limit leaves content out, rendering returns Error::ContentLost with those diagnostics; set content_loss: ContentLoss::Render to get the PDF anyway.
Python
=
The bindings live in python/; see python/README.md for options, multipart rendering and the fetch callback for loading remote resources yourself.
Development
Requires Rust 1.97 or newer.
To build and test the Python bindings: pip install maturin pytest && maturin develop && pytest python/tests.
The PDF checks in tests/*.py and the standards oracle in tests/oracle also need Python with Pillow and fontTools, Poppler and GNU FriBidi. See tests/oracle/README.md.
Licence
MIT. Forked dependencies in vendor/ and the SVG libraries inlined under src/svg/ keep their original licences, as do the bundled fonts in assets/fonts/ and the specification data in spec/; see vendor/FORKS.md.