pdf-writer
A step-by-step PDF writer.
The entry point into the API is the main PdfWriter
, which constructs the
document into one big internal buffer. The top-level writer has many methods to
create specialized writers for specific PDF objects. These all follow the same
general pattern: They borrow the main buffer mutably, expose a builder pattern
for writing individual fields in a strongly typed fashion and finish up the
object when dropped.
There are a few more top-level structs with internal buffers, like the builder
for Content
streams, but wherever possible buffers are borrowed from parent
writers to minimize allocations.
Minimal example
The following example creates a PDF with a single, empty A4 page.
use ;
// Define some indirect reference ids we'll use.
let catalog_id = new;
let page_tree_id = new;
let page_id = new;
// Write a document catalog and a page tree with one A4 page that uses no resources.
let mut writer = new;
writer.catalog.pages;
writer.pages.kids.count;
writer.page
.parent
.media_box
.resources;
// Finish with cross-reference table and trailer and write to file.
write?;
For a more comprehensive overview, check out the hello world example, which creates a document with text and a link in it.
Safety
This crate forbids unsafe code, but it depends on widespread crates that use unsafe internally.
License
This crate is dual-licensed under the MIT and Apache 2.0 licenses.