# pdfox 🦊
A pure-Rust PDF library. Create, parse, and render PDF documents with **zero C dependencies** and **zero unsafe blocks**.
[](https://crates.io/crates/pdfox)
[](https://docs.rs/pdfox)
[](LICENSE)
## Features
| **PDF creation** | Pages, text, shapes, images, hyperlinks |
| **PDF parsing** | Object tree traversal, text extraction |
| **14 built-in fonts** | Helvetica, Times, Courier families with per-glyph widths |
| **Tables** | Headers, alternating rows, column spans, borders |
| **Text flow** | Multi-page reflowing with headings, bullets, code blocks |
| **AcroForm** | Text, checkbox, radio, dropdown, button fields |
| **Outlines** | Nested bookmarks with color and style |
| **Watermarks** | Diagonal text stamps with opacity |
| **Headers/Footers** | `{page}` / `{total}` macros, automatic per-page |
| **Encryption** | RC4-40 / RC4-128 with permission flags |
| **Digital signatures** | `adbe.pkcs7.detached` placeholder + inject workflow |
| **Images** | JPEG (DCTDecode) and raw RGB embedding |
| **Compression** | FlateDecode (zlib) on all content streams |
## Quick Start
```toml
[dependencies]
pdfox = "0.1"
```
```rust
use pdfox::prelude::*;
use pdfox::color::Color;
fn main() {
let pdf = Document::new()
.title("Hello World")
.author("Samuel")
.page(|p| {
let f = p.use_helvetica();
let reg = p.reg_key(&f);
let bold = p.bold_key(&f);
p.filled_rect(0.0, 791.0, 595.0, 50.0, Color::rgb_u8(30, 60, 140));
p.text_centered("Hello from pdfox!", 297.5, 813.0, &bold, 20.0, Color::WHITE);
p.text("Pure Rust. No C deps. No unsafe.", 50.0, 740.0, ®, 12.0, Color::BLACK);
})
.build();
std::fs::write("hello.pdf", pdf).unwrap();
}
```
## Full Example
```rust
use pdfox::prelude::*;
use pdfox::color::Color;
fn main() {
// Bookmarks
let outline = Outline::new()
.add(OutlineItem::new("Chapter 1", Destination::Page(0)).bold())
.add(OutlineItem::new("Chapter 2", Destination::Page(1)));
// Interactive form
let form = AcroForm::new()
.add(FormField::text("name", FieldRect::new(100.0, 700.0, 300.0, 22.0), 0)
.value("Samuel").tooltip("Your name"))
.add(FormField::checkbox("agree", FieldRect::new(100.0, 670.0, 16.0, 16.0), 0, true));
// Watermark + footer
let watermark = Watermark::diagonal("DRAFT").opacity(0.10);
let footer = HeaderFooter::new()
.right(HFSlot::new("Page {page} of {total}", HFAlign::Right));
// Text flow across pages
let flow = TextFlow::new(FlowStyle::default())
.heading("Introduction", 1)
.paragraph("This text automatically reflows across pages...")
.bullets(vec!["Feature one", "Feature two", "Feature three"]);
let (pdf, _sigs) = Document::new()
.title("My Document")
.outline(outline)
.form(form)
.watermark(watermark)
.footer(footer)
.add_pages(flow.render(595.276, 841.890))
.page(|p| {
let f = p.use_helvetica();
p.text("Manual page content", 50.0, 750.0, &p.reg_key(&f), 12.0, Color::BLACK);
})
.build_signed();
std::fs::write("output.pdf", pdf).unwrap();
}
```
## Running the Demo
```bash
git clone https://github.com/sazalo101/pdffox
cd pdffox
cargo run --bin pdfox-demo
# Opens pdfox_demo.pdf — a 7-page showcase of all features
```
## Running Tests
```bash
cargo test
# 55 tests covering every module
```
## Architecture
```
Document ← top-level builder (pages, metadata, outline, form, sig)
└── PageBuilder ← per-page content (fonts, text, shapes, images, tables)
├── ContentStream ← raw PDF operator emitter
├── Table ← grid layout renderer
└── PdfImage ← JPEG / RGB image XObjects
TextFlow ← multi-page reflow engine
AcroForm ← interactive form fields
Outline ← bookmark navigation tree
Watermark ← diagonal/straight text stamps
HeaderFooter ← page header/footer with macros
Encryption ← RC4-40/128 + permission flags
SignatureField ← adbe.pkcs7.detached placeholder
PdfWriter ← low-level byte emitter + xref table
ParsedDocument ← PDF parser + text extractor
```
## Modules
| `color` | RGB, Grayscale, CMYK color types |
| `content` | PDF content stream operators |
| `document` | Top-level document builder |
| `encrypt` | RC4 encryption + permission flags |
| `error` | Unified `PdfError` type |
| `flow` | Multi-page text flow engine |
| `font` | 14 built-in Type1 fonts with glyph widths |
| `form` | AcroForm interactive fields |
| `image` | JPEG and raw RGB image embedding |
| `object` | Core PDF object model |
| `outline` | Document bookmarks/outline tree |
| `page` | Page builder and font registration |
| `parser` | PDF parser and text extractor |
| `signature` | Digital signature placeholder |
| `table` | Table layout and rendering |
| `watermark` | Watermarks and header/footer |
| `writer` | Low-level byte writer and xref |
## License
MIT — see [LICENSE](LICENSE)