Skip to content

Commit 0f13558

Browse files
authored
fix: change pdf params env variables to string (#24)
1 parent 161122a commit 0f13558

File tree

3 files changed

+120
-48
lines changed

3 files changed

+120
-48
lines changed

src/config.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -91,76 +91,76 @@ pub struct Chrome {
9191
pub pdf_landscape: bool,
9292
#[env_config(
9393
name = "ZO_PDF_DISPLAY_HEADER_FOOTER",
94-
default = false,
95-
help = "Display header and footer in the PDF"
94+
default = "",
95+
help = "Display header and footer in the PDF. Set to 'true' or 'false'. When empty, uses browser default"
9696
)]
97-
pub pdf_display_header_footer: bool,
97+
pub pdf_display_header_footer: String,
9898
#[env_config(
9999
name = "ZO_PDF_PRINT_BACKGROUND",
100-
default = false,
101-
help = "Print background graphics in the PDF"
100+
default = "",
101+
help = "Print background graphics in the PDF. Set to 'true' or 'false'. When empty, uses browser default"
102102
)]
103-
pub pdf_print_background: bool,
103+
pub pdf_print_background: String,
104104
#[env_config(
105105
name = "ZO_PDF_SCALE",
106-
default = 1.0,
107-
help = "Scale of the webpage rendering. Default is 1.0"
106+
default = "",
107+
help = "Scale of the webpage rendering (e.g., 1.0 for 100%). When empty, uses browser default"
108108
)]
109-
pub pdf_scale: f64,
109+
pub pdf_scale: String,
110110
#[env_config(
111111
name = "ZO_PDF_PAPER_WIDTH",
112-
default = 8.5,
113-
help = "Paper width in inches. Default is 8.5 inches (US Letter)"
112+
default = "",
113+
help = "Paper width in inches (e.g., 8.5 for US Letter). When empty, uses browser default"
114114
)]
115-
pub pdf_paper_width: f64,
115+
pub pdf_paper_width: String,
116116
#[env_config(
117117
name = "ZO_PDF_PAPER_HEIGHT",
118-
default = 11.0,
119-
help = "Paper height in inches. Default is 11 inches (US Letter)"
118+
default = "",
119+
help = "Paper height in inches (e.g., 11.0 for US Letter). When empty, uses browser default"
120120
)]
121-
pub pdf_paper_height: f64,
121+
pub pdf_paper_height: String,
122122
#[env_config(
123123
name = "ZO_PDF_MARGIN_TOP",
124-
default = 0.4,
125-
help = "Top margin in inches. Default is 0.4 inches (~1cm)"
124+
default = "",
125+
help = "Top margin in inches. When empty, uses browser default"
126126
)]
127-
pub pdf_margin_top: f64,
127+
pub pdf_margin_top: String,
128128
#[env_config(
129129
name = "ZO_PDF_MARGIN_BOTTOM",
130-
default = 0.4,
131-
help = "Bottom margin in inches. Default is 0.4 inches (~1cm)"
130+
default = "",
131+
help = "Bottom margin in inches. When empty, uses browser default"
132132
)]
133-
pub pdf_margin_bottom: f64,
133+
pub pdf_margin_bottom: String,
134134
#[env_config(
135135
name = "ZO_PDF_MARGIN_LEFT",
136-
default = 0.4,
137-
help = "Left margin in inches. Default is 0.4 inches (~1cm)"
136+
default = "",
137+
help = "Left margin in inches. When empty, uses browser default"
138138
)]
139-
pub pdf_margin_left: f64,
139+
pub pdf_margin_left: String,
140140
#[env_config(
141141
name = "ZO_PDF_MARGIN_RIGHT",
142-
default = 0.4,
143-
help = "Right margin in inches. Default is 0.4 inches (~1cm)"
142+
default = "",
143+
help = "Right margin in inches. When empty, uses browser default"
144144
)]
145-
pub pdf_margin_right: f64,
145+
pub pdf_margin_right: String,
146146
#[env_config(
147147
name = "ZO_PDF_PREFER_CSS_PAGE_SIZE",
148-
default = false,
149-
help = "Prefer page size as defined by CSS. If false, content will be scaled to fit paper size"
148+
default = "",
149+
help = "Prefer page size as defined by CSS. Set to 'true' or 'false'. When empty, uses browser default"
150150
)]
151-
pub pdf_prefer_css_page_size: bool,
151+
pub pdf_prefer_css_page_size: String,
152152
#[env_config(
153153
name = "ZO_PDF_GENERATE_TAGGED_PDF",
154-
default = false,
155-
help = "Generate tagged (accessible) PDF for screen readers and assistive technologies"
154+
default = "",
155+
help = "Generate tagged (accessible) PDF for screen readers. Set to 'true' or 'false'. When empty, uses browser default"
156156
)]
157-
pub pdf_generate_tagged_pdf: bool,
157+
pub pdf_generate_tagged_pdf: String,
158158
#[env_config(
159159
name = "ZO_PDF_GENERATE_DOCUMENT_OUTLINE",
160-
default = false,
161-
help = "Embed the document outline into the PDF for navigation"
160+
default = "",
161+
help = "Embed the document outline into the PDF. Set to 'true' or 'false'. When empty, uses browser default"
162162
)]
163-
pub pdf_generate_document_outline: bool,
163+
pub pdf_generate_document_outline: String,
164164
}
165165

166166
#[derive(EnvConfig)]

src/lib.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -462,20 +462,34 @@ pub async fn generate_report(
462462
// Convert the page into pdf
463463
let attachment_data = match report_type {
464464
ReportType::PDF => {
465+
// Helper function to convert string to Option<bool>
466+
let parse_bool_opt = |s: &str| -> Option<bool> {
467+
match s.trim().to_lowercase().as_str() {
468+
"true" => Some(true),
469+
"false" => Some(false),
470+
_ => None,
471+
}
472+
};
473+
474+
// Helper function to parse string to Option<f64>
475+
let parse_f64_opt = |s: &str| -> Option<f64> { s.trim().parse::<f64>().ok() };
476+
465477
page.pdf(PrintToPdfParams {
466478
landscape: Some(CONFIG.chrome.pdf_landscape),
467-
display_header_footer: Some(CONFIG.chrome.pdf_display_header_footer),
468-
print_background: Some(CONFIG.chrome.pdf_print_background),
469-
scale: Some(CONFIG.chrome.pdf_scale),
470-
paper_width: Some(CONFIG.chrome.pdf_paper_width),
471-
paper_height: Some(CONFIG.chrome.pdf_paper_height),
472-
margin_top: Some(CONFIG.chrome.pdf_margin_top),
473-
margin_bottom: Some(CONFIG.chrome.pdf_margin_bottom),
474-
margin_left: Some(CONFIG.chrome.pdf_margin_left),
475-
margin_right: Some(CONFIG.chrome.pdf_margin_right),
476-
prefer_css_page_size: Some(CONFIG.chrome.pdf_prefer_css_page_size),
477-
generate_tagged_pdf: Some(CONFIG.chrome.pdf_generate_tagged_pdf),
478-
generate_document_outline: Some(CONFIG.chrome.pdf_generate_document_outline),
479+
display_header_footer: parse_bool_opt(&CONFIG.chrome.pdf_display_header_footer),
480+
print_background: parse_bool_opt(&CONFIG.chrome.pdf_print_background),
481+
scale: parse_f64_opt(&CONFIG.chrome.pdf_scale),
482+
paper_width: parse_f64_opt(&CONFIG.chrome.pdf_paper_width),
483+
paper_height: parse_f64_opt(&CONFIG.chrome.pdf_paper_height),
484+
margin_top: parse_f64_opt(&CONFIG.chrome.pdf_margin_top),
485+
margin_bottom: parse_f64_opt(&CONFIG.chrome.pdf_margin_bottom),
486+
margin_left: parse_f64_opt(&CONFIG.chrome.pdf_margin_left),
487+
margin_right: parse_f64_opt(&CONFIG.chrome.pdf_margin_right),
488+
prefer_css_page_size: parse_bool_opt(&CONFIG.chrome.pdf_prefer_css_page_size),
489+
generate_tagged_pdf: parse_bool_opt(&CONFIG.chrome.pdf_generate_tagged_pdf),
490+
generate_document_outline: parse_bool_opt(
491+
&CONFIG.chrome.pdf_generate_document_outline,
492+
),
479493
..Default::default()
480494
})
481495
.await?

src/main.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,64 @@ async fn main() -> Result<(), anyhow::Error> {
4343
panic!("Report User email and password must be specified");
4444
}
4545

46+
// Log configured Chrome PDF parameters
47+
log::info!("Chrome PDF Configuration:");
48+
log::info!(" pdf_landscape: {}", CONFIG.chrome.pdf_landscape);
49+
50+
let display_opt = |s: &str| {
51+
if s.is_empty() {
52+
"None (browser default)".to_string()
53+
} else {
54+
format!("Some({})", s)
55+
}
56+
};
57+
58+
log::info!(
59+
" pdf_display_header_footer: {}",
60+
display_opt(&CONFIG.chrome.pdf_display_header_footer)
61+
);
62+
log::info!(
63+
" pdf_print_background: {}",
64+
display_opt(&CONFIG.chrome.pdf_print_background)
65+
);
66+
log::info!(" pdf_scale: {}", display_opt(&CONFIG.chrome.pdf_scale));
67+
log::info!(
68+
" pdf_paper_width: {}",
69+
display_opt(&CONFIG.chrome.pdf_paper_width)
70+
);
71+
log::info!(
72+
" pdf_paper_height: {}",
73+
display_opt(&CONFIG.chrome.pdf_paper_height)
74+
);
75+
log::info!(
76+
" pdf_margin_top: {}",
77+
display_opt(&CONFIG.chrome.pdf_margin_top)
78+
);
79+
log::info!(
80+
" pdf_margin_bottom: {}",
81+
display_opt(&CONFIG.chrome.pdf_margin_bottom)
82+
);
83+
log::info!(
84+
" pdf_margin_left: {}",
85+
display_opt(&CONFIG.chrome.pdf_margin_left)
86+
);
87+
log::info!(
88+
" pdf_margin_right: {}",
89+
display_opt(&CONFIG.chrome.pdf_margin_right)
90+
);
91+
log::info!(
92+
" pdf_prefer_css_page_size: {}",
93+
display_opt(&CONFIG.chrome.pdf_prefer_css_page_size)
94+
);
95+
log::info!(
96+
" pdf_generate_tagged_pdf: {}",
97+
display_opt(&CONFIG.chrome.pdf_generate_tagged_pdf)
98+
);
99+
log::info!(
100+
" pdf_generate_document_outline: {}",
101+
display_opt(&CONFIG.chrome.pdf_generate_document_outline)
102+
);
103+
46104
let haddr: SocketAddr = if CONFIG.http.ipv6_enabled {
47105
format!("[::]:{}", CONFIG.http.port).parse()?
48106
} else {

0 commit comments

Comments
 (0)