html2pdf-secure
A Rust crate for converting HTML to password-protected PDF files using headless Chrome and robust encryption.
Features
- 🌐 HTML to PDF Conversion: Convert HTML content, files, or URLs to PDF using headless Chrome
- 🔒 Password Protection: Add user and owner passwords to generated PDFs
- 🛡️ Multiple Encryption Levels: Support for various encryption standards (40-bit, 128-bit, AES-128, AES-256)
- ⚙️ Customizable PDF Options: Control page format, orientation, margins, and more
- 🔧 Flexible Permissions: Fine-grained control over PDF permissions (printing, copying, editing, etc.)
- 🚀 Async Support: Built with async/await for non-blocking operations
- 🔗 External Tool Integration: Optional integration with qpdf for stronger encryption
Quick Start
Add this to your Cargo.toml
:
[]
= "0.1"
= { = "1.0", = ["full"] }
Basic Usage
use ;
async
Installation Requirements
Basic Requirements
- Rust 1.85+: Required for Rust 2024 edition features
- Chrome/Chromium: Automatically downloaded by headless_chrome crate
Required: qpdf for Password Protection
Important: Password protection requires qpdf to be installed. Without qpdf, PDFs will be generated without encryption.
# Ubuntu/Debian
# macOS
# Windows
# Download from https://qpdf.sourceforge.io/
To verify qpdf installation:
Examples
Convert URL to Protected PDF
use ;
async
Advanced Configuration
use ;
async
API Reference
Core Types
Html2PdfConverter
: Main converter for HTML to PDF operationsPdfOptions
: Configuration for PDF generation (page size, margins, etc.)PasswordOptions
: Password and encryption configurationEncryptionLevel
: Encryption strength optionsPdfPermissions
: Fine-grained permission control
Conversion Methods
convert_html_to_pdf()
: Convert HTML string to PDFconvert_html_file_to_pdf()
: Convert HTML file to PDFconvert_url_to_pdf()
: Convert web page to PDFconvert_html_to_protected_pdf()
: Convert HTML to password-protected PDFconvert_html_to_protected_pdf_with_qpdf()
: Convert with qpdf encryption
Encryption Levels
Level | Description | Key Length | Security |
---|---|---|---|
Standard40 |
RC4 40-bit | 40-bit | Basic |
Standard128 |
RC4 128-bit | 128-bit | Good |
Aes128 |
AES 128-bit | 128-bit | Strong |
Aes256 |
AES 256-bit | 256-bit | Very Strong |
Security Considerations
Encryption Behavior
Important: This library requires qpdf for password protection:
- With qpdf installed: PDFs are properly encrypted with AES-256 by default
- Without qpdf: PDFs are generated without encryption and a warning is logged
- Fallback behavior: The library will attempt qpdf first, then fall back to unencrypted PDF
Encryption Levels
The library supports multiple encryption levels when qpdf is available:
- AES-256 (default): Strongest encryption, recommended for all use cases
- AES-128: Strong encryption, good for most applications
- 128-bit RC4: Legacy encryption, requires
--allow-weak-crypto
flag - 40-bit RC4: Very weak encryption, requires
--allow-weak-crypto
flag
Production Recommendations
For production use with sensitive data:
- Always install qpdf for proper encryption
- Use AES-256 (default) or AES-128 encryption levels
- Avoid RC4 encryption levels (weak and deprecated)
- Test encryption by verifying password requirements
Error Handling
use ;
async
Examples
Run the included examples:
# Basic usage
# qpdf encryption (requires qpdf installed)
# URL to PDF conversion
Testing
Run the test suite:
Performance
The crate is designed for efficiency:
- Reuses Chrome instances when possible
- Supports concurrent PDF generation
- Minimal memory footprint for PDF processing
Troubleshooting
Chrome/Chromium Issues
If you encounter Chrome-related errors:
- Ensure sufficient system resources
- Check Chrome installation
- Verify network connectivity for URL conversions
qpdf Integration
If qpdf encryption fails:
- Verify qpdf installation:
qpdf --version
- Check file permissions in temp directory
- Ensure sufficient disk space
Common Issues
- "Chrome browser error": Usually indicates Chrome startup issues
- "Encryption error": Check password requirements and qpdf availability
- "Invalid input": Verify HTML syntax and file paths
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.