fpdf

Crates.iofpdf
lib.rsfpdf
version0.1.0
created_at2025-09-02 17:58:00.259095+00
updated_at2025-09-02 17:58:00.259095+00
descriptionFDPF for writing PDF files in Rust.
homepage
repositoryhttps://codeberg.org/HumansAreWeak/fpdf-rs
max_upload_size
id1821479
size3,824,460
Maik Steiger (HumansAreWeak)

documentation

README

FPDF for Rust

A PDF document generator with high level support for text, drawing and images. It is written in safe Rust.

[!IMPORTANT] This project is in its very early development state and is thus prone to API changes. It is not recommended to build a product around this crate for the time being.

If you encounter any issue or crashes using this crate, please open an issue and post a reproducible setup.

If you have any wishes as to how the API can be improved and be made more "Rust"-idiomatic, please open an issue with that request.

Features

  • UTF-8 support
  • Page format and margins
  • Page header and footer management
  • Automatic page break, line breaks and text justification
  • Including of JPEG, PNG, GIF, TIFF and basic path-only SVG images
  • Colors, gradients and alpha channel transparency
  • Outline bookmarks
  • Internal and external links
  • TrueType, Type1 and encoding support
  • Page compression
  • Lines, Bézier curves, arcs, and ellipses
  • Rotation, scaling, skewing, translation, and mirroring
  • Clipping
  • Document protection (RC4 is cryptographically broken)
  • Layers
  • Templates
  • File attachements
  • Embedded XML
  • Embedded JavaScript
  • No unsafe Rust in the entire code

The fpdf crate has its use of dependencies minimized. This avoids compilation bloat for any project and its target binary size and performance is more predictable.

fpdf explicitly supports UTF-8 TrueType fonts and "right-to-left" languages.

Additional Features:

  • Very basic HTML support (using html5gum)

Basic Example

Here is an example code shown for a very quick introduction into the FPDF API. Take a look at the examples/ folder for more examples.

use fpdf::{Fpdf, ImageOptions, Pdf, Unit, UnitVec2};

fn main() {
    let mut pdf = Fpdf::default();
    pdf.set_top_margin(Unit::mm(30.0));

    pdf.set_header_fn_mode(
        |pdf| {
            pdf.image(
                "examples/resources/logo.png",
                Unit::mm(10.0),
                Unit::mm(6.0),
                UnitVec2::mm(30.0, 0.0),
                false,
                ImageOptions::default(),
                0,
                "",
            );
            pdf.set_y(Unit::mm(5.0));
            pdf.set_font("Arial", "B", Unit::pt(15.0));
            pdf.cell(Unit::mm(80.0), Unit::mm(0.0), "");
            pdf.cell_format(
                Unit::mm(30.0),
                Unit::mm(10.0),
                "Title",
                "1",
                0,
                "C",
                false,
                0,
                "",
            );
            pdf.ln(Unit::mm(20.0));
        },
        true,
    );

    pdf.set_footer_fn(|pdf| {
        pdf.set_y(Unit::mm(-15.0));
        pdf.set_font("Arial", "I", Unit::pt(8.0));
        pdf.cell_format(
            Unit::mm(0.0),
            Unit::mm(10.0),
            format!("Page {} / {{nb}}", pdf.page_no()).as_str(),
            "",
            0,
            "C",
            false,
            0,
            "",
        );
    });

    pdf.alias_nb_pages("");
    pdf.add_page();
    pdf.set_font("Times", "", Unit::pt(12.0));
    for j in 1..=40 {
        pdf.cell_format(
            Unit::mm(0.0),
            Unit::mm(10.0),
            format!("Printing line number {}", j).as_str(),
            "",
            1,
            "",
            false,
            0,
            "",
        );
    }

    pdf.output_file_and_close("example_add_page.pdf")
        .expect("could not produce PDF file");
}

Roadmap

The fpdf crate's goal is to become an established, general-use PDF library, such as the original FPDF for PHP. The F stands for Free and thus this project is free to use any modify however you like. A copyleft license is implemented to forbide the commercial usage of this package for user's in need, like the inspired Golang FDPF library has experienced.

Before this crate is published on crates.io, all examples should at least be able to run and produce the expected output.

Alternatives

If you want finer and more granular control over how your PDF file is produced, consider using one of the alternative Rust crate's for generating PDF files:

Errors

To ease the creation of PDF documents, the API mostly panics in case of errors, to avoid unwrap calls in user's code. The error can be read by the user directly to indicate its type. Methods are provided to easily check if the current PDF instance has encountered an error, if the user decides, they want this feature implemented.

License

The fpdf crate is released under GPLv3 license. It is copyrighted by Maik Steiger.

Commit count: 0

cargo fmt