html2pdf_secure/
options.rs

1//! Configuration options for PDF generation.
2
3use serde::{Deserialize, Serialize};
4
5/// Options for PDF generation from HTML.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct PdfOptions {
8    /// Page format (e.g., "A4", "Letter", "Legal")
9    pub page_format: PageFormat,
10    
11    /// Page orientation
12    pub orientation: PageOrientation,
13    
14    /// Page margins in inches
15    pub margins: PageMargins,
16    
17    /// Whether to print background graphics
18    pub print_background: bool,
19    
20    /// Scale factor for the page (0.1 to 2.0)
21    pub scale: f64,
22    
23    /// Whether to prefer CSS page size
24    pub prefer_css_page_size: bool,
25    
26    /// Custom page width in inches (overrides page_format if set)
27    pub custom_width: Option<f64>,
28    
29    /// Custom page height in inches (overrides page_format if set)
30    pub custom_height: Option<f64>,
31    
32    /// Timeout for page loading in seconds
33    pub timeout_seconds: u64,
34    
35    /// Whether to wait for network idle before generating PDF
36    pub wait_for_network_idle: bool,
37    
38    /// Additional wait time in milliseconds after page load
39    pub additional_wait_ms: u64,
40}
41
42impl Default for PdfOptions {
43    fn default() -> Self {
44        Self {
45            page_format: PageFormat::A4,
46            orientation: PageOrientation::Portrait,
47            margins: PageMargins::default(),
48            print_background: true,
49            scale: 1.0,
50            prefer_css_page_size: false,
51            custom_width: None,
52            custom_height: None,
53            timeout_seconds: 30,
54            wait_for_network_idle: true,
55            additional_wait_ms: 1000,
56        }
57    }
58}
59
60/// Page format options.
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub enum PageFormat {
63    A4,
64    A3,
65    A5,
66    Letter,
67    Legal,
68    Tabloid,
69    Ledger,
70}
71
72impl PageFormat {
73    /// Get the width and height in inches for the page format.
74    pub fn dimensions(&self) -> (f64, f64) {
75        match self {
76            PageFormat::A4 => (8.27, 11.7),
77            PageFormat::A3 => (11.7, 16.5),
78            PageFormat::A5 => (5.8, 8.3),
79            PageFormat::Letter => (8.5, 11.0),
80            PageFormat::Legal => (8.5, 14.0),
81            PageFormat::Tabloid => (11.0, 17.0),
82            PageFormat::Ledger => (17.0, 11.0),
83        }
84    }
85}
86
87/// Page orientation options.
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub enum PageOrientation {
90    Portrait,
91    Landscape,
92}
93
94/// Page margins configuration.
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct PageMargins {
97    pub top: f64,
98    pub bottom: f64,
99    pub left: f64,
100    pub right: f64,
101}
102
103impl Default for PageMargins {
104    fn default() -> Self {
105        Self {
106            top: 1.0,
107            bottom: 1.0,
108            left: 1.0,
109            right: 1.0,
110        }
111    }
112}
113
114impl PageMargins {
115    /// Create new margins with the same value for all sides.
116    pub fn uniform(margin: f64) -> Self {
117        Self {
118            top: margin,
119            bottom: margin,
120            left: margin,
121            right: margin,
122        }
123    }
124    
125    /// Create new margins with different values for vertical and horizontal.
126    pub fn symmetric(vertical: f64, horizontal: f64) -> Self {
127        Self {
128            top: vertical,
129            bottom: vertical,
130            left: horizontal,
131            right: horizontal,
132        }
133    }
134}