Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-18 01:56:33 +00:00
co-authored by renee-png
parent 610eb2dcb2
commit 4b0bc82157
+49 -5
View File
@@ -349,24 +349,68 @@ export function CustomFormBuilder() {
}
doc.setFontSize(fontSize);
const lineH = fontSize * 1.4;
const drawTablePdf = (rows: string[][], hasHeader: boolean) => {
if (!rows.length) return;
const cols = Math.max(...rows.map((r) => r.length));
const colW = maxW / cols;
const cellPad = 4;
for (let ri = 0; ri < rows.length; ri++) {
const row = rows[ri];
// Compute row height based on tallest cell
const cellLines = row.map((c) =>
doc.splitTextToSize(applyVariables(c, ctx) || " ", colW - cellPad * 2),
);
const rowH = Math.max(...cellLines.map((l) => l.length)) * lineH + cellPad * 2;
if (y + rowH > pageH - margin) {
doc.addPage();
y = margin;
}
// Draw cells
for (let ci = 0; ci < cols; ci++) {
const x = margin + ci * colW;
doc.setDrawColor(180);
doc.rect(x, y, colW, rowH);
const isHeader = hasHeader && ri === 0;
doc.setFont(pdfFont, isHeader ? "bold" : "normal");
const lines = cellLines[ci] ?? [""];
let ty = y + cellPad + lineH * 0.8;
for (const ln of lines) {
doc.text(ln, x + cellPad, ty);
ty += lineH;
}
}
y += rowH;
}
y += 6;
};
for (const seg of segments) {
if (seg.kind === "table") {
drawTablePdf(seg.rows, seg.hasHeader);
continue;
}
const text = applyVariables(seg.text, ctx) || " ";
doc.setFont(pdfFont, "normal");
const lines = doc.splitTextToSize(text, maxW);
doc.setFont(pdfFont, seg.heading && seg.heading > 0 ? "bold" : "normal");
const indentPx = seg.indent || 0;
const indentPt = indentPx * 0.75; // px → pt
const segMaxW = maxW - indentPt;
const lines = doc.splitTextToSize(text, segMaxW);
for (const line of lines) {
if (y > pageH - margin) {
doc.addPage();
y = margin;
}
let x = margin;
let x = margin + indentPt;
if (seg.align === "center") x = pageW / 2;
else if (seg.align === "right") x = pageW - margin;
doc.text(line, x, y, {
align: seg.align === "center" ? "center" : seg.align === "right" ? "right" : "left",
});
y += fontSize * 1.4;
y += lineH;
}
y += 4;
y += seg.caption ? 2 : 4;
}
const filenameBase = (renderTitle(ctx) || "Custom_Form").replace(/\s+/g, "_");