diff --git a/backend/src/utils/pdf.ts b/backend/src/utils/pdf.ts index 18fba50..f82e66f 100644 --- a/backend/src/utils/pdf.ts +++ b/backend/src/utils/pdf.ts @@ -134,7 +134,6 @@ export const createInvoicePDF = async (server:FastifyInstance, returnMode, invoi rightMarginMm: 20, bottomMarginMm: 40, labelGapMm: 5, - labelHeightMm: 4, safeGapMm: 5, } @@ -148,83 +147,19 @@ export const createInvoicePDF = async (server:FastifyInstance, returnMode, invoi const firstPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[0]) const secondPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[backgroudPdf.getPages().length > 1 ? 1 : 0]) - const drawFoldMarks = (page: any, lengthMm: number) => { - page.drawLine({ - start: getCoordinatesForPDFLib(0, 105, page), - end: getCoordinatesForPDFLib(lengthMm, 105, page), - thickness: 0.25, - color: rgb(0, 0, 0), - opacity: 1 - }) - - page.drawLine({ - start: getCoordinatesForPDFLib(0, 148.5, page), - end: getCoordinatesForPDFLib(lengthMm, 148.5, page), - thickness: 0.25, - color: rgb(0, 0, 0), - opacity: 1 - }) - - page.drawLine({ - start: getCoordinatesForPDFLib(0, 210, page), - end: getCoordinatesForPDFLib(lengthMm, 210, page), - thickness: 0.25, - color: rgb(0, 0, 0), - opacity: 1 - }) + const getPaymentQrX = (page: any) => { + return page.getWidth() + - paymentQrLayout.rightMarginMm * mm + - paymentQrLayout.sizeMm * mm } - const addFinalContinuationPage = () => { - const page = pdfDoc.addPage() + const getTextWidthBeforePaymentQr = (page: any, startX: number) => { + if (!paymentQrCode) return 500 - page.drawPage(secondPageBackground, { - x: 0, - y: 0, - }) - - drawFoldMarks(page, 7) - - pageCounter += 1 - pageIndex = 0 - pages.push(page) - - return page - } - - const getPaymentQrReservedTopMm = (page: any) => { - return page.getHeight() / mm - - paymentQrLayout.bottomMarginMm - - paymentQrLayout.sizeMm - - paymentQrLayout.labelGapMm - - paymentQrLayout.labelHeightMm - - paymentQrLayout.safeGapMm - } - - const getTextLineCount = (text: any, maxSplitLength: number) => { - return String(text || "") - .split(/\r?\n/) - .reduce((sum, line) => sum + Math.max(1, splitStringBySpace(line, maxSplitLength).length), 0) - } - - const getFinalBlockEndMm = (startRowHeight: number) => { - let estimatedRowHeight = startRowHeight + 6 - const titleSumKeys = Object.keys(invoiceData.total.titleSums || {}) - - titleSumKeys.forEach((key) => { - estimatedRowHeight += splitStringBySpace(key, 60).length * 5 - }) - - if (titleSumKeys.length > 0) estimatedRowHeight += 5 - - const totalArrayLength = invoiceData.totalArray.length - const totalArrayBottom = estimatedRowHeight + Math.max(0, totalArrayLength - 1) * 8 + 6 - const taxNoteBottom = ["13b UStG", "19 UStG", "12.3 UStG"].includes(invoiceData.taxType) - ? estimatedRowHeight + totalArrayLength * 8 + 6 - : estimatedRowHeight - const endTextTop = estimatedRowHeight + endTextDiff + (totalArrayLength - 3) * 8 - const endTextBottom = endTextTop + getTextLineCount(invoiceData.endText, 85) * 5 - - return Math.max(totalArrayBottom, taxNoteBottom, endTextBottom) + return Math.max( + 240, + getPaymentQrX(page) - startX - paymentQrLayout.safeGapMm * mm + ) } // @@ -950,11 +885,6 @@ export const createInvoicePDF = async (server:FastifyInstance, returnMode, invoi let endTextDiff = 35 if (!deliveryNoteLikeDocumentTypes.includes(invoiceData.type)) { - if (paymentQrCode && getFinalBlockEndMm(rowHeight) > getPaymentQrReservedTopMm(pages[pageCounter - 1])) { - addFinalContinuationPage() - rowHeight = 30 - } - pages[pageCounter - 1].drawLine({ start: getCoordinatesForPDFLib(20, rowHeight, page1), end: getCoordinatesForPDFLib(198, rowHeight, page1), @@ -1067,24 +997,24 @@ export const createInvoicePDF = async (server:FastifyInstance, returnMode, invoi } + const endTextPosition = getCoordinatesForPDFLib(21, rowHeight + endTextDiff + (invoiceData.totalArray.length - 3) * 8, page1) pages[pageCounter - 1].drawText(invoiceData.endText, { - ...getCoordinatesForPDFLib(21, rowHeight + endTextDiff + (invoiceData.totalArray.length - 3) * 8, page1), + ...endTextPosition, size: 10, color: rgb(0, 0, 0), lineHeight: 10, opacity: 1, - maxWidth: 500 + maxWidth: getTextWidthBeforePaymentQr(pages[pageCounter - 1], endTextPosition.x) }) } if (paymentQrCode) { const lastPage = pages[pages.length - 1] const qrSize = paymentQrLayout.sizeMm * mm - const rightMargin = paymentQrLayout.rightMarginMm * mm const bottomMargin = paymentQrLayout.bottomMarginMm * mm const labelGap = paymentQrLayout.labelGapMm * mm - const qrX = lastPage.getWidth() - rightMargin - qrSize + const qrX = getPaymentQrX(lastPage) const qrY = bottomMargin lastPage.drawText("Überweisung per QR-Code", {