Skip to content

Commit

Permalink
feat: merge attached pdfs to the invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
lajp committed Aug 11, 2024
1 parent e77e8a2 commit 510b88a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/mailgun/invoices.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
use super::MailgunClient;
use crate::api::invoices::Invoice;
use crate::error::Error;
use crate::merge::merge_pdf;
use typst::model::Document;

impl MailgunClient {
#[allow(dead_code)]
pub async fn send_mail(self, invoice: Invoice) -> Result<(), Error> {
let document: Document = invoice.to_owned().try_into()?;
let pdf = typst_pdf::pdf(&document, typst::foundations::Smart::Auto, None);

let mut pdfs = vec![pdf];
pdfs.extend_from_slice(
invoice
.attachments
.clone()
.into_iter()
.map(|a| a.bytes)
.collect::<Vec<_>>()
.as_slice(),
);

let pdf = merge_pdf(pdfs)?;

let invoice_recipient = format!("{} <{}>", invoice.recipient_name, invoice.recipient_email);
let form = reqwest::multipart::Form::new()
.text("from", self.from)
.text("to", self.default_to)
.text("cc", invoice_recipient)
.text("subject", format!("Uusi lasku {}", invoice.recipient_name))
.text("html", format!("Uusi lasku {}", invoice.recipient_name))
.text(
"subject",
format!("Uusi lasku, lähettäjä {}", invoice.recipient_name),
)
.text(
"html",
format!("Uusi lasku, lähettäjä {}", invoice.recipient_name),
)
.part(
"attachment",
reqwest::multipart::Part::bytes(pdf).file_name("invoice.pdf"),
Expand Down
1 change: 0 additions & 1 deletion src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::BTreeMap;

use lopdf::{Document, Object, ObjectId};

#[allow(dead_code)]
// Mostly copied from https://github.com/J-F-Liu/lopdf/blob/master/README.md merge example
pub fn merge_pdf(documents: Vec<Vec<u8>>) -> Result<Vec<u8>, Error> {
let documents = documents
Expand Down

0 comments on commit 510b88a

Please sign in to comment.