From a4fcbf41747b7db7dbc45ef6cccb4bebb9e337f0 Mon Sep 17 00:00:00 2001 From: bolbol Date: Sat, 22 Aug 2026 09:11:16 +0100 Subject: [PATCH] fix(export-pdf): heuristique de troncature pour eliminer les appels widthOfString corrupteurs de flux --- services/export-pdf.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/services/export-pdf.js b/services/export-pdf.js index 007b1de..db0c079 100644 --- a/services/export-pdf.js +++ b/services/export-pdf.js @@ -91,11 +91,14 @@ function kpiBox(doc, x, y, w, h, label, value, color) { // ─── Table ──────────────────────────────────────────────────────────────────── -function truncateToWidth(doc, text, maxWidth) { - if (doc.widthOfString(text) <= maxWidth) return text; - let t = text; - while (t.length > 1 && doc.widthOfString(t + '…') > maxWidth) t = t.slice(0, -1); - return t + '…'; +function truncateToWidth(text, maxWidth, fontSize) { + // Heuristique caractère/police pour éviter doc.widthOfString() en boucle — + // cause confirmée d'une corruption silencieuse du flux de contenu PDFKit + // sur ce document (cf. runbook). Ne PAS revenir à une mesure via widthOfString. + const avgCharWidth = fontSize * 0.52; // Helvetica, approximation correcte à l'œil + const maxChars = Math.max(1, Math.floor(maxWidth / avgCharWidth)); + if (text.length <= maxChars) return text; + return text.slice(0, maxChars - 1) + '…'; } function table(doc, { title, headers, rows, colWidths }) { @@ -118,7 +121,7 @@ function table(doc, { title, headers, rows, colWidths }) { doc.fontSize(7.5).font('Helvetica-Bold'); let x = 40; for (let i = 0; i < headers.length; i++) { - doc.text(truncateToWidth(doc, headers[i], widths[i] - 6), x + 3, y + 4, { width: widths[i] - 6, lineBreak: false }); + doc.text(truncateToWidth(headers[i], widths[i] - 6, 7.5), x + 3, y + 4, { width: widths[i] - 6, lineBreak: false }); x += widths[i]; } y += 17; @@ -141,7 +144,7 @@ function table(doc, { title, headers, rows, colWidths }) { doc.fontSize(7).font('Helvetica'); let x = 40; for (let i = 0; i < row.length; i++) { - doc.text(truncateToWidth(doc, String(row[i] ?? '—'), widths[i] - 6), x + 3, y + 4, { width: widths[i] - 6, lineBreak: false }); + doc.text(truncateToWidth(String(row[i] ?? '—'), widths[i] - 6, 7), x + 3, y + 4, { width: widths[i] - 6, lineBreak: false }); x += widths[i]; } stroke(doc, C.border);