#vector-graphics #pdf #graphics #pdf-file #vector

pdf-canvas

Generate PDF files in pure Rust. Currently, simple vector graphics and text set in the 14 built-in fonts are supported

4 releases (2 breaking)

0.7.0 Feb 7, 2022
0.6.0 Jul 15, 2018
0.5.4 Feb 14, 2017
0.5.2 Feb 14, 2017

#350 in Graphics APIs

Download history 68/week @ 2024-07-20 115/week @ 2024-07-27 61/week @ 2024-08-03 200/week @ 2024-08-10 137/week @ 2024-08-17 81/week @ 2024-08-24 138/week @ 2024-08-31 111/week @ 2024-09-07 124/week @ 2024-09-14 217/week @ 2024-09-21 123/week @ 2024-09-28 132/week @ 2024-10-05 97/week @ 2024-10-12 149/week @ 2024-10-19 246/week @ 2024-10-26 157/week @ 2024-11-02

666 downloads per month
Used in 4 crates

MIT license

150KB
1.5K SLoC

pdf-canvas

A pure rust library for generating PDF files. Currently, simple vector graphics and text set in the 14 built-in fonts are supported.

Build Status Build status

To use this library, add it as a dependency in your Cargo.toml:

[dependencies]
pdf-canvas = "*"

The API is still very alpha, usage may change. Some examples, that should work with the version containing them, can be found in the examples directory. Read the API documentation for the pdf-canvas crate. A larger example of a program using this crate is chord3, a chopro formatter.


lib.rs:

A library for creating pdf files.

Currently, simple vector graphics and text set in the 14 built-in fonts are supported. The main entry point of the crate is the struct Pdf, representing a PDF file being written.

Example

use pdf_canvas::{Pdf, BuiltinFont, FontSource};
use pdf_canvas::graphicsstate::Color;

let mut document = Pdf::create("example.pdf")
    .expect("Create pdf file");
// The 14 builtin fonts are available
let font = BuiltinFont::Times_Roman;

// Add a page to the document.  This page will be 180 by 240 pt large.
document.render_page(180.0, 240.0, |canvas| {
    // This closure defines the content of the page
    let hello = "Hello World!";
    let w = font.get_width(24.0, hello) + 8.0;

    // Some simple graphics
    canvas.set_stroke_color(Color::rgb(0, 0, 248))?;
    canvas.rectangle(90.0 - w / 2.0, 194.0, w, 26.0)?;
    canvas.stroke()?;

    // Some text
    canvas.center_text(90.0, 200.0, font, 24.0, hello)
}).expect("Write page");
// Write all pending content, including the trailer and index
document.finish().expect("Finish pdf document");

To use this library you need to add it as a dependency in your Cargo.toml:

[dependencies]
pdf-canvas = "*"

Some more working usage examples exists in [the examples directory] (https://github.com/kaj/rust-pdf/tree/master/examples).

Dependencies

~1MB
~18K SLoC