From f267433455dbc69e771b06dd7c433c90ad978d33 Mon Sep 17 00:00:00 2001 From: jelemux Date: Tue, 20 Oct 2020 01:09:47 +0200 Subject: made it work --- Cargo.toml | 5 ++++- src/pdfgen.rs | 23 ++++++++++++++--------- src/view.rs | 8 ++++---- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2727e08..e22fc82 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,7 @@ yew = "0.17.4" # react-like frontend framework wasm-bindgen = "0.2.68" # enables interaction with js chrono = "0.4.19" # date & time genpdf = { path = "../genpdf-rs" } # pdf generation -js-sys = "0.3.45" # raw bindings to js global apis \ No newline at end of file +js-sys = "0.3.45" # raw bindings to js global apis +printpdf = "0.3.3" +web-sys = "0.3.45" +base64 = "0.13.0" \ No newline at end of file diff --git a/src/pdfgen.rs b/src/pdfgen.rs index 410bb99..0bca467 100644 --- a/src/pdfgen.rs +++ b/src/pdfgen.rs @@ -2,19 +2,24 @@ use genpdf::Element as _; use genpdf::{elements, style, fonts}; pub fn genpdf() -> Vec { - let regular_bytes = include_bytes!("../fonts/fira-sans.regular.ttf"); - let regular_font_data = fonts::FontData::new(regular_bytes.to_vec(), None).expect("font data should be correct"); + let regular_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-Regular.ttf"); + let regular_font_data = fonts::FontData::new(regular_bytes.to_vec(), Some(printpdf::BuiltinFont::Helvetica)).expect("font data should be correct"); - let bold_bytes = include_bytes!("../fonts/fira-sans.bold.ttf"); - let bold_font_data = fonts::FontData::new(bold_bytes.to_vec(), None).expect("font data should be correct"); + let bold_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-Bold.ttf"); + let bold_font_data = fonts::FontData::new(bold_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaBold)).expect("font data should be correct"); - let italic_bytes = include_bytes!("../fonts/fira-sans.italic.ttf"); - let italic_font_data = fonts::FontData::new(italic_bytes.to_vec(), None).expect("font data should be correct"); + let italic_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-Italic.ttf"); + let italic_font_data = fonts::FontData::new(italic_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaOblique)).expect("font data should be correct"); - let bold_italic_bytes = include_bytes!("../fonts/fira-sans.bold-italic.ttf"); - let bold_italic_font_data = fonts::FontData::new(bold_italic_bytes.to_vec(), None).expect("font data should be correct"); + let bold_italic_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf"); + let bold_italic_font_data = fonts::FontData::new(bold_italic_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaBoldOblique)).expect("font data should be correct"); - let font_family = fonts::FontFamily{ regular: regular_font_data, bold: bold_font_data, italic: italic_font_data, bold_italic: bold_italic_font_data }; + let font_family = fonts::FontFamily{ + regular: regular_font_data, + bold: bold_font_data, + italic: italic_font_data, + bold_italic: bold_italic_font_data + }; let mut doc = genpdf::Document::new(font_family); diff --git a/src/view.rs b/src/view.rs index 9b95691..38cfa58 100644 --- a/src/view.rs +++ b/src/view.rs @@ -23,10 +23,10 @@ impl Component for Form { } fn view(&self) -> Html { - let pdf_raw = /*include_bytes!("../demo.pdf"); // this works */pdfgen::genpdf(); // this doesn't work - let pdf = std::str::from_utf8(&pdf_raw).expect("should be able to convert to string"); - let uri_component: String = js_sys::encode_uri_component(pdf).into(); - let href = format!{"data:application/pdf;charset=utf-8,{}", uri_component }; + let raw = pdfgen::genpdf(); + let data = base64::encode(&raw); + let uri_component: String = js_sys::encode_uri_component(&data).into(); + let href = format!{"data:application/pdf;base64,{}", uri_component }; html!{ { "Download PDF" } -- cgit v1.2.3