From 0590fa0875074631ee8de94a859b079109330f95 Mon Sep 17 00:00:00 2001 From: flfeders Date: Thu, 14 Dec 2023 20:59:44 +0100 Subject: [PATCH] Tests with PDF library --- test/pdfgen/index.js | 676 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 676 insertions(+) create mode 100644 test/pdfgen/index.js diff --git a/test/pdfgen/index.js b/test/pdfgen/index.js new file mode 100644 index 0000000..b65fd5c --- /dev/null +++ b/test/pdfgen/index.js @@ -0,0 +1,676 @@ +//import { PDFDocument, StandardFonts, rgb } from 'pdf-lib' +const { PDFDocument,StandardFonts,rgb } = require("pdf-lib"); + +const fs = require("fs") + + +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 + } + +} + + +async function createPdf() { + fs.readFile("Briefpapier.pdf",async (err,file) => { + console.log(file) + + const pdfDoc = await PDFDocument.create() + + const font = await pdfDoc.embedFont(StandardFonts.Helvetica) + const fontBold = await pdfDoc.embedFont(StandardFonts.HelveticaBold) + + + + const backgroudPdf = await PDFDocument.load(file) + + 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()) + + + //Falzmarke 1 + page1.drawLine({ + start: getCoordinatesForPDFLib(0,105,page1), + end: getCoordinatesForPDFLib(7,105,page1), + thickness: 0.25, + color: rgb(0,0,0), + opacity: 1 + }) + + //Lochmarke + page1.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 + page1.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 + })*/ + + page1.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 + })*/ + + + + + page1.drawText(headerData.recipient.name, { + ...getCoordinatesForPDFLib(21,55, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + page1.drawText(headerData.recipient.contact, { + ...getCoordinatesForPDFLib(21,60, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + page1.drawText(headerData.recipient.special, { + ...getCoordinatesForPDFLib(21,65, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + page1.drawText(headerData.recipient.address, { + ...getCoordinatesForPDFLib(21,70, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + page1.drawText(headerData.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 + })*/ + + page1.drawText("Rechnungsnummer", { + ...getCoordinatesForPDFLib(126,55, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText(headerData.info.invoiceNumber, { + 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 + }) + page1.drawText("Kundennummer", { + ...getCoordinatesForPDFLib(126,60, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText(headerData.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 + }) + + page1.drawText("Belegdatum", { + ...getCoordinatesForPDFLib(126,65, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText(headerData.info.invoiceDate, { + 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 + }) + page1.drawText("Leistungsdatum", { + ...getCoordinatesForPDFLib(126,70, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText(headerData.info.dateOfPerformance, { + y: getCoordinatesForPDFLib(126,70, page1).y, + x: getCoordinatesForPDFLib(126,70,page1).x + 210 - font.widthOfTextAtSize(headerData.info.dateOfPerformance,10), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("Ansprechpartner", { + ...getCoordinatesForPDFLib(126,75, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText(headerData.info.contactPerson, { + y: getCoordinatesForPDFLib(126,75, page1).y, + x: getCoordinatesForPDFLib(126,75,page1).x + 210 - font.widthOfTextAtSize(headerData.info.contactPerson,10), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("Telefon", { + ...getCoordinatesForPDFLib(126,80, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText(headerData.info.tel, { + y: getCoordinatesForPDFLib(126,80, page1).y, + x: getCoordinatesForPDFLib(126,80,page1).x + 210 - font.widthOfTextAtSize(headerData.info.tel,10), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("E-Mail", { + ...getCoordinatesForPDFLib(126,85, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText(headerData.info.email, { + y: getCoordinatesForPDFLib(126,85, page1).y, + x: getCoordinatesForPDFLib(126,85,page1).x + 210 - font.widthOfTextAtSize(headerData.info.email,10), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + /*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 + })*/ + + page1.drawText(headerData.title, { + ...getCoordinatesForPDFLib(20,102, page1), + size:15, + color:rgb(0,0,0), + lineHeight:15, + opacity: 1, + maxWidth: 240 + }) + + /*page1.drawLine({ + start: getCoordinatesForPDFLib(20,105,page1), + end: getCoordinatesForPDFLib(200,105,page1), + thickness: 0.5, + color: rgb(0,0,0), + opacity: 1 + })*/ + + page1.drawText(headerData.description, { + ...getCoordinatesForPDFLib(20,112, page1), + size:15, + color:rgb(0,0,0), + lineHeight:15, + opacity: 1, + maxWidth: 240 + }) + + /*page1.drawLine({ + start: getCoordinatesForPDFLib(20,115,page1), + end: getCoordinatesForPDFLib(200,115,page1), + thickness: 0.5, + color: rgb(0,0,0), + opacity: 1 + })*/ + + page1.drawRectangle({ + ...getCoordinatesForPDFLib(20,130, page1), + width: 180 * 2.83, + height: 8 * 2.83, + color: rgb(0,0,0), + opacity: 0.25 + }) + + //Header + + page1.drawText("Pos", { + ...getCoordinatesForPDFLib(21,127, page1), + size:12, + color:rgb(0,0,0), + lineHeight:12, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("Menge", { + ...getCoordinatesForPDFLib(35,127, page1), + size:12, + color:rgb(0,0,0), + lineHeight:12, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("Bezeichnung", { + ...getCoordinatesForPDFLib(52,127, page1), + size:12, + color:rgb(0,0,0), + lineHeight:12, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("Einheitspreis", { + ...getCoordinatesForPDFLib(135,127, page1), + size:12, + color:rgb(0,0,0), + lineHeight:12, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("Gesamt", { + y: getCoordinatesForPDFLib(25,127, page1).y, + x: getCoordinatesForPDFLib(25,127,page1).x + 490 - fontBold.widthOfTextAtSize("Gesamt",12), + size:12, + color:rgb(0,0,0), + lineHeight:12, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + //Pos 1 + + page1.drawText("1.001", { + ...getCoordinatesForPDFLib(21,135, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("40,00 Std", { + ...getCoordinatesForPDFLib(35,135, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("IT-Dienstleistung", { + ...getCoordinatesForPDFLib(52,135, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("Florian Federspiel 03.11.23 10.00 - 15.00 5h ", { + ...getCoordinatesForPDFLib(52,142, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240, + }) + + page1.drawText("28,00 €", { + ...getCoordinatesForPDFLib(135,135, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("1.120,00 €", { + y: getCoordinatesForPDFLib(25,135, page1).y, + x: getCoordinatesForPDFLib(25,135,page1).x + 490 - font.widthOfTextAtSize("1.120,00 €",10), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240, + }) + + //Pos 2 + + page1.drawText("1.002", { + ...getCoordinatesForPDFLib(21,149, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("40,00 Std", { + ...getCoordinatesForPDFLib(35,149, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("IT-Dienstleistung", { + ...getCoordinatesForPDFLib(52,149, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("Florian Federspiel 03.11.23 10.00 - 15.00 5h ", { + ...getCoordinatesForPDFLib(52,156, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240, + }) + + page1.drawText("28,00 €", { + ...getCoordinatesForPDFLib(135,149, page1), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240 + }) + + page1.drawText("1.120,00 €", { + y: getCoordinatesForPDFLib(25,149, page1).y, + x: getCoordinatesForPDFLib(25,149,page1).x + 490 - font.widthOfTextAtSize("1.120,00 €",10), + size:10, + color:rgb(0,0,0), + lineHeight:10, + opacity: 1, + maxWidth: 240, + }) + + //Footer + + page1.drawRectangle({ + ...getCoordinatesForPDFLib(20,200, page1), + width: 180 * 2.83, + height: 8 * 2.83, + color: rgb(0,0,0), + opacity: 0.25 + }) + + page1.drawRectangle({ + ...getCoordinatesForPDFLib(20,216, page1), + width: 180 * 2.83, + height: 8 * 2.83, + color: rgb(0,0,0), + opacity: 0.25 + }) + + page1.drawText("Nettobetrag", { + ...getCoordinatesForPDFLib(21,197, page1), + size:11, + color:rgb(0,0,0), + lineHeight:11, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("2.240,00 €", { + y: getCoordinatesForPDFLib(21,197, page1).y, + x: getCoordinatesForPDFLib(21,197,page1).x + 500 - fontBold.widthOfTextAtSize("2.240,00 €",11), + size:11, + color:rgb(0,0,0), + lineHeight:11, + opacity: 1, + maxWidth: 240, + font:fontBold + }) + + page1.drawText("zzgl. 19% MwSt", { + ...getCoordinatesForPDFLib(21,205, page1), + size:11, + color:rgb(0,0,0), + lineHeight:11, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("425,60 €", { + y: getCoordinatesForPDFLib(21,205, page1).y, + x: getCoordinatesForPDFLib(21,205,page1).x + 500 - fontBold.widthOfTextAtSize("425,60 €",11), + size:11, + color:rgb(0,0,0), + lineHeight:11, + opacity: 1, + maxWidth: 240, + font:fontBold + }) + + page1.drawText("Gesamtsumme", { + ...getCoordinatesForPDFLib(21,213, page1), + size:11, + color:rgb(0,0,0), + lineHeight:11, + opacity: 1, + maxWidth: 240, + font: fontBold + }) + + page1.drawText("2.665,60 €", { + y: getCoordinatesForPDFLib(21,213, page1).y, + x: getCoordinatesForPDFLib(21,213,page1).x + 500 - fontBold.widthOfTextAtSize("2.665,60 €",11), + size:11, + color:rgb(0,0,0), + lineHeight:11, + opacity: 1, + maxWidth: 240, + font:fontBold + }) + + page1.drawText("Bitte überweisen Sie den Rechnungsbetrag unter Angabe der Rechnungsnummer im Verwendungszweck innerhalb von 10 Tagen auf das unten angegebene Konto. Wir bedanken uns für das entgegengebrachte Vertrauen und freuen uns auf eine weitere gute Zusammenarbeit.",{ + ...getCoordinatesForPDFLib(20,222, page1), + size: 10, + color: rgb(0,0,0), + lineHeight: 10, + opacity: 1, + maxWidth: 500 + }) + + + + + + + /*const page2 = pdfDoc.addPage() + + page2.drawPage(secondPageBackground, { + x:0, + y:0 + })*/ + + + console.log("SAVING") + + const pdfBytes = await pdfDoc.save() + + fs.writeFile("test.pdf",pdfBytes, (err) => { + console.log(err) + }) + }) + + + + + +} + +createPdf() \ No newline at end of file