fix(export-pdf): heuristique de troncature pour eliminer les appels widthOfString corrupteurs de flux

This commit is contained in:
2026-08-22 09:11:16 +01:00
parent 7599b98959
commit a4fcbf4174
+10 -7
View File
@@ -91,11 +91,14 @@ function kpiBox(doc, x, y, w, h, label, value, color) {
// ─── Table ──────────────────────────────────────────────────────────────────── // ─── Table ────────────────────────────────────────────────────────────────────
function truncateToWidth(doc, text, maxWidth) { function truncateToWidth(text, maxWidth, fontSize) {
if (doc.widthOfString(text) <= maxWidth) return text; // Heuristique caractère/police pour éviter doc.widthOfString() en boucle —
let t = text; // cause confirmée d'une corruption silencieuse du flux de contenu PDFKit
while (t.length > 1 && doc.widthOfString(t + '…') > maxWidth) t = t.slice(0, -1); // sur ce document (cf. runbook). Ne PAS revenir à une mesure via widthOfString.
return t + '…'; 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 }) { 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'); doc.fontSize(7.5).font('Helvetica-Bold');
let x = 40; let x = 40;
for (let i = 0; i < headers.length; i++) { 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]; x += widths[i];
} }
y += 17; y += 17;
@@ -141,7 +144,7 @@ function table(doc, { title, headers, rows, colWidths }) {
doc.fontSize(7).font('Helvetica'); doc.fontSize(7).font('Helvetica');
let x = 40; let x = 40;
for (let i = 0; i < row.length; i++) { 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]; x += widths[i];
} }
stroke(doc, C.border); stroke(doc, C.border);