// window.runPagedIfPrinting = function () { // console.log("📄 Impression via iframe"); // // 1. CrĂ©er une iframe cachĂ©e // const iframe = document.createElement("iframe"); // iframe.style.position = "fixed"; // iframe.style.left = "-100vw"; // hors de la vue // iframe.style.top = "-100vh"; // iframe.style.width = "0"; // iframe.style.height = "0"; // iframe.style.visibility = "hidden"; // iframe.setAttribute("aria-hidden", "true"); // document.body.appendChild(iframe); // // 2. Copier tout le document dans l’iframe // const clonedHTML = document.documentElement.outerHTML; // const doc = iframe.contentWindow.document; // doc.open(); // doc.write(clonedHTML); // doc.close(); // // 3. Attendre que tout soit chargĂ© // iframe.onload = () => { // console.log("📩 Iframe chargĂ©e, injection manuelle de Paged.js"); // const pagedScript = iframe.contentDocument.createElement("script"); // pagedScript.src = "https://unpkg.com/pagedjs/dist/paged.js"; // pagedScript.onload = () => { // console.log("📚 Paged.js chargĂ©, gĂ©nĂ©ration des pages..."); // const previewer = new iframe.contentWindow.Paged.Previewer(); // previewer.preview().then(() => { // console.log("đŸ–šïž Aperçu prĂȘt, impression dans iframe
"); // setTimeout(() => { // const printScript = iframe.contentDocument.createElement("script"); // printScript.textContent = ` // window.addEventListener("load", () => { // setTimeout(() => { // window.print(); // window.onafterprint = () => window.close(); // facultatif // }, 100); // }); // `; // iframe.contentDocument.body.appendChild(printScript); // }, 300); // }); // }; // // Injecte le script dans de l’iframe // iframe.contentDocument.head.appendChild(pagedScript); // }; // }; // // 📎 Listeners impression // function setupPagedPrintListeners() { // window.addEventListener("beforeprint", (e) => { // e.preventDefault(); // empĂȘcher la version native // }); // if (window.matchMedia) { // window.matchMedia("print").addEventListener("change", (e) => { // if (e.matches) { // window.runPagedIfPrinting(); // } // }); // } // console.log("✅ Paged.js listeners (iframe) installĂ©s."); // } // if (document.readyState !== "loading") { // setupPagedPrintListeners(); // } else { // document.addEventListener("DOMContentLoaded", setupPagedPrintListeners); // } window.runPagedIfPrinting = function () { console.log("📄 Impression dĂ©tectĂ©e — Paged.js va s'exĂ©cuter"); if (typeof Paged !== "undefined" && Paged.Previewer) { // Sauvegarde du DOM actuel const original = document.body.cloneNode(true); const previewer = new Paged.Previewer(); previewer.preview().then(() => { console.log("📃 Paged.js rendu terminĂ©"); const restoreDOM = () => { console.log("🔄 Restauration du DOM aprĂšs impression"); document.body.replaceWith(original); window.removeEventListener("afterprint", restoreDOM); }; // ✅ Restauration aprĂšs impression (ou annulation) window.addEventListener("afterprint", restoreDOM); }); } else { console.warn("❌ Paged.js non chargĂ© ou Previewer indisponible !"); } }; // Initialisation automatique function setupPagedPrintListeners() { // Classique window.addEventListener("beforeprint", window.runPagedIfPrinting); // Pour certains navigateurs modernes if (window.matchMedia) { const mediaQueryList = window.matchMedia("print"); if (mediaQueryList.addEventListener) { mediaQueryList.addEventListener("change", (e) => { if (e.matches) { window.runPagedIfPrinting(); } }); } else { // Safari fallback mediaQueryList.addListener((e) => { if (e.matches) { window.runPagedIfPrinting(); } }); } } console.log("✅ Paged.js listeners installĂ©s."); } // DĂ©marrage au bon moment if (document.readyState !== "loading") { setupPagedPrintListeners(); } else { document.addEventListener("DOMContentLoaded", setupPagedPrintListeners); }