Pivot PDF
A PDF creation library written in Rust, designed to be used from any language. The core is implemented in Rust and can be used directly in Rust projects. Language bindings are being built for PHP, Java, C#, Python, Go, and other major languages.
Designed for low memory and CPU consumption — even for documents with hundreds of pages — making it well suited for SaaS and web applications that generate reports, contracts, invoices, or bills of material on the fly.
See Full Documentation: https://pivotpdftools.github.io/pivot-pdf/
Features
- Text placement — place text at arbitrary (x, y) positions using any supported font
- TextFlow — automatic word wrap and multi-page text reflow with mixed font styles
- Word-break control — configurable overflow handling for long tokens: force-break, hyphenate, or allow overflow; applies to both TextFlow and table cells
- 14 built-in fonts — all standard PDF fonts (Helvetica, Times, Courier, Symbol, ZapfDingbats) with no embedding required
- TrueType font embedding — load
.ttffiles for full Unicode text with automatic metrics extraction - Line graphics — move/lineto paths, rectangles, stroke, fill, fill-stroke, color and line-width control
- Images — place JPEG and PNG images (with alpha) using fit, fill, stretch, or none fit modes
- Tables — streaming row-by-row table layout with per-cell styles, overflow modes, borders, and background colors
- Coordinate origin — choose between bottom-left (PDF native) or top-left (screen/web style) coordinates at document creation time; the library transparently converts
- Form fields — fillable AcroForm text fields at specified positions; uniqueness enforced across the document
- Page editing — open completed pages for overlay content (e.g. "Page X of Y" numbering)
- Compression — optional FlateDecode compression for all stream objects (typically 50–80% size reduction)
- PDF reading — open an existing PDF to inspect page count and version string
- PDF merging — combine two or more PDF files into a single output, pages appended in order
- PHP extension — full PHP binding exposing all features via a native extension
PHP Installation
Install the Composer package for IDE autocompletion stubs:
The native extension binary must be installed separately. For the full installation guide, see docs/php-installation.md.
Requirements
Rust (pdf-core, pdf-cli)
- Rust stable toolchain — install via rustup
PHP Extension (pdf-php)
- Rust stable toolchain (same as above)
- PHP development headers:
sudo apt install php-dev - Clang development libraries:
sudo apt install libclang-dev
Building
# Build all workspace members (debug)
# Build release (recommended for PHP extension)
Running Tests
# Run all Rust tests
Rust Examples
Examples write output PDFs to the examples/output/ directory.
# Basic document — place_text
# TextFlow — multi-page text reflow with mixed font styles
# Line graphics — paths, rectangles, stroke, fill
# Images — JPEG and PNG placement
# Tables — streaming row layout with headers
# TrueType fonts — embed a .ttf font
# Page numbers — edit completed pages to add "Page X of Y"
# Form fields — fillable AcroForm text fields
# Merge — combine multiple PDFs into one
PHP Extension
Build
The compiled extension is at target/release/libpdf_php.so.
Run Tests
Run PHP Examples
Each PHP example mirrors its Rust counterpart and writes to the examples/output/ directory.
EXT="-d extension=target/release/libpdf_php.so"
IDE type hints and autocompletion are provided by pdf-php/pdf-php.stubs.php.
Workspace Structure
pivot-pdf/
├── pdf-core/ # Core library — all PDF generation logic
│ ├── src/
│ └── tests/
├── pdf-php/ # PHP extension wrapping pdf-core
│ ├── src/
│ └── tests/
├── examples/ # Rust and PHP example programs
│ ├── rust/
│ ├── php/
│ └── output/ # Generated PDFs (git-ignored)
└── docs/ # Architecture and feature documentation
Coordinate System
All coordinates are in PDF points (1 pt = 1/72 inch). A standard US Letter page is 612 × 792 pt.
By default, the origin is at the bottom-left of the page with y increasing upward (PDF native). Optionally, you can use a top-left origin with y increasing downward — more natural for screen/web contexts.
Pass DocumentOptions when creating a document:
use ;
// Top-left (screen/web style)
let opts = DocumentOptions ;
let mut doc = create?;
// Bottom-left (PDF native, the default)
let mut doc = create?;
In PHP:
The origin setting applies uniformly to every coordinate-taking API (place_text, fit_textflow, fit_row, place_image, move_to, line_to, rectangle, and form fields). See docs/features/coordinate-origin.md for full details.