summaryrefslogtreecommitdiff
path: root/src/pdfgen.rs
diff options
context:
space:
mode:
authorjelemux <jeremias.weber@protonmail.com>2020-10-18 22:40:57 +0200
committerjelemux <jeremias.weber@protonmail.com>2020-10-18 22:40:57 +0200
commit84c2dab4200c37c818d83c95f85445ee00d83bf6 (patch)
tree134d9ec538306a6626d35e737035e03c8b3a69c5 /src/pdfgen.rs
downloadwasm-card-84c2dab4200c37c818d83c95f85445ee00d83bf6.tar.gz
wasm-card-84c2dab4200c37c818d83c95f85445ee00d83bf6.tar.bz2
initial commit
Diffstat (limited to 'src/pdfgen.rs')
-rw-r--r--src/pdfgen.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/pdfgen.rs b/src/pdfgen.rs
new file mode 100644
index 0000000..b8c7f7d
--- /dev/null
+++ b/src/pdfgen.rs
@@ -0,0 +1,48 @@
+use genpdf::Element as _;
+use genpdf::{elements, style, fonts};
+use crate::model::BCard;
+
+pub fn genpdf(bcard: BCard) -> Vec<u8> {
+ 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 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 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 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 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);
+
+ doc.set_title("BCard test");
+ doc.set_minimal_conformance();
+ doc.set_margins(10);
+ doc.set_line_spacing(1.25);
+
+ #[cfg(feature = "hyphenation")]
+ {
+ use hyphenation::Load;
+
+ doc.set_hyphenator(
+ hyphenation::Standard::from_embedded(hyphenation::Language::EnglishUS)
+ .expect("Failed to load hyphenation data"),
+ );
+ }
+
+ doc.push(
+ elements::Paragraph::new("genpdf Demo Document")
+ .aligned(elements::Alignment::Center)
+ .styled(style::Style::new().bold().with_font_size(20)),
+ );
+
+ // TODO fill doc with real data
+
+ let mut buf: Vec<u8> = Vec::new();
+ doc.render(&mut buf);
+ buf
+} \ No newline at end of file