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 |
#292 in Graphics APIs
568 downloads per month
Used in 4 crates
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.
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