| Crates.io | html2pdf-secure |
| lib.rs | html2pdf-secure |
| version | 0.1.0 |
| created_at | 2025-09-23 10:16:41.453606+00 |
| updated_at | 2025-09-23 10:16:41.453606+00 |
| description | A Rust crate for converting HTML to password-protected PDF files |
| homepage | |
| repository | https://github.com/darshan25503/html2pdf-secure |
| max_upload_size | |
| id | 1851291 |
| size | 174,302 |
A Rust crate for converting HTML to password-protected PDF files using headless Chrome and robust encryption.
Add this to your Cargo.toml:
[dependencies]
html2pdf-secure = "0.1"
tokio = { version = "1.0", features = ["full"] }
use html2pdf_secure::{Html2PdfConverter, PdfOptions, PasswordOptions};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create converter
let converter = Html2PdfConverter::new().await?;
// HTML content
let html = "<html><body><h1>Hello, Secure World!</h1></body></html>";
// Configure password protection (uses AES-256 by default)
let password_options = PasswordOptions::new("user123", "owner456");
// Convert to protected PDF (requires qpdf)
let pdf_data = converter
.convert_html_to_protected_pdf(html, PdfOptions::default(), password_options)
.await?;
// Save to file
std::fs::write("secure_document.pdf", pdf_data)?;
Ok(())
}
Important: Password protection requires qpdf to be installed. Without qpdf, PDFs will be generated without encryption.
# Ubuntu/Debian
sudo apt-get install qpdf
# macOS
brew install qpdf
# Windows
# Download from https://qpdf.sourceforge.io/
To verify qpdf installation:
qpdf --version
use html2pdf_secure::{Html2PdfConverter, PdfOptions, PasswordOptions, PageFormat};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let converter = Html2PdfConverter::new().await?;
let pdf_options = PdfOptions {
page_format: PageFormat::A4,
print_background: true,
..Default::default()
};
let password_options = PasswordOptions::new("secret", "admin");
let pdf_data = converter
.convert_url_to_protected_pdf(
"https://www.rust-lang.org/",
pdf_options,
password_options
)
.await?;
std::fs::write("rust_website.pdf", pdf_data)?;
Ok(())
}
use html2pdf_secure::{
Html2PdfConverter, PdfOptions, PasswordOptions,
EncryptionLevel, PdfPermissions, PageFormat, PageOrientation
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let converter = Html2PdfConverter::new().await?;
// Advanced PDF options
let pdf_options = PdfOptions {
page_format: PageFormat::Letter,
orientation: PageOrientation::Landscape,
print_background: true,
scale: 0.8,
..Default::default()
};
// Strong encryption with restricted permissions
let password_options = PasswordOptions::new("user_pass", "owner_pass")
.with_encryption_level(EncryptionLevel::Aes256)
.with_permissions(PdfPermissions {
allow_printing: false,
allow_copy: false,
allow_modify_contents: false,
..Default::default()
});
let html = "<html><body><h1>Highly Secure Document</h1></body></html>";
// Use qpdf for strongest encryption (if available)
let pdf_data = converter
.convert_html_to_protected_pdf_with_qpdf(
html,
pdf_options,
password_options,
Some("/tmp")
)
.await?;
std::fs::write("highly_secure.pdf", pdf_data)?;
Ok(())
}
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 controlconvert_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| 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 |
Important: This library requires qpdf for password protection:
The library supports multiple encryption levels when qpdf is available:
--allow-weak-crypto flag--allow-weak-crypto flagFor production use with sensitive data:
use html2pdf_secure::{Html2PdfConverter, Html2PdfError};
#[tokio::main]
async fn main() {
let converter = Html2PdfConverter::new().await.unwrap();
match converter.convert_html_to_pdf("<html></html>", Default::default()).await {
Ok(pdf_data) => println!("Success: {} bytes", pdf_data.len()),
Err(Html2PdfError::ChromeError(msg)) => eprintln!("Chrome error: {}", msg),
Err(Html2PdfError::EncryptionError(msg)) => eprintln!("Encryption error: {}", msg),
Err(e) => eprintln!("Other error: {}", e),
}
}
Run the included examples:
# Basic usage
cargo run --example basic_usage
# qpdf encryption (requires qpdf installed)
cargo run --example qpdf_encryption
# URL to PDF conversion
cargo run --example url_to_pdf
Run the test suite:
cargo test
The crate is designed for efficiency:
If you encounter Chrome-related errors:
If qpdf encryption fails:
qpdf --versionContributions are welcome! Please feel free to submit a Pull Request.
git clone https://github.com/yourusername/html2pdf-secure
cd html2pdf-secure
cargo build
cargo test
This project is licensed under either of
at your option.