Better structure PDF renderer

This commit is contained in:
Jake Howard 2017-08-28 19:01:18 +01:00
parent bf768aa135
commit a51cc3ffdd
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 39 additions and 8 deletions

View file

@ -1,10 +1,8 @@
use wkhtmltopdf::PdfApplication;
use config::Config;
pub mod pdf;
pub fn output(html: String) {
let mut pdf_app = PdfApplication::new().expect("Failed to init PDF application");
let mut pdfout = pdf_app.builder().build_from_html(&html).expect("failed to build pdf");
pdfout.save("foo.pdf").expect("failed to save foo.pdf");
println!("generated PDF saved as: foo.pdf");
pub fn output(config: Config, output: String) -> Result<String, String> {
pdf::output(config, output);
return Ok("".into());
}

33
src/output/pdf/mod.rs Normal file
View file

@ -0,0 +1,33 @@
use config::Config;
use utils::result_override;
use std::error::Error;
use wkhtmltopdf::{PdfApplication, Result as WKResult, PageSize};
fn pdf_result<T>(res: WKResult<T>) -> Result<T, String> {
return match res {
Ok(out) => Ok(out),
Err(err) => Err(err.description().into()),
};
}
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 pdfout = try!(pdf_result(pdfout_result));
let output_file = try!(result_override(
pdfout.save(output_location),
format!("Failed to output PDF file to {}", output_location.display())
));
debug_assert!(output_file.metadata().unwrap().is_file());
return Ok(());
}

View file

@ -8,6 +8,6 @@ pub fn build(config: Config) -> Result<(), String> {
let input = try!(read_input_files(config.input.clone()));
let raw_html = try!(build_input(config.clone(), input));
println!("{}", raw_html);
output(raw_html);
try!(output(config, raw_html));
return Ok(());
}