fix: PDF export using hidden iframe for reliable print dialog
This commit is contained in:
@@ -240,14 +240,26 @@ export function downloadSeoReport(d: SeoAuditData, format: "html" | "pdf") {
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
} else {
|
||||
const blob = new Blob([html], { type: "text/html" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const win = window.open(url, "_blank");
|
||||
if (win) {
|
||||
win.addEventListener("load", () => {
|
||||
setTimeout(() => win.print(), 300);
|
||||
});
|
||||
const iframe = document.createElement("iframe");
|
||||
iframe.style.position = "fixed";
|
||||
iframe.style.right = "0";
|
||||
iframe.style.bottom = "0";
|
||||
iframe.style.width = "0";
|
||||
iframe.style.height = "0";
|
||||
iframe.style.border = "none";
|
||||
document.body.appendChild(iframe);
|
||||
const doc = iframe.contentDocument || iframe.contentWindow?.document;
|
||||
if (doc) {
|
||||
doc.open();
|
||||
doc.write(html);
|
||||
doc.close();
|
||||
iframe.onload = () => {
|
||||
setTimeout(() => {
|
||||
iframe.contentWindow?.focus();
|
||||
iframe.contentWindow?.print();
|
||||
setTimeout(() => document.body.removeChild(iframe), 5000);
|
||||
}, 600);
|
||||
};
|
||||
}
|
||||
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user