import {PDFDocument, StandardFonts, rgb} from "pdf-lib" let headerData = { sender: "Federspiel Technology UG haftungsbeschränkt, Am Schwarzen Brack 14 26452 Sande", recipient: { name: "NOA Service GmbH", contact: "Lena Kramer", special: "Hinterm Haus", address: "Oldenburger Str. 52", city: "26340 Zetel" }, info: { invoiceNumber: "RE23-1409", customerNumber: "10069", invoiceDate: "15.09.2023", dateOfPerformance: "31.08.2023", contactPerson: "Florian Federspiel", tel: "015755769509", email: "f.federspiel@federspiel.tech" }, title: "Rechnung-Nr. RE23-1409", description: "BV: Stubbendränk 23, 26340 Zetel" } const getCoordinatesForPDFLib = (x ,y, page) => { /* * @param x the wanted X Parameter in Millimeters from Top Left * @param y the wanted Y Parameter in Millimeters from Top Left * @param page the page Object * * @returns x,y object * */ let retX = x * 2.83 let retY = page.getHeight()-(y*2.83) return { x: retX, y: retY } } export const useCreatePdf = async (invoiceData) => { const uri = ref("test") const genPDF = async () => { const pdfDoc = await PDFDocument.create() const font = await pdfDoc.embedFont(StandardFonts.Helvetica) const fontBold = await pdfDoc.embedFont(StandardFonts.HelveticaBold) let pages = [] let pageCounter = 1 const backgroundPdfSourceBuffer = await fetch("/Briefpapier.pdf").then((res) => res.arrayBuffer()) const backgroudPdf = await PDFDocument.load(backgroundPdfSourceBuffer) const firstPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[0]) const secondPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[1]) //console.log("TEST") const page1 = pdfDoc.addPage() //console.log(page1.getSize().width/2.83) page1.drawPage(firstPageBackground, { x: 0, y: 0, }) //console.log(page1.getSize()) pages.push(page1) //console.log(pages) //Falzmarke 1 pages[pageCounter - 1].drawLine({ start: getCoordinatesForPDFLib(0,105,page1), end: getCoordinatesForPDFLib(7,105,page1), thickness: 0.25, color: rgb(0,0,0), opacity: 1 }) //Lochmarke pages[pageCounter - 1].drawLine({ start: getCoordinatesForPDFLib(0,148.5,page1), end: getCoordinatesForPDFLib(7,148.5,page1), thickness: 0.25, color: rgb(0,0,0), opacity: 1 }) //Falzmarke 2 pages[pageCounter - 1].drawLine({ start: getCoordinatesForPDFLib(0,210,page1), end: getCoordinatesForPDFLib(7,210,page1), thickness: 0.25, color: rgb(0,0,0), opacity: 1 }) /*page1.drawLine({ start: getCoordinatesForPDFLib(20,45,page1), end: getCoordinatesForPDFLib(105,45,page1), thickness: 0.5, color: rgb(0,0,0), opacity: 1 })*/ pages[pageCounter - 1].drawText("Federspiel Technology UG haftungsbeschränkt, Am Schwarzen Brack 14 26452 Sande", { ...getCoordinatesForPDFLib(21,48, page1), size:6, color:rgb(0,0,0), lineHeight:6, opacity: 1, maxWidth: 240 }) /*page1.drawLine({ start: getCoordinatesForPDFLib(20,50,page1), end: getCoordinatesForPDFLib(105,50,page1), thickness: 0.5, color: rgb(0,0,0), opacity: 1 })*/ pages[pageCounter - 1].drawText(invoiceData.recipient.name, { ...getCoordinatesForPDFLib(21,55, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.recipient.contact, { ...getCoordinatesForPDFLib(21,60, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.recipient.special, { ...getCoordinatesForPDFLib(21,65, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.recipient.street, { ...getCoordinatesForPDFLib(21,70, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(`${invoiceData.recipient.zip} ${invoiceData.recipient.city}`, { ...getCoordinatesForPDFLib(21,75, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) /*page1.drawLine({ start: getCoordinatesForPDFLib(20,90,page1), end: getCoordinatesForPDFLib(105,90,page1), thickness: 0.5, color: rgb(0,0,0), opacity: 1 })*/ //Rechts /*page1.drawLine({ start: getCoordinatesForPDFLib(125,50,page1), end: getCoordinatesForPDFLib(200,50,page1), thickness: 0.5, color: rgb(0,0,0), opacity: 1 })*/ pages[pageCounter - 1].drawText("Rechnungsnummer", { ...getCoordinatesForPDFLib(126,55, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.info.documentNumber, { y: getCoordinatesForPDFLib(126,55, page1).y, x: getCoordinatesForPDFLib(126,55,page1).x + 210 - font.widthOfTextAtSize(headerData.info.invoiceNumber,10), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText("Kundennummer", { ...getCoordinatesForPDFLib(126,60, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.info.customerNumber, { y: getCoordinatesForPDFLib(126,60, page1).y, x: getCoordinatesForPDFLib(126,60,page1).x + 210 - font.widthOfTextAtSize(headerData.info.customerNumber,10), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText("Belegdatum", { ...getCoordinatesForPDFLib(126,65, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.info.documentDate, { y: getCoordinatesForPDFLib(126,65, page1).y, x: getCoordinatesForPDFLib(126,65,page1).x + 210 - font.widthOfTextAtSize(headerData.info.invoiceDate,10), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText("Lieferdatum", { ...getCoordinatesForPDFLib(126,70, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.info.deliveryDate, { y: getCoordinatesForPDFLib(126,70, page1).y, x: getCoordinatesForPDFLib(126,70,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.deliveryDate,10), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText("Ansprechpartner", { ...getCoordinatesForPDFLib(126,75, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.info.contactPerson, { y: getCoordinatesForPDFLib(126,75, page1).y, x: getCoordinatesForPDFLib(126,75,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.contactPerson,10), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText("Telefon", { ...getCoordinatesForPDFLib(126,80, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.info.contactTel, { y: getCoordinatesForPDFLib(126,80, page1).y, x: getCoordinatesForPDFLib(126,80,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.contactTel,10), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText("E-Mail", { ...getCoordinatesForPDFLib(126,85, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.info.contactEMail, { y: getCoordinatesForPDFLib(126,85, page1).y, x: getCoordinatesForPDFLib(126,85,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.contactEMail,10), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) /*pages[pageCounter - 1].drawText("Projekt:", { ...getCoordinatesForPDFLib(126,90, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(invoiceData.info.project, { y: getCoordinatesForPDFLib(126,90, page1).y, x: getCoordinatesForPDFLib(126,90,page1).x + 50 /!*+ 210 - font.widthOfTextAtSize(invoiceData.info.project,10)*!/, size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 150 })*/ /*page1.drawLine({ start: getCoordinatesForPDFLib(125,90,page1), end: getCoordinatesForPDFLib(200,90,page1), thickness: 0.5, color: rgb(0,0,0), opacity: 1 })*/ //Title /*page1.drawLine({ start: getCoordinatesForPDFLib(20,95,page1), end: getCoordinatesForPDFLib(200,95,page1), thickness: 0.5, color: rgb(0,0,0), opacity: 1 })*/ pages[pageCounter - 1].drawText(invoiceData.title, { ...getCoordinatesForPDFLib(20,100, page1), size:13, color:rgb(0,0,0), lineHeight:15, opacity: 1, maxWidth: 500 }) /*page1.drawLine({ start: getCoordinatesForPDFLib(20,105,page1), end: getCoordinatesForPDFLib(200,105,page1), thickness: 0.5, color: rgb(0,0,0), opacity: 1 })*/ pages[pageCounter - 1].drawText(invoiceData.description, { ...getCoordinatesForPDFLib(20,112, page1), size:13, color:rgb(0,0,0), lineHeight:15, opacity: 1, maxWidth: 500 }) pages[pageCounter - 1].drawText(invoiceData.startText,{ ...getCoordinatesForPDFLib(20,119, page1), size: 10, color: rgb(0,0,0), lineHeight: 10, opacity: 1, maxWidth: 500 }) /*page1.drawLine({ start: getCoordinatesForPDFLib(20,115,page1), end: getCoordinatesForPDFLib(200,115,page1), thickness: 0.5, color: rgb(0,0,0), opacity: 1 })*/ pages[pageCounter - 1].drawRectangle({ ...getCoordinatesForPDFLib(20,140, page1), width: 180 * 2.83, height: 8 * 2.83, color: rgb(0,0,0), opacity: 0.25 }) //Header pages[pageCounter - 1].drawText("Pos", { ...getCoordinatesForPDFLib(21,137, page1), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) pages[pageCounter - 1].drawText("Menge", { ...getCoordinatesForPDFLib(35,137, page1), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) pages[pageCounter - 1].drawText("Bezeichnung", { ...getCoordinatesForPDFLib(52,137, page1), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) pages[pageCounter - 1].drawText("Einheitspreis", { ...getCoordinatesForPDFLib(135,137, page1), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) pages[pageCounter - 1].drawText("Gesamt", { y: getCoordinatesForPDFLib(25,137, page1).y, x: getCoordinatesForPDFLib(25,137,page1).x + 490 - fontBold.widthOfTextAtSize("Gesamt",12), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) let rowHeight = 145 let pageIndex = 0 invoiceData.rows.forEach((row,index) => { if(row.mode === 'free' || row.mode === 'normal'){ pages[pageCounter - 1].drawText(row.pos, { ...getCoordinatesForPDFLib(21,rowHeight, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(`${row.quantity} ${row.unit}`, { ...getCoordinatesForPDFLib(35,rowHeight, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(row.text, { ...getCoordinatesForPDFLib(52,rowHeight, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240, font: fontBold }) if(row.description) { pages[pageCounter - 1].drawText(row.description, { ...getCoordinatesForPDFLib(52,rowHeight + 7, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 220, }) } pages[pageCounter - 1].drawText(row.price, { ...getCoordinatesForPDFLib(135,rowHeight, page1), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240 }) pages[pageCounter - 1].drawText(row.rowAmount, { y: getCoordinatesForPDFLib(25,rowHeight, page1).y, x: getCoordinatesForPDFLib(25,rowHeight,page1).x + 490 - font.widthOfTextAtSize(row.rowAmount,10), size:10, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240, }) if(row.discountPercent > 0) { pages[pageCounter - 1].drawText(row.discountText, { y: getCoordinatesForPDFLib(25,rowHeight + 5, page1).y, x: getCoordinatesForPDFLib(25,rowHeight + 5,page1).x + 490 - font.widthOfTextAtSize(row.discountText,8), size:8, color:rgb(0,0,0), lineHeight:10, opacity: 1, maxWidth: 240, }) } rowHeight += 14 pageIndex += 1 } else if(row.mode === 'pagebreak') { const page = pdfDoc.addPage() page.drawPage(secondPageBackground, { x: 0, y: 0, }) //Falzmarke 1 page.drawLine({ start: getCoordinatesForPDFLib(0,105,page1), end: getCoordinatesForPDFLib(7,105,page1), thickness: 0.25, color: rgb(0,0,0), opacity: 1 }) //Lochmarke page.drawLine({ start: getCoordinatesForPDFLib(0,148.5,page1), end: getCoordinatesForPDFLib(7,148.5,page1), thickness: 0.25, color: rgb(0,0,0), opacity: 1 }) //Falzmarke 2 page.drawLine({ start: getCoordinatesForPDFLib(0,210,page1), end: getCoordinatesForPDFLib(7,210,page1), thickness: 0.25, color: rgb(0,0,0), opacity: 1 }) page.drawRectangle({ ...getCoordinatesForPDFLib(20,25, page1), width: 180 * 2.83, height: 8 * 2.83, color: rgb(0,0,0), opacity: 0.25 }) //Header page.drawText("Pos", { ...getCoordinatesForPDFLib(21,22, page1), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) page.drawText("Menge", { ...getCoordinatesForPDFLib(35,22, page1), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) page.drawText("Bezeichnung", { ...getCoordinatesForPDFLib(52,22, page1), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) page.drawText("Einheitspreis", { ...getCoordinatesForPDFLib(135,22, page1), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) page.drawText("Gesamt", { y: getCoordinatesForPDFLib(25,22, page1).y, x: getCoordinatesForPDFLib(25,22,page1).x + 490 - fontBold.widthOfTextAtSize("Gesamt",12), size:12, color:rgb(0,0,0), lineHeight:12, opacity: 1, maxWidth: 240, font: fontBold }) pageCounter += 1; pageIndex = 0; rowHeight = 30; pages.push(page) //console.log(pages) } }) //Pos 1 //Footer console.log(rowHeight) //rowHeight += 25 pages[pageCounter - 1].drawRectangle({ ...getCoordinatesForPDFLib(20,rowHeight, page1), width: 180 * 2.83, height: 8 * 2.83, color: rgb(0,0,0), opacity: 0.25 }) pages[pageCounter - 1].drawRectangle({ ...getCoordinatesForPDFLib(20,rowHeight +16, page1), width: 180 * 2.83, height: 8 * 2.83, color: rgb(0,0,0), opacity: 0.25 }) pages[pageCounter - 1].drawText("Nettobetrag", { ...getCoordinatesForPDFLib(21,rowHeight-3, page1), size:11, color:rgb(0,0,0), lineHeight:11, opacity: 1, maxWidth: 240, font: fontBold }) pages[pageCounter - 1].drawText(invoiceData.total.totalNet, { y: getCoordinatesForPDFLib(21,rowHeight-3, page1).y, x: getCoordinatesForPDFLib(21,rowHeight-3,page1).x + 500 - fontBold.widthOfTextAtSize(invoiceData.total.totalNet,11), size:11, color:rgb(0,0,0), lineHeight:11, opacity: 1, maxWidth: 240, font:fontBold }) pages[pageCounter - 1].drawText("zzgl. 19% MwSt", { ...getCoordinatesForPDFLib(21,rowHeight+5, page1), size:11, color:rgb(0,0,0), lineHeight:11, opacity: 1, maxWidth: 240, font: fontBold }) pages[pageCounter - 1].drawText(invoiceData.total.total19, { y: getCoordinatesForPDFLib(21,rowHeight+5, page1).y, x: getCoordinatesForPDFLib(21,rowHeight+5,page1).x + 500 - fontBold.widthOfTextAtSize(invoiceData.total.total19,11), size:11, color:rgb(0,0,0), lineHeight:11, opacity: 1, maxWidth: 240, font:fontBold }) pages[pageCounter - 1].drawText("Gesamtsumme", { ...getCoordinatesForPDFLib(21,rowHeight+13, page1), size:11, color:rgb(0,0,0), lineHeight:11, opacity: 1, maxWidth: 240, font: fontBold }) pages[pageCounter - 1].drawText(invoiceData.total.totalGross, { y: getCoordinatesForPDFLib(21,rowHeight+13, page1).y, x: getCoordinatesForPDFLib(21,rowHeight+13,page1).x + 500 - fontBold.widthOfTextAtSize(invoiceData.total.totalGross,11), size:11, color:rgb(0,0,0), lineHeight:11, opacity: 1, maxWidth: 240, font:fontBold }) pages[pageCounter - 1].drawText(invoiceData.endText,{ ...getCoordinatesForPDFLib(20,rowHeight+22, page1), size: 10, color: rgb(0,0,0), lineHeight: 10, opacity: 1, maxWidth: 500 }) //console.log(await pdfDoc.saveAsBase64({dataUri: true})) uri.value = await pdfDoc.saveAsBase64({dataUri: true}) //console.log(uri.value) } await genPDF() //const pdfBytes = await pdfDoc.save() //const pdfDataUri = await pdfDoc.saveAsBase64({dataUri: true}) return uri.value //let blob = new Blob(pdfBytes, {type: "application/pdf"}) /*let link = document.createElement('a') link.href = pdfDataUri//window.URL.createObjectURL(blob) link.download = "test.pdf" link.click()*/ }