use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PdfConfig {
pub page_size: PageSize,
pub orientation: Orientation,
pub margins: Margins,
pub scale: f64,
pub print_background: bool,
pub display_header_footer: bool,
pub header_template: Option<String>,
pub footer_template: Option<String>,
pub prefer_css_page_size: bool,
pub timeout_seconds: u64,
}
impl Default for PdfConfig {
fn default() -> Self {
Self {
page_size: PageSize::A4,
orientation: Orientation::Portrait,
margins: Margins::default(),
scale: 1.0,
print_background: true,
display_header_footer: false,
header_template: None,
footer_template: None,
prefer_css_page_size: false,
timeout_seconds: 30,
}
}
}
impl PdfConfig {
pub fn validate(&self) -> crate::Result<()> {
if !(0.1..=2.0).contains(&self.scale) {
return Err(crate::Error::InvalidConfig(
format!("Scale must be between 0.1 and 2.0, got {}", self.scale)
));
}
if self.timeout_seconds == 0 {
return Err(crate::Error::InvalidConfig(
"Timeout must be greater than 0".to_string()
));
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum PageSize {
A4,
Letter,
Legal,
Tabloid,
Custom {
width: f64,
height: f64,
},
}
impl PageSize {
pub fn dimensions_inches(&self) -> (f64, f64) {
match self {
PageSize::A4 => (8.27, 11.69),
PageSize::Letter => (8.5, 11.0),
PageSize::Legal => (8.5, 14.0),
PageSize::Tabloid => (11.0, 17.0),
PageSize::Custom { width, height } => (*width, *height),
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum Orientation {
Portrait,
Landscape,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Margins {
pub top: f64,
pub bottom: f64,
pub left: f64,
pub right: f64,
}
impl Default for Margins {
fn default() -> Self {
Self {
top: 0.4,
bottom: 0.4,
left: 0.4,
right: 0.4,
}
}
}
impl Margins {
pub fn uniform(margin: f64) -> Self {
Self {
top: margin,
bottom: margin,
left: margin,
right: margin,
}
}
pub fn none() -> Self {
Self {
top: 0.0,
bottom: 0.0,
left: 0.0,
right: 0.0,
}
}
}
#[derive(Debug, Clone)]
pub struct ChromeConfig {
pub binary_path: Option<String>,
pub no_sandbox: bool,
pub extra_args: Vec<String>,
pub headless: bool,
pub connection_timeout: u64,
}
impl Default for ChromeConfig {
fn default() -> Self {
Self {
binary_path: None,
no_sandbox: false,
extra_args: vec![],
headless: true,
connection_timeout: 10,
}
}
}