From 2ca2d6ff976432a188603723718cedbf6c75c416 Mon Sep 17 00:00:00 2001 From: OpenClaw Bot Date: Sun, 8 Mar 2026 10:42:03 +0100 Subject: [PATCH] fix: make PDF action always respond with guaranteed print fallback --- public/index.html | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/public/index.html b/public/index.html index b009c35..6d8864f 100644 --- a/public/index.html +++ b/public/index.html @@ -498,14 +498,17 @@ function closeAiModal(){ } async function downloadPdf(){ + const fallbackWin = window.open('', '_blank'); + const openPrintFallback = (msg='') => { + if (msg) alert(msg); + if (!fallbackWin) return; + fallbackWin.document.write(`Coachingcards PDF${app.innerHTML}`); + fallbackWin.document.close(); + fallbackWin.onload = () => setTimeout(() => fallbackWin.print(), 200); + }; + if (!window.html2canvas || !window.jspdf?.jsPDF) { - alert('PDF-Export-Bibliothek nicht geladen. Nutze Fallback-Druckdialog.'); - const printWin = window.open('', '_blank'); - if (printWin) { - printWin.document.write(`Coachingcards PDF${app.innerHTML}`); - printWin.document.close(); - printWin.onload = () => setTimeout(() => printWin.print(), 200); - } + openPrintFallback('PDF-Export-Bibliothek nicht geladen. Nutze Fallback-Druckdialog.'); return; } @@ -539,12 +542,15 @@ async function downloadPdf(){ const usableH = pageH - margin * 2; for (let i = 0; i < cards.length; i++) { - const canvas = await window.html2canvas(cards[i], { - scale: 2, - useCORS: true, - backgroundColor: '#ffffff', - logging: false - }); + const canvas = await Promise.race([ + window.html2canvas(cards[i], { + scale: 2, + useCORS: true, + backgroundColor: '#ffffff', + logging: false + }), + new Promise((_, rej) => setTimeout(() => rej(new Error('Render timeout')), 15000)) + ]); const img = canvas.toDataURL('image/jpeg', 0.98); const imgW = canvas.width; const imgH = canvas.height; @@ -559,15 +565,10 @@ async function downloadPdf(){ } pdf.save(`coachingcards-${new Date().toISOString().slice(0,10)}.pdf`); + if (fallbackWin && !fallbackWin.closed) fallbackWin.close(); } catch (e) { console.error('PDF export failed', e); - alert('PDF-Export fehlgeschlagen – öffne Druckdialog als Fallback.'); - const printWin = window.open('', '_blank'); - if (printWin) { - printWin.document.write(`Coachingcards PDF${app.innerHTML}`); - printWin.document.close(); - printWin.onload = () => setTimeout(() => printWin.print(), 200); - } + openPrintFallback('PDF-Export fehlgeschlagen – öffne Druckdialog als Fallback.'); } finally { root.remove(); }