Allow unsafe settings for PDF renderer

This commit is contained in:
Jake Howard 2017-09-05 14:33:37 +01:00
parent b8fadf82a9
commit 5724fa8a05
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -3,7 +3,7 @@ use utils::result_override;
use std::error::Error; use std::error::Error;
use wkhtmltopdf::{PdfApplication, Result as WKResult, PageSize}; use wkhtmltopdf::{PdfApplication, Result as WKResult, PageSize, PdfBuilder};
fn pdf_result<T>(res: WKResult<T>) -> Result<T, String> { fn pdf_result<T>(res: WKResult<T>) -> Result<T, String> {
return match res { return match res {
@ -12,18 +12,26 @@ fn pdf_result<T>(res: WKResult<T>) -> Result<T, String> {
}; };
} }
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> { pub fn output(config: Config, html: String) -> Result<(), String> {
let output_location = &config.output["pdf"]; let output_location = &config.output["pdf"];
let mut pdf_app = let mut pdf_app =
try!(result_override(PdfApplication::new(), "Failed to create PDF Application".into())); try!(result_override(PdfApplication::new(), "Failed to create PDF Application".into()));
let pdfout_result = pdf_app let mut base_builder = pdf_app.builder();
.builder() let builder = create_builder(config.clone(), &mut base_builder);
.page_size(PageSize::A4)
.image_quality(100)
.title(&config.title)
.build_from_html(&html);
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( let output_file = try!(result_override(
pdfout.save(output_location), pdfout.save(output_location),
format!("Failed to output PDF file to {}", output_location.display()) format!("Failed to output PDF file to {}", output_location.display())