This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
// 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user