~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(pdf): keep a table header on the same page as its first row (#7953)
Ehsanb8cd49e7d3d9
diff --git a/changelog.d/7953.bugfix.md b/changelog.d/7953.bugfix.mdnew file mode 100644index 0000000000..52c75c5d7d--- /dev/null+++ b/changelog.d/7953.bugfix.md@@ -0,0 +1 @@+PDF export no longer leaves a table's header alone at the bottom of a page when the first row does not fit under it; the table now starts on the next page unless the header and the first row could not fit on a single page together.diff --git a/src/local_deep_research/web/static/js/services/pdf.js b/src/local_deep_research/web/static/js/services/pdf.jsindex ccd032c704..5a85522844 100644--- a/src/local_deep_research/web/static/js/services/pdf.js+++ b/src/local_deep_research/web/static/js/services/pdf.js@@ -732,11 +732,32 @@ async function generatePdf(title, content, _metadata = {}) {}};+ const measureRow = (row) => {+ const cellContents = Array.from(row.querySelectorAll('td, th')).map((cell, index) =>+ pdf.splitTextToSize(cell.textContent.trim(), colWidths[index] - 2 * cellPadding));+ const rowHeight = Math.max(+ 20,+ ...cellContents.map((wrappedText) => wrappedText.length * (fontSize + 2) + 10),+ );+ return { cellContents, rowHeight };+ };+// Start table at current positionlet tableY = currentY;+ // Keep the header with the first row, unless the two+ // could not share even an empty page. The row is measured+ // in the normal face the loop draws it in, and firstRow is+ // non-null exactly when the loop reaches i === 1.+ pdf.setFont("helvetica", "normal");+ const firstRow = rows.length > 1 ? measureRow(rows[1]) : null;+ let neededHeight = headerHeight;+ if (firstRow && headerHeight + firstRow.rowHeight <= pdfHeight - 2 * margin) {+ neededHeight += firstRow.rowHeight;+ }+// Check if we need a new page- if (tableY + headerHeight > pdfHeight - margin) {+ if (tableY + neededHeight > pdfHeight - margin) {pageNum++;pdf.addPage();tableY = margin;@@ -750,20 +771,7 @@ async function generatePdf(title, content, _metadata = {}) {pdf.setFontSize(fontSize);for (let i = 1; i < rows.length; i++) {- // Get cells for this row- const cells = Array.from(rows[i].querySelectorAll('td, th'));-- // Calculate row height based on content- let rowHeight = 20;- const cellContents = [];-- cells.forEach((cell, index) => {- const text = cell.textContent.trim();- const wrappedText = pdf.splitTextToSize(text, colWidths[index] - 2 * cellPadding);- cellContents.push(wrappedText);- const cellHeight = wrappedText.length * (fontSize + 2) + 10;- rowHeight = Math.max(rowHeight, cellHeight);- });+ const { cellContents, rowHeight } = i === 1 ? firstRow : measureRow(rows[i]);// Check if we need a new pageif (tableY + rowHeight > pdfHeight - margin) {diff --git a/tests/js/services/pdf-runtime.test.js b/tests/js/services/pdf-runtime.test.jsindex 0990b72d67..b1fcef1352 100644--- a/tests/js/services/pdf-runtime.test.js+++ b/tests/js/services/pdf-runtime.test.js@@ -494,6 +494,79 @@ it('caps a tall header in a column other than the first', async () => {expect(rowPages).toEqual([1, 1, 1, 1, 1, 2, 2, 2, 2, 2]);});+function cappedHeaderTable(firstRowLines) {+ const words = (prefix, count) => Array.from({ length: count }, (_, i) =>+ `${prefix}${String(i).padStart(6, '0')}`).join(' ');+ // Four twelve-character words fill one line of a 256pt column.+ return `<table><tr><th>${words('header', 120)}</th><th>Note</th></tr>+ <tr><td>${words('firstr', 4 * firstRowLines)}</td><td>one</td></tr>+ <tr><td>row 1</td><td>two</td></tr></table>`;+}++it('moves a capped header to the next page when its first row does not fit under it', async () => {+ // The table starts at y=100 and the band ends at 206. The five-line+ // first row needs 70pt, which passes the 260pt bottom margin.+ pageHeight = 300;+ globalThis.jsPDF = PagedPdf;+ renderReturns(`<p>one</p><p>two</p><p>three</p>${cappedHeaderTable(5)}`);++ await generatePdf('Title', 'ignored');++ const pdf = pdfInstances[0];+ expect(pdf.headerBands().map((b) => [b.page, b.y, b.h])).toEqual([[2, 40, 106]]);+ expect(pdf.columnRules(1)).toHaveLength(0);+ const firstRow = pdf.ops.find((o) =>+ o.op === 'text' && o.text[0].startsWith('firstr'));+ expect([firstRow.page, firstRow.y]).toEqual([2, 40 + 106 + 12]);+});++it('keeps the header-only check when the first row cannot fit under the header on any page', async () => {+ // 106 + 12 * 12 + 10 = 260pt, more than the 220pt between the margins,+ // so a new page would not keep them together either.+ pageHeight = 300;+ globalThis.jsPDF = PagedPdf;+ renderReturns(`<p>one</p><p>two</p><p>three</p>${cappedHeaderTable(12)}`);++ await generatePdf('Title', 'ignored');++ const pdf = pdfInstances[0];+ // The header stays on page 1; the row moves and takes a repeated header.+ expect(pdf.headerBands().map((b) => [b.page, b.y, b.h])).toEqual([[1, 100, 106], [2, 40, 106], [3, 40, 106]]);+ const firstRow = pdf.ops.find((o) =>+ o.op === 'text' && o.text[0].startsWith('firstr'));+ expect([firstRow.page, firstRow.y]).toEqual([2, 40 + 106 + 12]);+});++it('moves the header with its first row when the two exactly fill a page', async () => {+ // 106 + 8 * 12 + 10 = 212pt, exactly the space between the margins.+ pageHeight = 292;+ globalThis.jsPDF = PagedPdf;+ renderReturns(`<p>one</p><p>two</p><p>three</p>${cappedHeaderTable(8)}`);++ await generatePdf('Title', 'ignored');++ const pdf = pdfInstances[0];+ // No band on page 1; the second row, not the first, opens page 3.+ expect(pdf.headerBands().map((b) => [b.page, b.y, b.h])).toEqual([[2, 40, 106], [3, 40, 106]]);+ const firstRow = pdf.ops.find((o) =>+ o.op === 'text' && o.text[0].startsWith('firstr'));+ expect([firstRow.page, firstRow.y]).toEqual([2, 40 + 106 + 12]);+});++it('checks only the header of a table with no data rows', async () => {+ pageHeight = 300;+ globalThis.jsPDF = PagedPdf;+ const longHeader = Array.from({ length: 120 }, (_, i) =>+ `header${String(i).padStart(6, '0')}`).join(' ');+ renderReturns(`<p>one</p><p>two</p><p>three</p>+ <table><tr><th>${longHeader}</th><th>Note</th></tr></table>`);++ await generatePdf('Title', 'ignored');++ const pdf = pdfInstances[0];+ expect(pdf.headerBands().map((b) => [b.page, b.y, b.h])).toEqual([[1, 100, 106]]);+});+it('wraps long links and citations across pages without losing their targets', async () => {pageWidth = 140;pageHeight = 90;