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): remove the offscreen container when content building throws (#7648)

HPH Protoneb90f9c77c50
diff --git a/changelog.d/7639.bugfix.md b/changelog.d/7639.bugfix.md
new file mode 100644
index 0000000000..76f74d17f1
--- /dev/null
+++ b/changelog.d/7639.bugfix.md
@@ -0,0 +1 @@
+Exporting a report to PDF no longer leaves the offscreen report container attached to the page when building the PDF body fails.
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 137d27bc69..6981f1c015 100644
--- a/src/local_deep_research/web/static/js/services/pdf.js
+++ b/src/local_deep_research/web/static/js/services/pdf.js
@@ -208,8 +208,10 @@ async function generatePdf(title, content, _metadata = {}) {
// Add to the document body temporarily
document.body.appendChild(tempContainer);
- // Now parse and add the content
+ // Now parse and add the content. Sanitizer failures remove the
+ // offscreen container themselves and throw before PDF generation.
const pdfBodyDiv = tempContainer.querySelector('#pdf-body-content');
+ let renderedHtml;
if (pdfBodyDiv) {
if (window.marked && typeof window.marked.parse === 'function') {
// isSupported must be exactly true: an unsupported DOMPurify
@@ -233,12 +235,26 @@ async function generatePdf(title, content, _metadata = {}) {
document.body.removeChild(tempContainer);
throw new Error('Markdown sanitizer not loaded. Cannot generate PDF safely.');
}
- const renderedHtml = window.ui.renderMarkdown(content);
+ renderedHtml = window.ui.renderMarkdown(content);
if (typeof renderedHtml !== 'string'
|| !renderedHtml.startsWith(RENDERED_MARKDOWN_PREFIX)) {
document.body.removeChild(tempContainer);
throw new Error('Markdown rendering failed. Cannot generate PDF safely.');
}
+ } else {
+ // marked.js not available - this is a critical error for PDF generation
+ document.body.removeChild(tempContainer);
+ throw new Error('Markdown parser (marked.js) not loaded. Cannot generate PDF.');
+ }
+ } else {
+ document.body.removeChild(tempContainer);
+ throw new Error('PDF body container not found in template');
+ }
+
+ try {
+ // Body construction can throw. It stays in this try so the finally
+ // removes the offscreen .ldr-pdf-content container.
+ if (pdfBodyDiv && renderedHtml !== undefined) {
// Parse the already-sanitized string in an inert <template>
// (no script execution, no resource loading) and move its nodes
// into the live container, so no HTML string is ever assigned
@@ -264,17 +280,8 @@ async function generatePdf(title, content, _metadata = {}) {
}
wrapTopLevelText(pdfBodyDiv);
replaceKatexWithLatex(pdfBodyDiv);
- } else {
- // marked.js not available - this is a critical error for PDF generation
- document.body.removeChild(tempContainer);
- throw new Error('Markdown parser (marked.js) not loaded. Cannot generate PDF.');
}
- } else {
- document.body.removeChild(tempContainer);
- throw new Error('PDF body container not found in template');
- }
- try {
// Create a new PDF document
const pdf = new jsPDFLib('p', 'pt', 'letter');
const pdfWidth = pdf.internal.pageSize.getWidth();
diff --git a/tests/js/services/pdf-runtime.test.js b/tests/js/services/pdf-runtime.test.js
index 2d745e1f77..89d6c8e25f 100644
--- a/tests/js/services/pdf-runtime.test.js
+++ b/tests/js/services/pdf-runtime.test.js
@@ -566,3 +566,77 @@ it('reports generation failures and always removes transient browser state', asy
expect(document.querySelector('.ldr-loading-indicator')).toBeNull();
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
+
+it('removes the offscreen container when moving the parsed report throws', async () => {
+ renderReturns('<p>Huge report</p>');
+ const original = Node.prototype.appendChild;
+ vi.spyOn(Node.prototype, 'appendChild').mockImplementation(function (child) {
+ if (this.id === 'pdf-body-content' && this.isConnected) {
+ throw new Error('body move failed');
+ }
+ return original.call(this, child);
+ });
+
+ await expect(generatePdf('Huge report', '# ignored')).rejects.toThrow(
+ 'body move failed',
+ );
+
+ expect(document.querySelector('.ldr-pdf-content')).toBeNull();
+});
+
+it('rejects a missing PDF body container and removes the offscreen node', async () => {
+ const original = Element.prototype.querySelector;
+ vi.spyOn(Element.prototype, 'querySelector').mockImplementation(function (selector) {
+ if (selector === '#pdf-body-content') {
+ return null;
+ }
+ return original.call(this, selector);
+ });
+
+ await expect(generatePdf('Title', '# Report')).rejects.toThrow(
+ 'PDF body container not found in template',
+ );
+
+ expect(document.querySelector('.ldr-pdf-content')).toBeNull();
+ expect(pdfInstances).toHaveLength(0);
+});
+
+it('removes the offscreen container when wrapping top-level text throws', async () => {
+ renderReturns('bare text that the PDF walker would otherwise drop');
+ const original = Node.prototype.insertBefore;
+ vi.spyOn(Node.prototype, 'insertBefore').mockImplementation(function (node, child) {
+ if (this.id === 'pdf-body-content' && this.isConnected
+ && child?.nodeType === Node.TEXT_NODE) {
+ throw new Error('text wrap failed');
+ }
+ return original.call(this, node, child);
+ });
+
+ await expect(generatePdf('Report', 'bare text')).rejects.toThrow(
+ 'text wrap failed',
+ );
+
+ expect(document.querySelector('.ldr-pdf-content')).toBeNull();
+});
+
+it('removes the offscreen container when rewriting KaTeX throws', async () => {
+ renderReturns(
+ '<span class="katex-display"><annotation encoding="application/x-tex">x</annotation></span>',
+ );
+ const original = Element.prototype.querySelectorAll;
+ vi.spyOn(Element.prototype, 'querySelectorAll').mockImplementation(
+ function (selector) {
+ if (this.id === 'pdf-body-content' && selector === '.katex-display') {
+ throw new Error('katex walk failed');
+ }
+ return original.call(this, selector);
+ },
+ );
+
+ await expect(generatePdf('Report', '$$x$$')).rejects.toThrow(
+ 'katex walk failed',
+ );
+
+ expect(document.querySelector('.ldr-pdf-content')).toBeNull();
+ expect(pdfInstances).toHaveLength(0);
+});