DocRaptor HTML TO PDF API
Rust Logo

Top Rust PDF Generation Libraries

There are a few Rust PDF generation libraries, but unfortunately, it's a relatively limited selection compared to other languages. We'll review the pros and cons of each package, as well a few non-Rust options for your consideration, including DocRaptor's HTML to PDF API.

Evaluating PDF Generators

When evaluating a PDF generator, there are several crucial factors to consider.

  • Ease of use: The best PDF file generators are user-friendly, requiring minimal code to integrate and use within your project. Strongly consider the development and implementation time between various generators.
  • Features: Look for a library that allows you to create documents you need. This can be surprisingly challenging, especially for complex or high-quality printed documents. We've created a separate guide on the common problem areas of PDF documents.
  • Performance: Assess the speed and efficiency of the PDF generator, as well as its resource usage, especially if you're dealing with large volumes of data or complex designs. If you need to generate many concurrent PDF files, some libraries may require substantially more infrastructure than other libraries.
  • Documentation and support: Good libraries usually have robust documentation and a community that offers support for troubleshooting and updates. If you have a question, how quickly can you get an answer?
  • Compliance and compatibility: Only a few PDF generators can create accessible (or "tagged") PDFs or PDFs compliant with printing or archival standards.

By keeping these factors in mind, you can select the most suitable PDF generator for your Rust project.

Rust PDF Builders

These libraries allow you to generate a PDF element by element, text line by text line. We generally recommend against using PDF builders. They allow for the most minute control, but they're difficult and slow to implement. They provide little support for tables, charts, table of contents, accessibility, forms, etc. We include them here because they're popular in the Rust community.

lopdf

lopdf supports PDF document creation, merging, and editing. You'll be editing the individual PDF elements and the lopdf readme suggests it would be useful to understand the PDF specification. With this power comes a lot of effort. Creating anything other than the most basic PDFs in lopdf will be very timing consuming.

printpdf

printpdf builds upon lopdf with a PDF creation focus. It has a slightly simpler interface, but you're still adding one element at a time.

Rust HTML to PDF Generators

HTML to PDF generators are the easiest and fastest way to create a PDF. They piggyback on the power of HTML, CSS, and JavaScript to provide powerful features. Tables, columns, font family support, charts, SVG support, and much more are readily available. Columns and flexbox provide many page layout options.

Headless Chrome

Similar to Puppeteer, the aptly named Headless Chrome crate is a high-level API to control headless Google Chromium. It allows you to create HTML documents with the latest in CSS and JavaScript functionality and turn them into PDFs.

The largest downside is that Chromium can be a resource hog. If your projects requires concurrently generating multiple PDFs, you'll need to more deeply consider your infrastructure needs.

Non-Rust HTML to PDF Generators

Despite the immense power of HTML and CSS, it still lacks many useful (and sometimes necessary) features. CSS is focused on webpages, which are long, continuously scrolled documents. PDF documents, on the other hand, made up of multiple separate pages.

The CSS Paged Media specifications were designed to fill this gap. They provide support for page regions, named pages, page counters, page selectors, footnotes, cross-references and much more. With these features, it becomes trivial to create headers, footers, watermarks, table of contents, indexes, and many other features necessary for printed or high-quality documents.

Unfortunately, none of the browsers, including Google Chrome, yet fully support CSS Paged Media. But, several non-Rust HTML to PDF generators do!

Prince

Prince is the leading commercial HTML to PDF generator. While the majority of Prince is written in Mercury, they've recently started working with Rust as well. Prince's support for CSS Paged Media makes it simple to create the most complex PDFs.

Because Prince has created their own HTML/CSS engine, it's normally much faster and less resource intensive than headless Chrome. The major downside, other than cost, is that it also provides less support for cutting edge JavaScript and CSS and may render slightly differently than browsers.

WeasyPrint

WeasyPrint is a popular Python-based open-source HTML to PDF generator. Similar to Prince, it's largely focused on supporting CSS Paged Media and solving the gaps left by the browsers. In our experience, it struggles with more complex documents and edge case scenarios.

DocRaptor

DocRaptor is an HTML to PDF API that uses Prince as the PDF generator. With DocRaptor, you get the full power of Prince but with paid plans as low as $15/mo. We provide demo documents so you can see for yourself the unique features provided by Prince and DocRaptor, as well as free templates for your own use.

We offer a public API key for testing so if you want to try DocRaptor, just copy and paste this Rust code sample into a new project:

use reqwest;
use serde_json::json;
use std::fs::File;
use std::io::Write;

#[tokio::main]
async fn main() -> Result<(), Box> {
    let api_key = "YOUR_API_KEY_HERE"; // <- actually works!
    let document_content = "Hello World!";

    let client = reqwest::Client::new();
    let res = client.post("https://docraptor.com/docs")
        .header("Content-Type", "application/json")
        .basic_auth(api_key, Some(""))
        .json(&json!({
            "doc": {
                "document_type": "pdf",
                "document_content": document_content,
                "test": true,
            }
        }))
        .send()
        .await?;

    let body = res.bytes().await?;
    let mut file = File::create("output.pdf")?;
    file.write_all(&body)?;

    Ok(())
}

In conclusion, the choice of a PDF generator for your Rust project largely depends on your specific needs and requirements. While a PDF builder like lopdf or printpdf might suit you for very simple projects, HTML to PDF generators like Headless Chrome, Prince, WeasyPrint, and `DocRaptor offer more advanced features. These options leverage the power of HTML and CSS, filling in the gaps left by browsers and facilitating the creation of more complex PDFs.

Remember to evaluate these options based on their functionality, performance, support, and compatibility to make an informed decision. The Rust community offers a diverse array of libraries and tools to help you with your PDF generation needs. Let your project requirements guide you to the right solution.