A step-by-step, zero-unsafe 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.
The following example creates a PDF with a single, empty A4 page.
```rust use pdf_writer::{PdfWriter, Rect, Ref};
// Define some indirect reference ids we'll use. let catalogid = Ref::new(1); let pagetreeid = Ref::new(2); let pageid = Ref::new(3);
// Start writing with the PDF version 1.7 header. let mut writer = PdfWriter::new(1, 7);
// The document catalog and a page tree with one A4 page that uses no resources. writer.catalog(catalogid).pages(pagetreeid); writer.pages(pagetreeid).kids([pageid]); writer.page(pageid) .parent(pagetreeid) .mediabox(Rect::new(0.0, 0.0, 595.0, 842.0)) .resources();
// Finish with cross-reference table and trailer and write to file. std::fs::write("target/empty.pdf", writer.finish(catalog_id))?; ```
For a more comprehensive overview, check out the [hello world example], which creates a document with text and a link in it.
This crate is dual-licensed under the MIT and Apache 2.0 licenses.