Better structure PDF renderer
This commit is contained in:
parent
bf768aa135
commit
a51cc3ffdd
3 changed files with 39 additions and 8 deletions
|
@ -1,10 +1,8 @@
|
||||||
use wkhtmltopdf::PdfApplication;
|
use config::Config;
|
||||||
|
|
||||||
|
pub mod pdf;
|
||||||
|
|
||||||
pub fn output(html: String) {
|
pub fn output(config: Config, output: String) -> Result<String, String> {
|
||||||
let mut pdf_app = PdfApplication::new().expect("Failed to init PDF application");
|
pdf::output(config, output);
|
||||||
let mut pdfout = pdf_app.builder().build_from_html(&html).expect("failed to build pdf");
|
return Ok("".into());
|
||||||
|
|
||||||
pdfout.save("foo.pdf").expect("failed to save foo.pdf");
|
|
||||||
println!("generated PDF saved as: foo.pdf");
|
|
||||||
}
|
}
|
||||||
|
|
33
src/output/pdf/mod.rs
Normal file
33
src/output/pdf/mod.rs
Normal 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(());
|
||||||
|
}
|
|
@ -8,6 +8,6 @@ pub fn build(config: Config) -> Result<(), String> {
|
||||||
let input = try!(read_input_files(config.input.clone()));
|
let input = try!(read_input_files(config.input.clone()));
|
||||||
let raw_html = try!(build_input(config.clone(), input));
|
let raw_html = try!(build_input(config.clone(), input));
|
||||||
println!("{}", raw_html);
|
println!("{}", raw_html);
|
||||||
output(raw_html);
|
try!(output(config, raw_html));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue