Files
beer-inventory/app/utils/pdf/pdf.generate.server.ts
T
kennyboy55 e4c93eb024
Build dev docker image / build (push) Successful in 34s
Build dev docker image / release (push) Successful in 1s
AI Changes: add user management and hashed passwords, admin dashboard, report fixes
2026-08-01 11:47:21 +02:00

53 lines
1.7 KiB
TypeScript

import { jsPDF } from 'jspdf'
import {applyPlugin} from 'jspdf-autotable'
import { ContainerWithDrinkAndHistory, DrinkComposite, containerTypeToString } from '~/models/types';
import { volume } from '../conversions';
export async function createPdf(filename: string, reportId: string, dateStart: string, dateEnd: string, containers: ContainerWithDrinkAndHistory[]){
applyPlugin(jsPDF);
let body : string[][] = [];
let total : number = 0;
for(let container of containers){
let price = Math.round((container.price * container.checkouts.length) * 100) / 100;
total += price;
let bodyRow = [
container.drink.name + " - " + containerTypeToString(container.type) + " " + volume(container.volume),
"€" + container.price,
container.checkouts.length.toString(),
"€" + price];
body.push(bodyRow);
}
const doc = new jsPDF();
doc.setCreationDate(new Date(Date.now()));
doc.setFontSize(20);
doc.text("K-FRIDGE", 15, 25);
doc.setFontSize(12);
doc.text("Report #" + reportId, 160, 25);
doc.setFontSize(10);
doc.text("Selected date range: " + dateStart.slice(0, -8).replace("T", "") + " - " + dateEnd.slice(0,-8).replace("T", ""), 15, 35);
const table = doc.autoTable({
startY: 40,
head: [['Drink', 'Price', 'Amount', 'Total']],
body: body,
foot: [["", "", "Total", "€" + (Math.round(total*100)/100)]]
});
const finalY = table?.lastAutoTable?.finalY ?? 40;
doc.text("Generated on " + new Date(Date.now()).toString(), 15, finalY + 10);
const filepath = "/reports/" + filename + ".pdf";
doc.save("public" + filepath);
return filepath;
}