Avoid unnecessary invoice QR page break

This commit is contained in:
2026-07-22 20:51:13 +02:00
parent e7107f151a
commit 4ab4c24b1a

View File

@@ -134,7 +134,6 @@ export const createInvoicePDF = async (server:FastifyInstance, returnMode, invoi
rightMarginMm: 20, rightMarginMm: 20,
bottomMarginMm: 40, bottomMarginMm: 40,
labelGapMm: 5, labelGapMm: 5,
labelHeightMm: 4,
safeGapMm: 5, safeGapMm: 5,
} }
@@ -148,83 +147,19 @@ export const createInvoicePDF = async (server:FastifyInstance, returnMode, invoi
const firstPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[0]) const firstPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[0])
const secondPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[backgroudPdf.getPages().length > 1 ? 1 : 0]) const secondPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[backgroudPdf.getPages().length > 1 ? 1 : 0])
const drawFoldMarks = (page: any, lengthMm: number) => { const getPaymentQrX = (page: any) => {
page.drawLine({ return page.getWidth()
start: getCoordinatesForPDFLib(0, 105, page), - paymentQrLayout.rightMarginMm * mm
end: getCoordinatesForPDFLib(lengthMm, 105, page), - paymentQrLayout.sizeMm * mm
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 addFinalContinuationPage = () => { const getTextWidthBeforePaymentQr = (page: any, startX: number) => {
const page = pdfDoc.addPage() if (!paymentQrCode) return 500
page.drawPage(secondPageBackground, { return Math.max(
x: 0, 240,
y: 0, getPaymentQrX(page) - startX - paymentQrLayout.safeGapMm * mm
}) )
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)
} }
// //
@@ -950,11 +885,6 @@ export const createInvoicePDF = async (server:FastifyInstance, returnMode, invoi
let endTextDiff = 35 let endTextDiff = 35
if (!deliveryNoteLikeDocumentTypes.includes(invoiceData.type)) { if (!deliveryNoteLikeDocumentTypes.includes(invoiceData.type)) {
if (paymentQrCode && getFinalBlockEndMm(rowHeight) > getPaymentQrReservedTopMm(pages[pageCounter - 1])) {
addFinalContinuationPage()
rowHeight = 30
}
pages[pageCounter - 1].drawLine({ pages[pageCounter - 1].drawLine({
start: getCoordinatesForPDFLib(20, rowHeight, page1), start: getCoordinatesForPDFLib(20, rowHeight, page1),
end: getCoordinatesForPDFLib(198, 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, { pages[pageCounter - 1].drawText(invoiceData.endText, {
...getCoordinatesForPDFLib(21, rowHeight + endTextDiff + (invoiceData.totalArray.length - 3) * 8, page1), ...endTextPosition,
size: 10, size: 10,
color: rgb(0, 0, 0), color: rgb(0, 0, 0),
lineHeight: 10, lineHeight: 10,
opacity: 1, opacity: 1,
maxWidth: 500 maxWidth: getTextWidthBeforePaymentQr(pages[pageCounter - 1], endTextPosition.x)
}) })
} }
if (paymentQrCode) { if (paymentQrCode) {
const lastPage = pages[pages.length - 1] const lastPage = pages[pages.length - 1]
const qrSize = paymentQrLayout.sizeMm * mm const qrSize = paymentQrLayout.sizeMm * mm
const rightMargin = paymentQrLayout.rightMarginMm * mm
const bottomMargin = paymentQrLayout.bottomMarginMm * mm const bottomMargin = paymentQrLayout.bottomMarginMm * mm
const labelGap = paymentQrLayout.labelGapMm * mm const labelGap = paymentQrLayout.labelGapMm * mm
const qrX = lastPage.getWidth() - rightMargin - qrSize const qrX = getPaymentQrX(lastPage)
const qrY = bottomMargin const qrY = bottomMargin
lastPage.drawText("Überweisung per QR-Code", { lastPage.drawText("Überweisung per QR-Code", {