# pdfmake_rust

Rust library for generating PDF documents. Inspired by the JavaScript [pdfmake](https://github.com/bpampuch/pdfmake) library.

## Features

- Pure Rust, no C dependencies
- Builder API for easy document construction
- Tables with cell spanning (colSpan, rowSpan)
- Lists (ordered, unordered)
- Images (PNG, JPEG)
- SVG rendering
- QR codes
- Text wrapping and alignment
- Page headers and footers (static and dynamic)
- CJK font support (register TTF files)
- Font subsetting (only used glyphs are embedded)
- Named styles for consistent formatting
- Feature flags for optional components

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
pdfmake_rust = "0.1.0"
```

## Quick Start

```rust
use pdfmake_rust::{Document, DocumentNode, Margins, PageSize, PdfMake, TextNode};
use std::fs;

fn main() {
    let doc = Document::builder()
        .page_size(PageSize::a4())
        .page_margins(Margins::all(40.0))
        .content(DocumentNode::Text(TextNode::new("Hello, World!")))
        .build();

    let pdf = PdfMake::new();
    let bytes = pdf.render(&doc).expect("Failed to render PDF");

    fs::write("output.pdf", &bytes).expect("Failed to write PDF");
}
```

## More Examples

See the `examples/` directory for complete examples:

- `hello_world` - Basic document
- `table` - Table with spanning
- `invoice` - Real-world invoice layout
- `multi_column` - Multi-column layout

Run examples with:

```bash
cargo run --example hello_world
cargo run --example table
cargo run --example invoice
```

## Feature Flags

Optional components can be enabled/disabled via feature flags:

```toml
[dependencies]
pdfmake_rust = { version = "0.1.0", default-features = false, features = ["svg", "image", "qr"] }
```

| Feature | Default | Description |
|---------|---------|-------------|
| `svg` | Yes | SVG rendering support |
| `image` | Yes | PNG/JPEG image support |
| `qr` | Yes | QR code generation |
| `encryption` | No | PDF encryption (planned) |
| `tagged-pdf` | No | Tagged PDF for accessibility (planned) |

## Architecture

The rendering pipeline follows these stages:

```
Document → normalize → measure → layout → render → PDF bytes
```

| Stage | Module | Purpose |
|-------|--------|---------|
| Model | `model.rs` | Document structure and builder API |
| Normalize | `normalize.rs` | Flatten document tree into blocks |
| Measure | `measure.rs` | Calculate text metrics, line wrapping |
| Layout | `layout.rs` | Position blocks into pages |
| Render | `render.rs` | Emit PDF 1.4 byte stream |

## CJK Font Support

Register CJK fonts to support Chinese, Japanese, Korean text:

```rust
let doc = Document::builder()
    .font("SimHei", "path/to/SimHei.ttf")
    .content(DocumentNode::Text(TextNode::new("你好世界")))
    .build();
```

## Acknowledgments

This project is inspired by and references the JavaScript [pdfmake](https://github.com/bpampuch/pdfmake) library created by bpampuch and maintained by liborm85. The Rust implementation is written from scratch while following the same design principles.

## License

Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for details.

The original JavaScript pdfmake library is licensed under the MIT License.
