From 5724fa8a05b98d8a4bbe6f641ff34eed22dd5ef0 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 5 Sep 2017 14:33:37 +0100 Subject: [PATCH] Allow unsafe settings for PDF renderer --- src/output/pdf/mod.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/output/pdf/mod.rs b/src/output/pdf/mod.rs index fc8f35c..8b60df0 100644 --- a/src/output/pdf/mod.rs +++ b/src/output/pdf/mod.rs @@ -3,7 +3,7 @@ use utils::result_override; use std::error::Error; -use wkhtmltopdf::{PdfApplication, Result as WKResult, PageSize}; +use wkhtmltopdf::{PdfApplication, Result as WKResult, PageSize, PdfBuilder}; fn pdf_result(res: WKResult) -> Result { return match res { @@ -12,18 +12,26 @@ fn pdf_result(res: WKResult) -> Result { }; } + +fn create_builder<'a>(config: Config, builder: &'a mut PdfBuilder) -> &'a mut PdfBuilder { + let mut safe_builder = builder.page_size(PageSize::A4).image_quality(100).title(&config.title); + unsafe { + return safe_builder + .global_setting("useCompression", "true") + .object_setting("useLocalLinks", "true") + .object_setting("useExternalLinks", "true") + .object_setting("pagesCount", "true"); + } +} + pub fn output(config: Config, html: String) -> Result<(), String> { let output_location = &config.output["pdf"]; let mut pdf_app = try!(result_override(PdfApplication::new(), "Failed to create PDF Application".into())); - let pdfout_result = pdf_app - .builder() - .page_size(PageSize::A4) - .image_quality(100) - .title(&config.title) - .build_from_html(&html); + let mut base_builder = pdf_app.builder(); + let builder = create_builder(config.clone(), &mut base_builder); - let mut pdfout = try!(pdf_result(pdfout_result)); + let mut pdfout = try!(pdf_result(builder.build_from_html(&html))); let output_file = try!(result_override( pdfout.save(output_location), format!("Failed to output PDF file to {}", output_location.display())