fix: make PDF action always respond with guaranteed print fallback
This commit is contained in:
@@ -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(`<!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) {
|
||||
alert('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);
|
||||
}
|
||||
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], {
|
||||
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(`<!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);
|
||||
}
|
||||
openPrintFallback('PDF-Export fehlgeschlagen – öffne Druckdialog als Fallback.');
|
||||
} finally {
|
||||
root.remove();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user