Skip to content

Commit

Permalink
Fix PDF output to actually apply expected CSS
Browse files Browse the repository at this point in the history
[Fixes #20]
  • Loading branch information
iredelmeier committed Mar 24, 2019
1 parent 0fe885f commit 32de531
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
35 changes: 31 additions & 4 deletions src/pdf_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Option<PdfApplication>>>,
}

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<Vec<u8>> {
let mut compiler = self.compiler.borrow_mut();
let mut builder = match *compiler {
Expand All @@ -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<u8> = vec![];

pdf.read_to_end(&mut bytes)?;
Expand All @@ -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;
Expand Down

0 comments on commit 32de531

Please sign in to comment.