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

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

#480 in Images

Download history 4/week @ 2024-01-01 64/week @ 2024-01-08 32/week @ 2024-01-15 51/week @ 2024-01-22 23/week @ 2024-02-05 98/week @ 2024-02-12 80/week @ 2024-02-19 106/week @ 2024-02-26 60/week @ 2024-03-04 74/week @ 2024-03-11 72/week @ 2024-03-18 43/week @ 2024-03-25 188/week @ 2024-04-01 30/week @ 2024-04-08 66/week @ 2024-04-15

336 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