Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(pdf): size table headers from their wrapped text on every page (#7658)

Ehsanf4bd6dd13750
diff --git a/changelog.d/2292.bugfix.md b/changelog.d/2292.bugfix.md
new file mode 100644
index 0000000000..c8266bb75b
--- /dev/null
+++ b/changelog.d/2292.bugfix.md
@@ -0,0 +1 @@
+PDF export now sizes a table's header band to its wrapped header text, draws the same header on every page the table continues onto, starts the table on a new page when that header does not fit, and draws the column lines on each page a table spans instead of only on the last one.
diff --git a/src/local_deep_research/web/static/js/services/pdf.js b/src/local_deep_research/web/static/js/services/pdf.js
index b76a3395f1..137d27bc69 100644
--- a/src/local_deep_research/web/static/js/services/pdf.js
+++ b/src/local_deep_research/web/static/js/services/pdf.js
@@ -665,51 +665,67 @@ async function generatePdf(title, content, _metadata = {}) {
colWidths.push(baseColWidth);
}
+ // Measure the header once; its height sizes the band, the
+ // separator and the row offset on every page that draws it.
+ pdf.setFont("helvetica", "bold");
+ pdf.setFontSize(fontSize);
+ const headerTexts = headerCells.map((cell, index) =>
+ pdf.splitTextToSize(cell.textContent.trim(), colWidths[index] - 2 * cellPadding));
+ const headerHeight = Math.max(
+ 25,
+ ...headerTexts.map((cellText) => cellText.length * (fontSize + 2) + 10),
+ );
+
+ const drawHeader = (y) => {
+ pdf.setFillColor(0, 51, 102);
+ pdf.rect(margin - 10, y, totalWidth + 20, headerHeight, 'F');
+
+ pdf.setFont("helvetica", "bold");
+ pdf.setFontSize(fontSize);
+ pdf.setTextColor(255, 255, 255);
+ headerTexts.forEach((cellText, index) => {
+ let x = margin;
+ for (let j = 0; j < index; j++) {
+ x += colWidths[j];
+ }
+ pdf.text(cellText, x + cellPadding, y + 15);
+ });
+
+ pdf.setDrawColor(200, 200, 200);
+ pdf.setLineWidth(0.5);
+ pdf.line(margin - 10, y + headerHeight, margin + totalWidth + 10, y + headerHeight);
+
+ pdf.setFont("helvetica", "normal");
+ pdf.setTextColor(0, 0, 0);
+ return y + headerHeight;
+ };
+
+ const drawColumnRules = (top, bottom) => {
+ pdf.setDrawColor(220, 220, 220);
+ let xPos = margin - 10;
+ for (let i = 0; i <= colCount; i++) {
+ pdf.line(xPos, top, xPos, bottom);
+ if (i < colCount) {
+ xPos += colWidths[i];
+ }
+ }
+ };
+
// Start table at current position
let tableY = currentY;
// Check if we need a new page
- if (tableY + 20 > pdfHeight - margin) {
+ if (tableY + headerHeight > pdfHeight - margin) {
pageNum++;
pdf.addPage();
tableY = margin;
}
- // Draw header background
- pdf.setFillColor(0, 51, 102);
- pdf.rect(margin - 10, tableY, totalWidth + 20, 25, 'F');
-
- // Draw header text
- pdf.setFont("helvetica", "bold");
- pdf.setFontSize(fontSize);
- pdf.setTextColor(255, 255, 255);
-
- let headerHeight = 25;
- headerCells.forEach((cell, index) => {
- const text = cell.textContent.trim();
- let x = margin;
- for (let i = 0; i < index; i++) {
- x += colWidths[i];
- }
-
- // Wrap text in cells
- const cellText = pdf.splitTextToSize(text, colWidths[index] - 2 * cellPadding);
- pdf.text(cellText, x + cellPadding, tableY + 15);
-
- // Track max height needed
- const cellHeight = cellText.length * (fontSize + 2) + 10;
- headerHeight = Math.max(headerHeight, cellHeight);
- });
-
- // Draw horizontal line after header
- pdf.setDrawColor(200, 200, 200);
- pdf.setLineWidth(0.5);
- pdf.line(margin - 10, tableY + headerHeight, margin + totalWidth + 10, tableY + headerHeight);
-
- tableY += headerHeight;
+ // Top of the part of the table on the current page
+ let segmentTop = tableY;
+ tableY = drawHeader(tableY);
// Draw table rows
- pdf.setFont("helvetica", "normal");
pdf.setFontSize(fontSize);
for (let i = 1; i < rows.length; i++) {
@@ -730,33 +746,11 @@ async function generatePdf(title, content, _metadata = {}) {
// Check if we need a new page
if (tableY + rowHeight > pdfHeight - margin) {
+ drawColumnRules(segmentTop, tableY);
pageNum++;
pdf.addPage();
- tableY = margin;
-
- // Redraw header on new page
- pdf.setFillColor(0, 51, 102);
- pdf.rect(margin - 10, tableY, totalWidth + 20, 25, 'F');
-
- pdf.setFont("helvetica", "bold");
- pdf.setTextColor(255, 255, 255);
- // tableY is mutated by the surrounding loop but the forEach is
- // synchronous, so no closure-over-loop-var hazard.
- // eslint-disable-next-line no-loop-func
- headerCells.forEach((cell, index) => {
- const text = cell.textContent.trim();
- let x = margin;
- for (let j = 0; j < index; j++) {
- x += colWidths[j];
- }
- const cellText = pdf.splitTextToSize(text, colWidths[index] - 2 * cellPadding);
- pdf.text(cellText, x + cellPadding, tableY + 15);
- });
-
- pdf.line(margin - 10, tableY + 25, margin + totalWidth + 10, tableY + 25);
- tableY += 25;
- pdf.setFont("helvetica", "normal");
- pdf.setTextColor(0, 0, 0);
+ segmentTop = margin;
+ tableY = drawHeader(margin);
}
// Alternate row background
@@ -783,15 +777,7 @@ async function generatePdf(title, content, _metadata = {}) {
tableY += rowHeight;
}
- // Draw vertical lines
- pdf.setDrawColor(220, 220, 220);
- let xPos = margin - 10;
- for (let i = 0; i <= colCount; i++) {
- pdf.line(xPos, currentY, xPos, tableY);
- if (i < colCount) {
- xPos += colWidths[i];
- }
- }
+ drawColumnRules(segmentTop, tableY);
// Update current position to after the table
currentY = tableY + 15;
diff --git a/tests/js/services/pdf-runtime.test.js b/tests/js/services/pdf-runtime.test.js
index 80105579d0..2d745e1f77 100644
--- a/tests/js/services/pdf-runtime.test.js
+++ b/tests/js/services/pdf-runtime.test.js
@@ -282,6 +282,145 @@ it('paginates long structured reports and redraws table ownership headers', asyn
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
+// Wraps like jsPDF (5pt per character, see getTextWidth) and records the
+// page and fill colour of every draw, which the stock fake does not.
+class PagedPdf extends FakePdf {
+ constructor() {
+ super();
+ this.page = 1;
+ this.fill = null;
+ this.ops = [];
+ this.splitTextToSize.mockImplementation((text, maxWidth) => {
+ const lines = [];
+ let line = '';
+ for (const word of String(text).split(' ')) {
+ const next = line ? `${line} ${word}` : word;
+ if (line && next.length * 5 > maxWidth) {
+ lines.push(line);
+ line = word;
+ } else {
+ line = next;
+ }
+ }
+ lines.push(line);
+ return lines;
+ });
+ this.addPage.mockImplementation(() => { this.page++; });
+ this.setFillColor.mockImplementation((...rgb) => { this.fill = rgb.join(','); });
+ this.rect.mockImplementation((x, y, w, h) => {
+ this.ops.push({ op: 'rect', page: this.page, y, h, fill: this.fill });
+ });
+ this.line.mockImplementation((x1, y1, x2, y2) => {
+ this.ops.push({ op: 'line', page: this.page, x1, y1, x2, y2 });
+ });
+ this.text.mockImplementation((text, x, y) => {
+ this.ops.push({ op: 'text', page: this.page, text, y });
+ });
+ }
+
+ headerBands() {
+ return this.ops.filter((o) => o.op === 'rect' && o.fill === '0,51,102');
+ }
+
+ columnRules(page) {
+ return this.ops.filter((o) =>
+ o.op === 'line' && o.x1 === o.x2 && o.page === page);
+ }
+}
+
+const WRAPPING_HEADER =
+ '<tr><th>Aspect of the claim being compared across both of the reports</th><th>Note</th></tr>';
+
+it('sizes the table header band to its wrapped header text', async () => {
+ globalThis.jsPDF = PagedPdf;
+ renderReturns(`<table>${WRAPPING_HEADER}
+ <tr><td>first</td><td>one</td></tr></table>`);
+
+ await generatePdf('Title', 'ignored');
+
+ const pdf = pdfInstances[0];
+ const [band] = pdf.headerBands();
+ const headerText = pdf.ops.find((o) =>
+ o.op === 'text' && o.text[0] === 'Aspect of the claim being compared across both of');
+ // Two header lines at fontSize 10: 2 * (10 + 2) + 10.
+ expect(headerText.text).toHaveLength(2);
+ expect(band.h).toBe(34);
+ const separator = pdf.ops.find((o) =>
+ o.op === 'line' && o.y1 === o.y2 && o.y1 === band.y + band.h);
+ expect(separator).toBeDefined();
+ const firstRow = pdf.ops.find((o) =>
+ o.op === 'text' && o.text[0] === 'first');
+ expect(firstRow.y).toBe(band.y + 34 + 12);
+});
+
+function longTable(rowCount) {
+ const rows = Array.from({ length: rowCount }, (_, i) =>
+ `<tr><td>row ${i}</td><td>value</td></tr>`).join('');
+ return `<table>${WRAPPING_HEADER}${rows}</table>`;
+}
+
+it('redraws the same wrapped header on every page a table spans', async () => {
+ pageHeight = 300;
+ globalThis.jsPDF = PagedPdf;
+ renderReturns(longTable(15));
+
+ await generatePdf('Title', 'ignored');
+
+ const pdf = pdfInstances[0];
+ const bands = pdf.headerBands();
+ expect(bands.map((b) => b.page)).toEqual([1, 2]);
+ for (const band of bands) {
+ expect(band.h).toBe(34);
+ const firstRowOnPage = pdf.ops.find((o) =>
+ o.op === 'text' && o.page === band.page && o.text[0].startsWith('row'));
+ expect(firstRowOnPage.y).toBe(band.y + 34 + 12);
+ }
+});
+
+it('draws column rules on every page a table spans, within that page', async () => {
+ pageHeight = 300;
+ globalThis.jsPDF = PagedPdf;
+ renderReturns(longTable(15));
+
+ await generatePdf('Title', 'ignored');
+
+ const pdf = pdfInstances[0];
+ const bands = pdf.headerBands();
+ expect(bands).toHaveLength(2);
+ for (const band of bands) {
+ const rules = pdf.columnRules(band.page);
+ // Three rules for two columns, from this page's header to the
+ // last row drawn on this page.
+ expect(rules).toHaveLength(3);
+ const lastRowLine = pdf.ops.filter((o) =>
+ o.op === 'line' && o.y1 === o.y2 && o.page === band.page).at(-1);
+ for (const rule of rules) {
+ expect(rule.y1).toBe(band.y);
+ expect(rule.y2).toBe(lastRowLine.y1);
+ }
+ }
+});
+
+it('moves the table to a new page when its wrapped header does not fit', async () => {
+ // Three paragraphs put the table at y=100; 165 - 40 leaves 25pt,
+ // room for a one-line header but not for this two-line one.
+ pageHeight = 165;
+ globalThis.jsPDF = PagedPdf;
+ renderReturns(`<p>one</p><p>two</p><p>three</p><table>${WRAPPING_HEADER}
+ <tr><td>first</td><td>one</td></tr></table>`);
+
+ await generatePdf('Title', 'ignored');
+
+ const pdf = pdfInstances[0];
+ const [band] = pdf.headerBands();
+ expect(band.page).toBe(2);
+ expect(band.y).toBe(40);
+ for (const rule of pdf.columnRules(1)) {
+ expect(rule.y1).not.toBe(100);
+ }
+ expect(pdf.columnRules(2).every((rule) => rule.y1 === 40)).toBe(true);
+});
+
it('wraps long links and citations across pages without losing their targets', async () => {
pageWidth = 140;
pageHeight = 90;