fix: make PDF action always respond with guaranteed print fallback

This commit is contained in:
2026-03-08 10:42:03 +01:00
parent 5f5250cbee
commit 2ca2d6ff97

View File

@@ -498,14 +498,17 @@ function closeAiModal(){
} }
async function downloadPdf(){ async function downloadPdf(){
const fallbackWin = window.open('', '_blank');
const openPrintFallback = (msg='') => {
if (msg) alert(msg);
if (!fallbackWin) return;
fallbackWin.document.write(`<!doctype html><html><head><meta charset="utf-8"><title>Coachingcards PDF</title></head><body>${app.innerHTML}</body></html>`);
fallbackWin.document.close();
fallbackWin.onload = () => setTimeout(() => fallbackWin.print(), 200);
};
if (!window.html2canvas || !window.jspdf?.jsPDF) { if (!window.html2canvas || !window.jspdf?.jsPDF) {
alert('PDF-Export-Bibliothek nicht geladen. Nutze Fallback-Druckdialog.'); openPrintFallback('PDF-Export-Bibliothek nicht geladen. Nutze Fallback-Druckdialog.');
const printWin = window.open('', '_blank');
if (printWin) {
printWin.document.write(`<!doctype html><html><head><meta charset="utf-8"><title>Coachingcards PDF</title></head><body>${app.innerHTML}</body></html>`);
printWin.document.close();
printWin.onload = () => setTimeout(() => printWin.print(), 200);
}
return; return;
} }
@@ -539,12 +542,15 @@ async function downloadPdf(){
const usableH = pageH - margin * 2; const usableH = pageH - margin * 2;
for (let i = 0; i < cards.length; i++) { for (let i = 0; i < cards.length; i++) {
const canvas = await window.html2canvas(cards[i], { const canvas = await Promise.race([
window.html2canvas(cards[i], {
scale: 2, scale: 2,
useCORS: true, useCORS: true,
backgroundColor: '#ffffff', backgroundColor: '#ffffff',
logging: false logging: false
}); }),
new Promise((_, rej) => setTimeout(() => rej(new Error('Render timeout')), 15000))
]);
const img = canvas.toDataURL('image/jpeg', 0.98); const img = canvas.toDataURL('image/jpeg', 0.98);
const imgW = canvas.width; const imgW = canvas.width;
const imgH = canvas.height; const imgH = canvas.height;
@@ -559,15 +565,10 @@ async function downloadPdf(){
} }
pdf.save(`coachingcards-${new Date().toISOString().slice(0,10)}.pdf`); pdf.save(`coachingcards-${new Date().toISOString().slice(0,10)}.pdf`);
if (fallbackWin && !fallbackWin.closed) fallbackWin.close();
} catch (e) { } catch (e) {
console.error('PDF export failed', e); console.error('PDF export failed', e);
alert('PDF-Export fehlgeschlagen öffne Druckdialog als Fallback.'); openPrintFallback('PDF-Export fehlgeschlagen öffne Druckdialog als Fallback.');
const printWin = window.open('', '_blank');
if (printWin) {
printWin.document.write(`<!doctype html><html><head><meta charset="utf-8"><title>Coachingcards PDF</title></head><body>${app.innerHTML}</body></html>`);
printWin.document.close();
printWin.onload = () => setTimeout(() => printWin.print(), 200);
}
} finally { } finally {
root.remove(); root.remove();
} }