paged-setup.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // window.runPagedIfPrinting = function () {
  2. // console.log("📄 Impression via iframe");
  3. // // 1. Créer une iframe cachée
  4. // const iframe = document.createElement("iframe");
  5. // iframe.style.position = "fixed";
  6. // iframe.style.left = "-100vw"; // hors de la vue
  7. // iframe.style.top = "-100vh";
  8. // iframe.style.width = "0";
  9. // iframe.style.height = "0";
  10. // iframe.style.visibility = "hidden";
  11. // iframe.setAttribute("aria-hidden", "true");
  12. // document.body.appendChild(iframe);
  13. // // 2. Copier tout le document dans l’iframe
  14. // const clonedHTML = document.documentElement.outerHTML;
  15. // const doc = iframe.contentWindow.document;
  16. // doc.open();
  17. // doc.write(clonedHTML);
  18. // doc.close();
  19. // // 3. Attendre que tout soit chargé
  20. // iframe.onload = () => {
  21. // console.log("📦 Iframe chargée, injection manuelle de Paged.js");
  22. // const pagedScript = iframe.contentDocument.createElement("script");
  23. // pagedScript.src = "https://unpkg.com/pagedjs/dist/paged.js";
  24. // pagedScript.onload = () => {
  25. // console.log("📚 Paged.js chargé, génération des pages...");
  26. // const previewer = new iframe.contentWindow.Paged.Previewer();
  27. // previewer.preview().then(() => {
  28. // console.log("🖨️ Aperçu prêt, impression dans iframe…");
  29. // setTimeout(() => {
  30. // const printScript = iframe.contentDocument.createElement("script");
  31. // printScript.textContent = `
  32. // window.addEventListener("load", () => {
  33. // setTimeout(() => {
  34. // window.print();
  35. // window.onafterprint = () => window.close(); // facultatif
  36. // }, 100);
  37. // });
  38. // `;
  39. // iframe.contentDocument.body.appendChild(printScript);
  40. // }, 300);
  41. // });
  42. // };
  43. // // Injecte le script dans <head> de l’iframe
  44. // iframe.contentDocument.head.appendChild(pagedScript);
  45. // };
  46. // };
  47. // // 📎 Listeners impression
  48. // function setupPagedPrintListeners() {
  49. // window.addEventListener("beforeprint", (e) => {
  50. // e.preventDefault(); // empêcher la version native
  51. // });
  52. // if (window.matchMedia) {
  53. // window.matchMedia("print").addEventListener("change", (e) => {
  54. // if (e.matches) {
  55. // window.runPagedIfPrinting();
  56. // }
  57. // });
  58. // }
  59. // console.log("✅ Paged.js listeners (iframe) installés.");
  60. // }
  61. // if (document.readyState !== "loading") {
  62. // setupPagedPrintListeners();
  63. // } else {
  64. // document.addEventListener("DOMContentLoaded", setupPagedPrintListeners);
  65. // }
  66. window.runPagedIfPrinting = function () {
  67. console.log("📄 Impression détectée — Paged.js va s'exécuter");
  68. if (typeof Paged !== "undefined" && Paged.Previewer) {
  69. // Sauvegarde du DOM actuel
  70. const original = document.body.cloneNode(true);
  71. const previewer = new Paged.Previewer();
  72. previewer.preview().then(() => {
  73. console.log("📃 Paged.js rendu terminé");
  74. const restoreDOM = () => {
  75. console.log("🔄 Restauration du DOM après impression");
  76. document.body.replaceWith(original);
  77. window.removeEventListener("afterprint", restoreDOM);
  78. };
  79. // ✅ Restauration après impression (ou annulation)
  80. window.addEventListener("afterprint", restoreDOM);
  81. });
  82. } else {
  83. console.warn("❌ Paged.js non chargé ou Previewer indisponible !");
  84. }
  85. };
  86. // Initialisation automatique
  87. function setupPagedPrintListeners() {
  88. // Classique
  89. window.addEventListener("beforeprint", window.runPagedIfPrinting);
  90. // Pour certains navigateurs modernes
  91. if (window.matchMedia) {
  92. const mediaQueryList = window.matchMedia("print");
  93. if (mediaQueryList.addEventListener) {
  94. mediaQueryList.addEventListener("change", (e) => {
  95. if (e.matches) {
  96. window.runPagedIfPrinting();
  97. }
  98. });
  99. } else {
  100. // Safari fallback
  101. mediaQueryList.addListener((e) => {
  102. if (e.matches) {
  103. window.runPagedIfPrinting();
  104. }
  105. });
  106. }
  107. }
  108. console.log("✅ Paged.js listeners installés.");
  109. }
  110. // Démarrage au bon moment
  111. if (document.readyState !== "loading") {
  112. setupPagedPrintListeners();
  113. } else {
  114. document.addEventListener("DOMContentLoaded", setupPagedPrintListeners);
  115. }