printpdf
printpdf is a Rust library for creating, reading, writing and rendering PDF documents.
Website | Crates.io | Documentation | Donate
[!NOTE] HTML-to-PDF rendering (
PdfDocument::from_html,htmlfeature) works for basic layouts - tables, page breaks, footnotes - but is still evolving. For pixel-exact control, position PDF elements manually with ops.
Features
- Pages, Bookmarks, link annotations (read / write)
- Layers (read / write)
- Graphics: lines, shapes, bezier curves, SVG content (read / write)
- Gradients: axial and radial (read / write), also from CSS
linear-gradientandradial-gradientin HTML - Images encoding / decoding (read support: experimental / write supported with
image) - Embedded fonts, Unicode support (read support: experimental / write)
- Minifying file size (auto-subsetting fonts)
- Advanced graphics - overprint, blending, etc.
- Advanced typography - character / word scaling and spacing, superscript, subscript, etc.
- Embedding SVGs (uses
svg2pdfcrate internally)
Experimental features:
- Rendering PDF pages to SVG, one standalone SVG file per page
resvgcan render the SVG to an image
- Extracting text from PDF pages, with Unicode decoding, line breaks and text positions
- Preflight (
preflight_bytes,PdfDocument::preflight,ppdf check) reports structural problems and a subset of the PDF/A-1b, PDF/A-2b, PDF/X-1a and PDF/X-3 rules- Each finding has a severity, a location and a rule id
- The preflight is not a certification and does not replace veraPDF or Acrobat Preflight
- PDF/A-1b, PDF/A-2b and PDF/X-1a:2001 output (
PdfConformance) writes the XMP identification, an output intent with a color profile and the header version of the standard- Embedded fonts are required, and printpdf has no font files for the 14 built-in fonts
- Register a replacement for each built-in font the document uses with
PdfDocument::replace_builtin_font - Or turn on the feature
include_base_fonts, which embeds the Foxit fonts of PDFium (BSD-3-Clause) - PDF/X files and CMYK-only PDF/A files need a CMYK profile as output intent, and printpdf contains none
- Set the profile in
PdfDocumentInfo::output_intent. The printing company supplies it, and eci.org offers the ECI profiles - Without the profile the save reports an error
- Forbidden content, such as transparency in PDF/A-1, layers and mixed RGB and CMYK, is written as it is and the save warns
- The files pass printpdf's own preflight, the PDF/A-1b and PDF/A-2b test documents pass veraPDF 1.30, and the PDF/X files are not checked by veraPDF
- XHTML layouting for simple page layout, using
azul-layoutandxmlparser- Good enough for basic page layouts, book rendering, reports and forms, with automatic page breaks
NOT supported are:
- Patterns (tiling and shading patterns as fill or stroke colors)
- File attachments
- Open Prepress Interface
- Halftoning images
- Full validation against PDF standards (only the subset of rules listed under preflight is checked)
- Embedded Javascript
See the WASM32 demo live at: https://fschutt.github.io/printpdf
Writing PDF
Basic example
use *;
Graphics
use *;
Images
- Images only get compressed in release mode. You might get huge PDFs (6 or more MB) in debug mode.
- To make this process faster, use
BufReaderinstead of directly reading from the file. - Scaling of images is implicitly done to fit one pixel = one dot at 300 dpi.
use *;
Fonts
use *;
Tables, HTML
For creating tables, etc. printpdf uses a basic layout system, similar to wkhtmltopdf (although more limited in terms of features). It's good enough for basic page layouting, book rendering and reports / forms / etc. Includes automatic page-breaking.
Since printpdf supports WASM, there is an interactive demo at https://fschutt.github.io/printpdf - try playing with the HTML.
See docs/HTML.md for the supported HTML/CSS.
// needs --features="html"
use *;
Command line
ppdf is a command-line tool built on printpdf. It converts a PDF to JSON
and back, so a PDF can be edited with jq, and it extracts text, text boxes, SVG
pages, fonts and images. It also renders HTML to PDF.
| |
Roadmap
The goal of printpdf is to be a general-use PDF library, such as libharu or similar, that writes PDFs which adhere to a PDF standard when you ask for one.
- For PDF/A-1b, PDF/A-2b and PDF/X-1a:2001, a document that uses only what the standard allows is written so that the preflight (
ppdf check) finds no errors - printpdf does not convert colors or remove forbidden content, and the save warns about it
- printpdf writes no tagged structure, so the
alevels of PDF/A claim levelb - Other standards (PDF/X-4, PDF/UA, ...) get the identification and the output intent only
- The PDF/A-1b and PDF/A-2b test documents pass veraPDF 1.30, and the PDF/X output is checked by the preflight only
- The preflight checks a subset of the rules and is not a certification, and a file with forbidden content still fails veraPDF
The printpdf wiki is live at: https://github.com/fschutt/printpdf/wiki
Here are some resources I found while working on this library:
PDFXPlorer, shows the DOM tree of a PDF, needs .NET 2.0- Official PDF 1.7 reference
- [GERMAN] How to embed unicode fonts in PDF
- PDF X/1-a Validator
- PDF X/3 technical notes
License / Support
Library is licensed MIT. The Foxit fonts of PDFium in third_party/foxit-fonts are BSD-3-Clause. So are the standard-font metrics measured from them.
You can donate (one-time or recurrent) at https://github.com/sponsors/fschutt. Thanks!