From 32de53132fc24ba6e8d40bde74b97df5ae8da6cd Mon Sep 17 00:00:00 2001 From: Isobel Redelmeier Date: Sun, 24 Mar 2019 00:35:50 -0700 Subject: [PATCH] Fix PDF output to actually apply expected CSS [Fixes #20] --- src/lib.rs | 6 ++++++ src/pdf_compiler.rs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d64850b..f1a3183 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,5 +13,11 @@ pub mod templates; mod html_compiler; mod pdf_compiler; +use filesystem::OsFileSystem; + pub use crate::html_compiler::HtmlCompiler; pub use crate::pdf_compiler::PdfCompiler; + +lazy_static! { + static ref FILE_SYSTEM: OsFileSystem = OsFileSystem::new(); +} diff --git a/src/pdf_compiler.rs b/src/pdf_compiler.rs index a78ce33..42409b6 100644 --- a/src/pdf_compiler.rs +++ b/src/pdf_compiler.rs @@ -2,23 +2,37 @@ use std::cell::RefCell; use std::io::Read; use std::rc::Rc; +use filesystem::{FileSystem, OsFileSystem, TempDir, TempFileSystem}; use wkhtmltopdf::PdfApplication; use crate::error::Result; use crate::resume::Resume; +use crate::FILE_SYSTEM; -#[derive(Default)] -pub struct PdfCompiler { +pub struct PdfCompiler<'a, T = OsFileSystem> { + fs: &'a T, compiler: Rc>>, } -impl PdfCompiler { +impl<'a> PdfCompiler<'a, OsFileSystem> { pub fn new() -> Self { Self { + fs: &FILE_SYSTEM, compiler: Rc::new(RefCell::new(None)), } } +} + +impl<'a, FS: FileSystem> PdfCompiler<'a, FS> { + pub fn fs<'b: 'a, T: FileSystem>(self, fs: &'b T) -> PdfCompiler<'a, T> { + PdfCompiler { + fs, + compiler: self.compiler, + } + } +} +impl<'a, FS: FileSystem + TempFileSystem> PdfCompiler<'a, FS> { pub fn compile(&self, resume: &Resume) -> Result> { let mut compiler = self.compiler.borrow_mut(); let mut builder = match *compiler { @@ -32,7 +46,14 @@ impl PdfCompiler { b } }; - let mut pdf = builder.build_from_html(&resume.html())?; + let temp_dir = self.fs.temp_dir("pdf")?; + let html_path = temp_dir.path().join("resume.html"); + let css_path = temp_dir.path().join("style.css"); + + self.fs.create_file(&html_path, &resume.html())?; + self.fs.create_file(&css_path, &resume.stylesheet())?; + + let mut pdf = builder.build_from_path(&html_path)?; let mut bytes: Vec = vec![]; pdf.read_to_end(&mut bytes)?; @@ -41,6 +62,12 @@ impl PdfCompiler { } } +impl<'a> Default for PdfCompiler<'a, OsFileSystem> { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod tests { use crate::resume::RawResume;