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): cap the table header at half the usable page height (#7737)

Ehsanc7b62afa4db2
diff --git a/changelog.d/+pdf-table-tall-header-cap.bugfix.md b/changelog.d/+pdf-table-tall-header-cap.bugfix.md
new file mode 100644
index 0000000000..8161f4033e
--- /dev/null
+++ b/changelog.d/+pdf-table-tall-header-cap.bugfix.md
@@ -0,0 +1 @@
+A table header too tall for half the page in a PDF export is now cut to fit with a trailing "...", instead of pushing every row of the table onto its own page.
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 6981f1c015..ccd032c704 100644
--- a/src/local_deep_research/web/static/js/services/pdf.js
+++ b/src/local_deep_research/web/static/js/services/pdf.js
@@ -676,8 +676,22 @@ async function generatePdf(title, content, _metadata = {}) {
// 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));
+ // Cap it at half the usable height, or a tall header leaves
+ // room for only one row under it and every row starts a page.
+ const maxHeaderLines = Math.max(1, Math.floor(
+ ((pdfHeight - 2 * margin) / 2 - 10) / (fontSize + 2)));
+ const headerTexts = headerCells.map((cell, index) => {
+ const width = colWidths[index] - 2 * cellPadding;
+ const lines = pdf.splitTextToSize(cell.textContent.trim(), width);
+ if (lines.length <= maxHeaderLines) return lines;
+ const kept = lines.slice(0, maxHeaderLines);
+ let last = kept[maxHeaderLines - 1];
+ while (last && pdf.splitTextToSize(`${last}...`, width).length > 1) {
+ last = last.slice(0, -1);
+ }
+ kept[maxHeaderLines - 1] = `${last.trimEnd()}...`;
+ return kept;
+ });
const headerHeight = Math.max(
25,
...headerTexts.map((cellText) => cellText.length * (fontSize + 2) + 10),
diff --git a/tests/js/services/pdf-runtime.test.js b/tests/js/services/pdf-runtime.test.js
index 89d6c8e25f..0990b72d67 100644
--- a/tests/js/services/pdf-runtime.test.js
+++ b/tests/js/services/pdf-runtime.test.js
@@ -421,6 +421,79 @@ it('moves the table to a new page when its wrapped header does not fit', async (
expect(pdf.columnRules(2).every((rule) => rule.y1 === 40)).toBe(true);
});
+it('caps a header taller than half the page so rows still share a page', async () => {
+ // 120 twelve-character words wrap four to a line: 30 header lines,
+ // taller than the 220pt between the margins of a 300pt page.
+ pageHeight = 300;
+ globalThis.jsPDF = PagedPdf;
+ const longHeader = Array.from({ length: 120 }, (_, i) =>
+ `header${String(i).padStart(6, '0')}`).join(' ');
+ const rows = Array.from({ length: 10 }, (_, i) =>
+ `<tr><td>row ${i}</td><td>value</td></tr>`).join('');
+ renderReturns(`<table><tr><th>${longHeader}</th><th>Note</th></tr>${rows}</table>`);
+
+ await generatePdf('Title', 'ignored');
+
+ const pdf = pdfInstances[0];
+ const bands = pdf.headerBands();
+ // Half of 220pt allows 8 lines: 8 * (10 + 2) + 10.
+ expect(bands.map((b) => [b.page, b.h])).toEqual([[1, 106], [2, 106]]);
+ const headerText = pdf.ops.find((o) =>
+ o.op === 'text' && o.text[0].startsWith('header000000'));
+ expect(headerText.text).toHaveLength(8);
+ // The eighth line was full, so it loses characters to fit the marker.
+ expect(headerText.text[7]).toBe('header000028 header000029 header000030 header000...');
+ const rowPages = pdf.ops.filter((o) =>
+ o.op === 'text' && o.text[0].startsWith('row')).map((o) => o.page);
+ expect(rowPages).toEqual([1, 1, 1, 1, 1, 2, 2, 2, 2, 2]);
+});
+
+it('keeps one header line when half the page is shorter than a line', async () => {
+ // 110 - 80 leaves 30pt between the margins; half of it minus the
+ // band padding is under one 12pt line, so the cap floors at 1.
+ pageHeight = 110;
+ 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 bands = pdf.headerBands();
+ expect(bands.length).toBeGreaterThan(0);
+ // One line is 1 * (10 + 2) + 10 = 22, under the 25pt minimum band.
+ expect(bands.every((b) => b.h === 25)).toBe(true);
+ const headerText = pdf.ops.find((o) =>
+ o.op === 'text' && o.text[0].startsWith('Aspect'));
+ expect(headerText.text).toHaveLength(1);
+ expect(headerText.text[0].endsWith('...')).toBe(true);
+ expect(pdf.ops.some((o) => o.op === 'text' && o.text[0] === 'first')).toBe(true);
+});
+
+it('caps a tall header in a column other than the first', async () => {
+ pageHeight = 300;
+ globalThis.jsPDF = PagedPdf;
+ const longHeader = Array.from({ length: 120 }, (_, i) =>
+ `header${String(i).padStart(6, '0')}`).join(' ');
+ const rows = Array.from({ length: 10 }, (_, i) =>
+ `<tr><td>row ${i}</td><td>value</td></tr>`).join('');
+ renderReturns(`<table><tr><th>Note</th><th>${longHeader}</th></tr>${rows}</table>`);
+
+ await generatePdf('Title', 'ignored');
+
+ const pdf = pdfInstances[0];
+ expect(pdf.headerBands().map((b) => [b.page, b.h])).toEqual([[1, 106], [2, 106]]);
+ const firstColumn = pdf.ops.find((o) => o.op === 'text' && o.text[0] === 'Note');
+ expect(firstColumn.text).toHaveLength(1);
+ const secondColumn = pdf.ops.find((o) =>
+ o.op === 'text' && o.text[0].startsWith('header000000'));
+ expect(secondColumn.text).toHaveLength(8);
+ expect(secondColumn.text[7]).toBe('header000028 header000029 header000030 header000...');
+ const rowPages = pdf.ops.filter((o) =>
+ o.op === 'text' && o.text[0].startsWith('row')).map((o) => o.page);
+ expect(rowPages).toEqual([1, 1, 1, 1, 1, 2, 2, 2, 2, 2]);
+});
+
it('wraps long links and citations across pages without losing their targets', async () => {
pageWidth = 140;
pageHeight = 90;