Skip to main content

Crate pdfrs

Crate pdfrs 

Source
Expand description

§PDF-CLI Library

A comprehensive Rust library for reading, writing, and manipulating PDF documents. This library provides functionality for:

  • PDF Generation: Create PDFs from markdown or raw text content
  • PDF Parsing: Extract text and structure from existing PDFs
  • PDF Manipulation: Merge, split, rotate, and reorder pages
  • Image Support: Embed JPEG, PNG, and BMP images in PDFs
  • Annotations: Add text, link, and highlight annotations
  • Forms: Create interactive PDF forms with text fields, checkboxes, radio buttons, and dropdowns
  • Watermarks: Add text or image watermarks to PDFs
  • Metadata: Manage document metadata including custom fields
  • Security: Add password protection and permissions to PDFs

§Quick Start

use pdfrs::pdf_generator;
use pdfrs::elements;

// Parse markdown content
let markdown = "# Hello World\n\nThis is a test document.";
let elements = elements::parse_markdown(markdown);

// Generate PDF
let layout = pdf_generator::PageLayout::portrait();
pdf_generator::create_pdf_from_elements_with_layout(
    "output.pdf",
    &elements,
    "Helvetica",
    12.0,
    layout,
).expect("Failed to create PDF");

§Modules

  • pdf: PDF document parsing and text extraction
  • pdf_generator: PDF generation from elements and content streams
  • pdf_ops: High-level PDF operations (merge, split, watermark, etc.)
  • elements: Markdown parsing and element representation
  • markdown: Markdown to PDF conversion utilities
  • image: Image loading, parsing, and PDF embedding
  • compression: Data compression utilities
  • security: PDF security, encryption, and permission management
  • builder: Fluent builder API for ergonomic PDF creation
  • streaming: Memory-efficient streaming PDF generation for large documents
  • parallel: High-performance parallel PDF operations using Rayon
  • optimization: PDF optimization profiles for different use cases (web, print, archive, ebook)

§Examples

§Creating a PDF from Markdown

use pdfrs::markdown;

markdown::markdown_to_pdf_full(
    "input.md",
    "output.pdf",
    "Helvetica",
    12.0,
    pdfrs::pdf_generator::PageOrientation::Portrait,
).expect("Failed to convert");

§Merging PDFs

use pdfrs::pdf_ops;

pdf_ops::merge_pdfs(
    &["file1.pdf", "file2.pdf"],
    "merged.pdf",
).expect("Failed to merge");

§Adding a Watermark

use pdfrs::pdf_ops;

pdf_ops::watermark_pdf(
    "input.pdf",
    "output.pdf",
    "CONFIDENTIAL",
    48.0,
    0.3,
).expect("Failed to add watermark");

§Parallel PDF Operations

use pdfrs::parallel;

// Merge multiple PDFs in parallel (loads inputs concurrently)
parallel::merge_pdfs_parallel(
    &["file1.pdf", "file2.pdf", "file3.pdf"],
    "merged.pdf",
).expect("Failed to merge");

// Extract text from multiple PDFs in parallel
let results = parallel::extract_text_parallel(&["doc1.pdf", "doc2.pdf"])
    .expect("Failed to extract text");
for (path, text) in results {
    println!("{}: {} characters", path, text.len());
}

§PDF Optimization Profiles

use pdfrs::optimization::{OptimizationProfile, OptimizedPdfGenerator};

// Create a web-optimized PDF (smallest file size)
let _pdf_gen = OptimizedPdfGenerator::new(OptimizationProfile::web());

// Or create a print-optimized PDF (highest quality)
let _pdf_gen = OptimizedPdfGenerator::new(OptimizationProfile::print());

// Or create a custom optimization profile
use pdfrs::optimization::{OptimizationSettings, CompressionLevel};
let settings = OptimizationSettings::new()
    .with_compression(CompressionLevel::High)
    .with_image_dpi(200);
let _pdf_gen = OptimizedPdfGenerator::new(OptimizationProfile::custom(settings));

Modules§

builder
Fluent builder API for ergonomic PDF creation
compression
Data compression and decompression utilities
elements
Document element types and Markdown parsing
image
Image loading, parsing, and PDF embedding
markdown
Markdown to PDF conversion utilities
optimization
PDF optimization profiles for different use cases
parallel
High-performance parallel PDF operations using Rayon
pdf
PDF document model, parsing, text extraction, and structural diff
pdf_generator
PDF generation from structured document elements
pdf_ops
PDF high-level operations module
security
PDF security and encryption support
streaming
Memory-efficient streaming PDF generation for large documents
table_renderer
Table rendering module for PDF generation