#vector-graphics #pdf #pdf-file #font #canvas #pure #built-in

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

#559 in Images

Download history 25/week @ 2023-11-18 56/week @ 2023-11-25 19/week @ 2023-12-02 20/week @ 2023-12-09 36/week @ 2023-12-16 28/week @ 2023-12-23 5/week @ 2023-12-30 46/week @ 2024-01-06 42/week @ 2024-01-13 50/week @ 2024-01-20 11/week @ 2024-01-27 12/week @ 2024-02-03 77/week @ 2024-02-10 98/week @ 2024-02-17 98/week @ 2024-02-24 50/week @ 2024-03-02

325 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