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): render a table that directly follows text as a table (#8124)

dodoshell772f7d502bf9
diff --git a/changelog.d/8096.bugfix.md b/changelog.d/8096.bugfix.md
new file mode 100644
index 0000000000..c2c5e8a1a6
--- /dev/null
+++ b/changelog.d/8096.bugfix.md
@@ -0,0 +1 @@
+PDF export now renders a Markdown table that directly follows a line of text as a table, as the report view does, instead of printing its pipes and separator dashes as text.
diff --git a/src/local_deep_research/web/services/pdf_service.py b/src/local_deep_research/web/services/pdf_service.py
index ec13c6e00a..916d67269b 100644
--- a/src/local_deep_research/web/services/pdf_service.py
+++ b/src/local_deep_research/web/services/pdf_service.py
@@ -26,6 +26,7 @@ from urllib.parse import unquote, urlsplit
import markdown # type: ignore[import-untyped]
from loguru import logger
+from markdown.preprocessors import Preprocessor # type: ignore[import-untyped]
from ...security import dns_pinning, redact_url_for_log, validate_url
from ...utilities.resource_utils import safe_close
@@ -2508,6 +2509,40 @@ a {
"""
+class _TableAfterText(Preprocessor):
+ """Put a blank line before a table that directly follows a line of text.
+
+ Python-Markdown's ``tables`` extension only finds a table at the start of
+ a block, so a table written right under a paragraph reached the PDF as
+ pipe text, while the report view (marked, GFM) shows it as a table
+ (#8096). A table start is whatever the extension itself accepts as one.
+ This runs after ``fenced_code`` has stashed code blocks, so pipes inside
+ them are left alone. A line indented by a tab stop or more is never a
+ table start: it belongs to an indented code block, or continues the
+ paragraph above it.
+ """
+
+ def run(self, lines):
+ table = self.md.parser.blockprocessors["table"]
+ indent = " " * self.md.tab_length
+ out = []
+ in_table = False
+ for i, line in enumerate(lines):
+ if not line.strip():
+ in_table = False
+ elif (
+ not in_table
+ and not line.startswith(indent)
+ and i + 1 < len(lines)
+ and table.test(None, f"{line}\n{lines[i + 1]}")
+ ):
+ if out and out[-1].strip():
+ out.append("")
+ in_table = True
+ out.append(line)
+ return out
+
+
class PDFService:
"""Service for converting markdown to PDF using WeasyPrint."""
@@ -2763,6 +2798,8 @@ class PDFService:
# Markdown 3.x.
md.preprocessors.deregister("html_block")
md.inlinePatterns.deregister("html")
+ # Priority 20: after fenced_code (25), see _TableAfterText.
+ md.preprocessors.register(_TableAfterText(md), "table_after_text", 20)
html_body = md.convert(markdown_content)
diff --git a/tests/web/services/test_pdf_service.py b/tests/web/services/test_pdf_service.py
index 8703050574..d0afc6d938 100644
--- a/tests/web/services/test_pdf_service.py
+++ b/tests/web/services/test_pdf_service.py
@@ -456,6 +456,94 @@ class TestRawHtmlInReportTextIsEscaped:
assert "Marker Author" in text, text[:400]
+class TestTableAfterText:
+ """A table right under a line of text is a table in the PDF too (#8096).
+
+ The report view (marked, GFM) reads a table that directly follows a
+ paragraph line as a table; Python-Markdown only did so after a blank
+ line, so the PDF showed the pipes and separator dashes as text.
+ """
+
+ TABLE = "| Item | Description |\n|------|-------------|\n| A | First |\n"
+
+ def _html(self, markdown):
+ from local_deep_research.web.services.pdf_service import PDFService
+
+ return PDFService()._markdown_to_html(markdown)
+
+ @pytest.mark.parametrize(
+ "markdown",
+ [
+ "## Example\n\nIntroductory text.\n" + TABLE,
+ "## Example\n\nIntroductory text.\n\n" + TABLE,
+ "Introductory text:\n" + TABLE + "\nAfter the table.\n",
+ ],
+ ids=["adjacent", "blank_line", "text_after"],
+ )
+ def test_table_after_text_is_a_table(self, markdown):
+ html = self._html(markdown)
+
+ assert "<p>Introductory text" in html
+ assert "<th>Item</th>" in html
+ assert "<td>First</td>" in html
+ assert "|---" not in html
+
+ def test_several_adjacent_tables(self):
+ html = self._html(
+ "First.\n| A | B |\n|---|---|\n| 1 | 2 |\n\n"
+ "Second.\n| C | D |\n|---|---|\n| 3 | 4 |\n"
+ )
+
+ assert html.count("<table>") == 2
+ assert "<p>First.</p>" in html
+ assert "<p>Second.</p>" in html
+ assert "<td>4</td>" in html
+
+ def test_dash_only_row_stays_in_its_table(self):
+ """A html row of dashes looks like a separator row; it must not be
+ read as the start of a second table."""
+ html = self._html("Text.\n| A | B |\n|---|---|\n| - | - |\n| 1 | 2 |\n")
+
+ assert html.count("<table>") == 1
+ assert "<td>-</td>" in html
+ assert "<td>1</td>" in html
+
+ def test_pipes_in_fenced_code_stay_code(self):
+ html = self._html("Text.\n```\nIntro.\n| A | B |\n|---|---|\n```\n")
+
+ assert "<table>" not in html
+ assert "<code>Intro.\n| A | B |\n|---|---|\n</code>" in html
+
+ def test_indented_code_text_is_unchanged(self):
+ """fenced_code does not stash indented code, so a table example in
+ it must not get a blank line inserted into the code text."""
+ html = self._html("Text.\n\n Intro.\n | A | B |\n |---|---|\n")
+
+ assert "<table>" not in html
+ assert "<pre><code>Intro.\n| A | B |\n|---|---|\n</code></pre>" in html
+
+ def test_text_with_pipes_is_not_a_table(self):
+ html = self._html("a | b\nc | d\n")
+
+ assert "<table>" not in html
+ assert "a | b" in html
+
+ def test_pdf_shows_no_table_markup(self):
+ from pypdf import PdfReader
+
+ from local_deep_research.web.services.pdf_service import PDFService
+
+ pdf_bytes = PDFService().markdown_to_pdf(
+ "## Example\n\nIntroductory text.\n" + self.TABLE
+ )
+
+ reader = PdfReader(io.BytesIO(pdf_bytes))
+ text = "\n".join(page.extract_text() or "" for page in reader.pages)
+ assert "First" in text, text[:400]
+ assert "| Item" not in text, text[:400]
+ assert "---" not in text, text[:400]
+
+
class TestMarkdownToPDF:
"""Tests for markdown_to_pdf method."""