pdfrs/lib.rs
1//! # PDF-CLI Library
2//!
3//! A comprehensive Rust library for reading, writing, and manipulating PDF documents.
4//! This library provides functionality for:
5//!
6//! - **PDF Generation**: Create PDFs from markdown or raw text content
7//! - **PDF Parsing**: Extract text and structure from existing PDFs
8//! - **PDF Manipulation**: Merge, split, rotate, and reorder pages
9//! - **Image Support**: Embed JPEG, PNG, and BMP images in PDFs
10//! - **Annotations**: Add text, link, and highlight annotations
11//! - **Forms**: Create interactive PDF forms with text fields, checkboxes, radio buttons, and dropdowns
12//! - **Watermarks**: Add text or image watermarks to PDFs
13//! - **Metadata**: Manage document metadata including custom fields
14//! - **Security**: Add password protection and permissions to PDFs
15//!
16//! ## Quick Start
17//!
18//! ```rust
19//! use pdfrs::pdf_generator;
20//! use pdfrs::elements;
21//!
22//! // Parse markdown content
23//! let markdown = "# Hello World\n\nThis is a test document.";
24//! let elements = elements::parse_markdown(markdown);
25//!
26//! // Generate PDF
27//! let layout = pdf_generator::PageLayout::portrait();
28//! pdf_generator::create_pdf_from_elements_with_layout(
29//! "output.pdf",
30//! &elements,
31//! "Helvetica",
32//! 12.0,
33//! layout,
34//! ).expect("Failed to create PDF");
35//! ```
36//!
37//! ## Modules
38//!
39//! - [`pdf`]: PDF document parsing and text extraction
40//! - [`pdf_generator`]: PDF generation from elements and content streams
41//! - [`pdf_ops`]: High-level PDF operations (merge, split, watermark, etc.)
42//! - [`elements`]: Markdown parsing and element representation
43//! - [`markdown`]: Markdown to PDF conversion utilities
44//! - [`image`]: Image loading, parsing, and PDF embedding
45//! - [`compression`]: Data compression utilities
46//! - [`security`]: PDF security, encryption, and permission management
47//! - [`builder`]: Fluent builder API for ergonomic PDF creation
48//! - [`streaming`]: Memory-efficient streaming PDF generation for large documents
49//! - [`parallel`]: High-performance parallel PDF operations using Rayon
50//! - [`optimization`]: PDF optimization profiles for different use cases (web, print, archive, ebook)
51//!
52//! ## Examples
53//!
54//! ### Creating a PDF from Markdown
55//!
56//! ```rust,no_run
57//! use pdfrs::markdown;
58//!
59//! markdown::markdown_to_pdf_full(
60//! "input.md",
61//! "output.pdf",
62//! "Helvetica",
63//! 12.0,
64//! pdfrs::pdf_generator::PageOrientation::Portrait,
65//! ).expect("Failed to convert");
66//! ```
67//!
68//! ### Merging PDFs
69//!
70//! ```rust,no_run
71//! use pdfrs::pdf_ops;
72//!
73//! pdf_ops::merge_pdfs(
74//! &["file1.pdf", "file2.pdf"],
75//! "merged.pdf",
76//! ).expect("Failed to merge");
77//! ```
78//!
79//! ### Adding a Watermark
80//!
81//! ```rust,no_run
82//! use pdfrs::pdf_ops;
83//!
84//! pdf_ops::watermark_pdf(
85//! "input.pdf",
86//! "output.pdf",
87//! "CONFIDENTIAL",
88//! 48.0,
89//! 0.3,
90//! ).expect("Failed to add watermark");
91//! ```
92//!
93//! ### Parallel PDF Operations
94//!
95//! ```rust,no_run
96//! use pdfrs::parallel;
97//!
98//! // Merge multiple PDFs in parallel (loads inputs concurrently)
99//! parallel::merge_pdfs_parallel(
100//! &["file1.pdf", "file2.pdf", "file3.pdf"],
101//! "merged.pdf",
102//! ).expect("Failed to merge");
103//!
104//! // Extract text from multiple PDFs in parallel
105//! let results = parallel::extract_text_parallel(&["doc1.pdf", "doc2.pdf"])
106//! .expect("Failed to extract text");
107//! for (path, text) in results {
108//! println!("{}: {} characters", path, text.len());
109//! }
110//! ```
111//!
112//! ### PDF Optimization Profiles
113//!
114//! ```rust
115//! use pdfrs::optimization::{OptimizationProfile, OptimizedPdfGenerator};
116//!
117//! // Create a web-optimized PDF (smallest file size)
118//! let _pdf_gen = OptimizedPdfGenerator::new(OptimizationProfile::web());
119//!
120//! // Or create a print-optimized PDF (highest quality)
121//! let _pdf_gen = OptimizedPdfGenerator::new(OptimizationProfile::print());
122//!
123//! // Or create a custom optimization profile
124//! use pdfrs::optimization::{OptimizationSettings, CompressionLevel};
125//! let settings = OptimizationSettings::new()
126//! .with_compression(CompressionLevel::High)
127//! .with_image_dpi(200);
128//! let _pdf_gen = OptimizedPdfGenerator::new(OptimizationProfile::custom(settings));
129//! ```
130
131pub mod builder;
132pub mod compression;
133pub mod elements;
134pub mod image;
135pub mod markdown;
136pub mod optimization;
137pub mod parallel;
138pub mod pdf;
139pub mod pdf_generator;
140pub mod pdf_ops;
141pub mod security;
142pub mod streaming;
143pub mod table_renderer;
144
145#[cfg(test)]
146mod tests {
147 use crate::markdown::markdown_to_text;
148
149 #[test]
150 fn test_markdown_to_text() {
151 let markdown = "# Header\n\nThis is **bold** and *italic* text.\n\n- Item 1\n- Item 2";
152 let expected = "Header\nThis is bold and italic text.\n• Item 1\n• Item 2\n";
153 let result = markdown_to_text(markdown);
154 assert_eq!(result, expected);
155 }
156
157 #[test]
158 fn test_markdown_table_to_text() {
159 let markdown = "| Name | Age |\n|------|-----|\n| John | 25 |\n| Jane | 30 |";
160 let expected = "Name Age \n------ ----- \nJohn 25 \nJane 30 \n";
161 let result = markdown_to_text(markdown);
162 assert_eq!(result, expected);
163 }
164
165 #[test]
166 fn test_markdown_code_blocks() {
167 let markdown = "Here is some code:\n\n```rust\nfn main() {\n println!(\"Hello\");\n}\n```\n\nMore text.";
168 let expected =
169 "Here is some code:\n\nfn main() {\n println!(\"Hello\");\n}\n\nMore text.\n";
170 let result = markdown_to_text(markdown);
171 assert_eq!(result, expected);
172 }
173}