fop-pdf-renderer
A pure Rust PDF-to-image renderer. Parses PDF documents and rasterizes pages to RGBA/PNG images without any C or native dependencies. Designed to complement the fop-render crate by enabling self-contained verification of generated PDF output.
Features
- Pure Rust — zero C/C++ or native library dependencies; runs everywhere Rust compiles
- PDF parsing — xref table and xref-stream (PDF 1.5+) cross-reference parsing, indirect object resolution, full page-tree traversal
- Content stream interpretation — tokenizes and interprets PDF content operators (path construction, fills, strokes, text, images, graphics state)
- Stream filter decoding — FlateDecode (zlib/deflate), DCTDecode (JPEG pass-through), ASCIIHexDecode, and chained filter pipelines
- Rasterization — DPI-scalable rendering via tiny-skia; anti-aliased fill and stroke paths, image compositing
- Font handling — embedded TrueType font extraction via ttf-parser; ToUnicode CMap decoding; composite (Type0) font support
- PNG output — lossless RGBA PNG encoding via the png crate
- CLI tool —
fop-render-pdfbinary for batch and single-page rendering from the command line - Graceful error handling — structured
PdfRenderErrorenum with descriptive variants; no panics on malformed input
Installation
Add fop-pdf-renderer to your Cargo.toml:
[]
= "0.1"
Usage
Render a single page to a PNG file
use PdfRenderer;
Render a page to in-memory RGBA pixels
use PdfRenderer;
Render all pages
use PdfRenderer;
Working with RasterPage directly
use ;
Error handling
use ;
CLI Usage — fop-render-pdf
The crate ships a command-line binary named fop-render-pdf.
Installation
Or build from source:
# Binary is at: target/release/fop-render-pdf
Synopsis
fop-render-pdf <input.pdf> <output.png> [OPTIONS]
Options:
--page N Page to render (0-indexed, default: 0)
--dpi N Output resolution in DPI (default: 150)
--all Render all pages (output must contain %d, e.g. page-%d.png)
Examples
# Render the first page at the default 150 DPI
# Render page 2 (0-indexed) at 300 DPI
# Render all pages to page-0.png, page-1.png, …
# Render all pages at 72 DPI
The tool prints progress to stderr and exits with a non-zero status on error, making it safe to use in shell pipelines and CI scripts.
Public API Summary
| Item | Description |
|---|---|
PdfRenderer |
High-level entry point — parse and render PDF documents |
PdfRenderer::from_bytes(data) |
Parse a PDF from a &[u8] slice |
PdfRenderer::page_count() |
Return the number of pages |
PdfRenderer::render_page(idx, dpi) |
Render one page to a RasterPage |
PdfRenderer::save_as_png(idx, path, dpi) |
Render and write a PNG file |
PdfRenderer::render_all_pages(dpi) |
Render every page, return Vec<Vec<u8>> (PNG bytes) |
RasterPage |
Rasterized page: width, height, pixels (RGBA) |
RasterPage::to_png() |
Encode pixels as PNG, returning Vec<u8> |
RasterPage::save_png(path) |
Encode and write PNG to a file path |
PdfRenderError |
Structured error enum (Parse, Unsupported, PageNotFound, Font, Image, Io, Decompress) |
Result<T> |
Type alias for std::result::Result<T, PdfRenderError> |
Feature Flags
This crate currently has no optional Cargo feature flags. All capabilities are enabled by default.
Integration with fop-render
fop-pdf-renderer is designed to work alongside fop-render, the XSL-FO to PDF backend in the fop workspace. A typical verification workflow is:
// 1. Generate a PDF with fop-render
let pdf_bytes: = pdf_doc.to_bytes?;
// 2. Immediately render it back to an image for visual inspection
let renderer = from_bytes?;
let image = renderer.render_page?;
image.save_png?;
Dependencies
| Crate | Purpose |
|---|---|
thiserror |
Derive-based error type definitions |
log |
Structured logging facade |
flate2 |
FlateDecode (zlib/deflate) stream decompression |
ttf-parser |
TrueType/OpenType font outline parsing |
png |
PNG encoding |
jpeg-decoder |
DCT/JPEG image decoding |
tiny-skia |
Software 2D rasterizer (paths, fills, strokes, anti-aliasing) |
All dependencies are pure Rust with no C or Fortran build dependencies.
License
Copyright 2024 COOLJAPAN OU (Team Kitasan)
Licensed under the Apache License, Version 2.0.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.