CarbonPDF
Production-ready HTML to PDF conversion in Rust using Headless Chrome.
Features
- High-fidelity rendering - Uses Headless Chrome for accurate HTML/CSS rendering
- Production-ready - Battle-tested architecture with comprehensive error handling
- Fully configurable - Control page size, margins, orientation, scale, and more
- Easy to use - Ergonomic builder API and sensible defaults
- Type-safe - Leverages Rust's type system for compile-time correctness
- Async by default - Built on Tokio for high-performance concurrent operations
- Modern web standards - Full support for CSS3, flexbox, grid, web fonts
- Extensible - Trait-based architecture for pluggable rendering backends
Requirements
Chrome or Chromium must be installed and accessible. CarbonPDF will automatically detect Chrome in standard locations, or you can specify a custom path.
Installation
On Ubuntu/Debian:
On Arch Linux:
On macOS:
On Windows: Download from google.com/chrome
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
Basic Usage
use ;
async
From File
let pdf = new
.file
.page_size
.build
.await?;
From URL
let pdf = new
.url
.build
.await?;
Template Features (Optional)
CarbonPDF includes optional Handlebars templating support for custom templates with variables.
Enable Templates
Add the templates feature to your Cargo.toml:
[]
= { = "0.2", = ["templates"] }
Custom Templates
Use your own Handlebars templates:
use ;
use json;
async
Template Helpers
CarbonPDF includes custom Handlebars helpers:
{{format_currency value symbol}}- Format currency values{{format_date date_string}}- Format dates
Example:
<p>Total: {{format_currency 1234.56 "$"}}</p>
<!-- Outputs: Total: $1234.56 -->
Advanced Configuration
use ;
let pdf = new
.html
.page_size
.orientation
.margins // top, right, bottom, left (inches)
.scale
.print_background
.header
.footer
.timeout // seconds
.build
.await?;
Custom Page Size
let pdf = new
.html
.custom_page_size // width, height in inches
.build
.await?;
Docker/CI Configuration
let pdf = new
.html
.no_sandbox // Required in Docker
.chrome_args
.build
.await?;
CLI Tool
Install the CLI:
Usage:
# Convert HTML file
# Convert from URL
# With options
Architecture
CarbonPDF uses a layered architecture:
- Public API Layer - Builder pattern and high-level types
- Renderer Abstraction -
PdfRenderertrait for backend implementations - Chrome Backend - Chrome DevTools Protocol integration
- Process Management - Automatic Chrome lifecycle handling
See ARCHITECTURE.md for detailed design documentation.
Error Handling
CarbonPDF provides detailed error types:
use Error;
match pdf_result
Best Practices
In Backend Services
// Reuse renderer instance across requests
lazy_static!
async
In CI/CD
# Dockerfile
FROM rust:1.70 as builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y \
chromium \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/myapp /usr/local/bin/
CMD ["myapp"]
// In your code
let pdf = new
.html
.no_sandbox // Required in Docker
.chrome_args
.build
.await?;
Performance Tips
- Reuse renderer instances - Browser initialization is expensive
- Set appropriate timeouts - Default is 30s, adjust based on content complexity
- Optimize HTML - Minimize external resources, inline critical CSS
- Use connection pooling - For high-volume scenarios (coming in v0.2)
Roadmap
- Automatic Chrome download (via feature flag)
- Connection pooling for high-volume scenarios
- Playwright backend support
- PDF/A compliance mode
- Watermarking support
- Parallel batch processing utilities
- Add templates to cli
- Add stdin to cli
- Add --css to cli
- migrar a subcommands (carbonpdf render)
Contributing
Contributions welcome! Please see CONTRIBUTING.md.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Acknowledgments
Built on the shoulders of giants:
- chromiumoxide - Chrome DevTools Protocol
- tokio - Async runtime
- The Chromium team for an amazing rendering engine