123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- // 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 <head> 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);
- }
-
|