From 748d8a1f192e58b32aeebb741ad7e44a8edb246c Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 23 Mar 2026 11:19:20 +0100 Subject: [PATCH] Remove PDF generation Revert PDF generation feature - will use external tool for HTML to PDF conversion. --- Cargo.toml | 1 - src/main.rs | 62 ++++++------- src/pdf.rs | 244 ---------------------------------------------------- 3 files changed, 26 insertions(+), 281 deletions(-) delete mode 100644 src/pdf.rs diff --git a/Cargo.toml b/Cargo.toml index 832818b..6c6e527 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,4 @@ edition = "2021" askama = "0.15.5" chrono = "0.4.44" csv = "1.4.0" -printpdf = "0.9.1" serde = "1.0.228" diff --git a/src/main.rs b/src/main.rs index ef26c65..98da45f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,6 @@ use std::fs; use std::path::Path; mod invoice_generator; -mod pdf; use invoice_generator::{group_by_customer, read_csv_file, Customer}; @@ -15,43 +14,43 @@ fn fmt(v: f64) -> String { } #[derive(Clone)] -pub struct ProductSummary { - pub name: String, - pub volume: String, - pub amount: String, - pub avg_price: String, +struct ProductSummary { + name: String, + volume: String, + amount: String, + avg_price: String, } #[derive(Clone)] -pub struct Summary { - pub total_volume: String, - pub grand_total: String, - pub products: Vec, +struct Summary { + total_volume: String, + grand_total: String, + products: Vec, } #[derive(Clone)] -pub struct CardData { - pub card_number: String, - pub transactions: Vec, - pub total_amount: String, - pub total_volume: String, +struct CardData { + card_number: String, + transactions: Vec, + total_amount: String, + total_volume: String, } #[derive(Clone)] -pub struct FormattedTransaction { - pub date: String, - pub quality_name: String, - pub price: String, - pub volume: String, - pub amount: String, - pub receipt: String, +struct FormattedTransaction { + date: String, + quality_name: String, + price: String, + volume: String, + amount: String, + receipt: String, } #[derive(Clone)] -pub struct PreparedCustomer { - pub customer_number: String, - pub cards: Vec, - pub summary: Summary, +struct PreparedCustomer { + customer_number: String, + cards: Vec, + summary: Summary, } impl PreparedCustomer { @@ -213,7 +212,7 @@ fn main() -> Result<(), Box> { for (customer_num, customer) in customers { let prepared = PreparedCustomer::from_customer(customer); let customer_html = CustomerTemplate { - customer: prepared.clone(), + customer: prepared, period: period.clone(), generated_date: generated_date.clone(), } @@ -222,15 +221,6 @@ fn main() -> Result<(), Box> { let filename = format!("customer_{}.html", customer_num); fs::write(output_dir.join(&filename), customer_html)?; println!("Genererade {}", filename); - - let pdf_filename = format!("customer_{}.pdf", customer_num); - pdf::generate_customer_pdf( - &prepared, - &period, - &generated_date, - &output_dir.join(&pdf_filename), - )?; - println!("Genererade {}", pdf_filename); } println!( diff --git a/src/pdf.rs b/src/pdf.rs deleted file mode 100644 index 4d557e6..0000000 --- a/src/pdf.rs +++ /dev/null @@ -1,244 +0,0 @@ -use printpdf::*; -use std::collections::BTreeMap; -use std::fs::File; -use std::io::BufWriter; -use std::path::Path; - -use crate::PreparedCustomer; - -pub fn generate_customer_pdf( - customer: &PreparedCustomer, - period: &str, - generated_date: &str, - output_path: &Path, -) -> Result<(), Box> { - let html = format!( - r#" - - - - Faktura - Kund {} - - - -
-
-

Faktura - Kund {}

-
Period: {}
-
-
-
Genererad: {}
-
-
- -
-

Sammanfattning

- - - - - - - - - - - {} - - - - - - - -
ProduktVolym (L)BeloppSnittpris/L
Totalt{}{} Kr
-
- - {} - -"#, - customer.customer_number, - customer.customer_number, - period, - generated_date, - generate_product_rows(&customer.summary.products), - customer.summary.total_volume, - customer.summary.grand_total, - generate_card_sections(&customer.cards), - ); - - let mut warnings = Vec::new(); - let doc = PdfDocument::from_html( - &html, - &BTreeMap::new(), - &BTreeMap::new(), - &GeneratePdfOptions::default(), - &mut warnings, - )?; - - let file = File::create(output_path)?; - let mut writer = BufWriter::new(file); - let mut warnings = Vec::new(); - doc.save_writer(&mut writer, &PdfSaveOptions::default(), &mut warnings); - - Ok(()) -} - -fn generate_product_rows(products: &[crate::ProductSummary]) -> String { - products - .iter() - .map(|p| { - format!( - r#" - {} - {} - {} Kr - {} Kr - "#, - p.name, p.volume, p.amount, p.avg_price - ) - }) - .collect::>() - .join("\n ") -} - -fn generate_card_sections(cards: &[crate::CardData]) -> String { - cards - .iter() - .map(|card| { - format!( - r#"
-
Kort: {} | {} transaktioner
-
Summa: {} Kr | Volym: {} L
- - - - - - - - - - - - - {} - - - - - - - -
DatumProduktPris/LVolymBeloppKvitto
Kortsumma{}{}
-
"#, - card.card_number, - card.transactions.len(), - card.total_amount, - card.total_volume, - generate_transaction_rows(&card.transactions), - card.total_volume, - card.total_amount, - ) - }) - .collect::>() - .join("\n\n ") -} - -fn generate_transaction_rows(transactions: &[crate::FormattedTransaction]) -> String { - transactions - .iter() - .map(|tx| { - format!( - r#" - {} - {} - {} - {} - {} - {} - "#, - tx.date, tx.quality_name, tx.price, tx.volume, tx.amount, tx.receipt - ) - }) - .collect::>() - .join("\n ") -}