fix(export-pdf): heuristique de troncature pour eliminer les appels widthOfString corrupteurs de flux
This commit is contained in:
+10
-7
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user