pdf_print_service.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFPrintService = PDFPrintService;
  27. var _pdf = require("../pdf");
  28. var _app = require("./app.js");
  29. var _print_utils = require("./print_utils.js");
  30. let activeService = null;
  31. let dialog = null;
  32. let overlayManager = null;
  33. function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size, printResolution, optionalContentConfigPromise) {
  34. const scratchCanvas = activeService.scratchCanvas;
  35. const PRINT_UNITS = printResolution / _pdf.PixelsPerInch.PDF;
  36. scratchCanvas.width = Math.floor(size.width * PRINT_UNITS);
  37. scratchCanvas.height = Math.floor(size.height * PRINT_UNITS);
  38. const ctx = scratchCanvas.getContext("2d");
  39. ctx.save();
  40. ctx.fillStyle = "rgb(255, 255, 255)";
  41. ctx.fillRect(0, 0, scratchCanvas.width, scratchCanvas.height);
  42. ctx.restore();
  43. return pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  44. const renderContext = {
  45. canvasContext: ctx,
  46. transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
  47. viewport: pdfPage.getViewport({
  48. scale: 1,
  49. rotation: size.rotation
  50. }),
  51. intent: "print",
  52. annotationMode: _pdf.AnnotationMode.ENABLE_STORAGE,
  53. optionalContentConfigPromise
  54. };
  55. return pdfPage.render(renderContext).promise;
  56. });
  57. }
  58. function PDFPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise = null, l10n) {
  59. this.pdfDocument = pdfDocument;
  60. this.pagesOverview = pagesOverview;
  61. this.printContainer = printContainer;
  62. this._printResolution = printResolution || 150;
  63. this._optionalContentConfigPromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
  64. this.l10n = l10n;
  65. this.currentPage = -1;
  66. this.scratchCanvas = document.createElement("canvas");
  67. }
  68. PDFPrintService.prototype = {
  69. layout() {
  70. this.throwIfInactive();
  71. const body = document.querySelector("body");
  72. body.setAttribute("data-pdfjsprinting", true);
  73. const hasEqualPageSizes = this.pagesOverview.every(function (size) {
  74. return size.width === this.pagesOverview[0].width && size.height === this.pagesOverview[0].height;
  75. }, this);
  76. if (!hasEqualPageSizes) {
  77. console.warn("Not all pages have the same size. The printed " + "result may be incorrect!");
  78. }
  79. this.pageStyleSheet = document.createElement("style");
  80. const pageSize = this.pagesOverview[0];
  81. this.pageStyleSheet.textContent = "@page { size: " + pageSize.width + "pt " + pageSize.height + "pt;}";
  82. body.appendChild(this.pageStyleSheet);
  83. },
  84. destroy() {
  85. if (activeService !== this) {
  86. return;
  87. }
  88. this.printContainer.textContent = "";
  89. const body = document.querySelector("body");
  90. body.removeAttribute("data-pdfjsprinting");
  91. if (this.pageStyleSheet) {
  92. this.pageStyleSheet.remove();
  93. this.pageStyleSheet = null;
  94. }
  95. this.scratchCanvas.width = this.scratchCanvas.height = 0;
  96. this.scratchCanvas = null;
  97. activeService = null;
  98. ensureOverlay().then(function () {
  99. if (overlayManager.active === dialog) {
  100. overlayManager.close(dialog);
  101. }
  102. });
  103. },
  104. renderPages() {
  105. if (this.pdfDocument.isPureXfa) {
  106. (0, _print_utils.getXfaHtmlForPrinting)(this.printContainer, this.pdfDocument);
  107. return Promise.resolve();
  108. }
  109. const pageCount = this.pagesOverview.length;
  110. const renderNextPage = (resolve, reject) => {
  111. this.throwIfInactive();
  112. if (++this.currentPage >= pageCount) {
  113. renderProgress(pageCount, pageCount, this.l10n);
  114. resolve();
  115. return;
  116. }
  117. const index = this.currentPage;
  118. renderProgress(index, pageCount, this.l10n);
  119. renderPage(this, this.pdfDocument, index + 1, this.pagesOverview[index], this._printResolution, this._optionalContentConfigPromise).then(this.useRenderedPage.bind(this)).then(function () {
  120. renderNextPage(resolve, reject);
  121. }, reject);
  122. };
  123. return new Promise(renderNextPage);
  124. },
  125. useRenderedPage() {
  126. this.throwIfInactive();
  127. const img = document.createElement("img");
  128. const scratchCanvas = this.scratchCanvas;
  129. if ("toBlob" in scratchCanvas) {
  130. scratchCanvas.toBlob(function (blob) {
  131. img.src = URL.createObjectURL(blob);
  132. });
  133. } else {
  134. img.src = scratchCanvas.toDataURL();
  135. }
  136. const wrapper = document.createElement("div");
  137. wrapper.className = "printedPage";
  138. wrapper.appendChild(img);
  139. this.printContainer.appendChild(wrapper);
  140. return new Promise(function (resolve, reject) {
  141. img.onload = resolve;
  142. img.onerror = reject;
  143. });
  144. },
  145. performPrint() {
  146. this.throwIfInactive();
  147. return new Promise(resolve => {
  148. setTimeout(() => {
  149. if (!this.active) {
  150. resolve();
  151. return;
  152. }
  153. print.call(window);
  154. setTimeout(resolve, 20);
  155. }, 0);
  156. });
  157. },
  158. get active() {
  159. return this === activeService;
  160. },
  161. throwIfInactive() {
  162. if (!this.active) {
  163. throw new Error("This print request was cancelled or completed.");
  164. }
  165. }
  166. };
  167. const print = window.print;
  168. window.print = function () {
  169. if (activeService) {
  170. console.warn("Ignored window.print() because of a pending print job.");
  171. return;
  172. }
  173. ensureOverlay().then(function () {
  174. if (activeService) {
  175. overlayManager.open(dialog);
  176. }
  177. });
  178. try {
  179. dispatchEvent("beforeprint");
  180. } finally {
  181. if (!activeService) {
  182. console.error("Expected print service to be initialized.");
  183. ensureOverlay().then(function () {
  184. if (overlayManager.active === dialog) {
  185. overlayManager.close(dialog);
  186. }
  187. });
  188. return;
  189. }
  190. const activeServiceOnEntry = activeService;
  191. activeService.renderPages().then(function () {
  192. return activeServiceOnEntry.performPrint();
  193. }).catch(function () {}).then(function () {
  194. if (activeServiceOnEntry.active) {
  195. abort();
  196. }
  197. });
  198. }
  199. };
  200. function dispatchEvent(eventType) {
  201. const event = document.createEvent("CustomEvent");
  202. event.initCustomEvent(eventType, false, false, "custom");
  203. window.dispatchEvent(event);
  204. }
  205. function abort() {
  206. if (activeService) {
  207. activeService.destroy();
  208. dispatchEvent("afterprint");
  209. }
  210. }
  211. function renderProgress(index, total, l10n) {
  212. dialog ||= document.getElementById("printServiceDialog");
  213. const progress = Math.round(100 * index / total);
  214. const progressBar = dialog.querySelector("progress");
  215. const progressPerc = dialog.querySelector(".relative-progress");
  216. progressBar.value = progress;
  217. l10n.get("print_progress_percent", {
  218. progress
  219. }).then(msg => {
  220. progressPerc.textContent = msg;
  221. });
  222. }
  223. window.addEventListener("keydown", function (event) {
  224. if (event.keyCode === 80 && (event.ctrlKey || event.metaKey) && !event.altKey && (!event.shiftKey || window.chrome || window.opera)) {
  225. window.print();
  226. event.preventDefault();
  227. if (event.stopImmediatePropagation) {
  228. event.stopImmediatePropagation();
  229. } else {
  230. event.stopPropagation();
  231. }
  232. }
  233. }, true);
  234. if ("onbeforeprint" in window) {
  235. const stopPropagationIfNeeded = function (event) {
  236. if (event.detail !== "custom" && event.stopImmediatePropagation) {
  237. event.stopImmediatePropagation();
  238. }
  239. };
  240. window.addEventListener("beforeprint", stopPropagationIfNeeded);
  241. window.addEventListener("afterprint", stopPropagationIfNeeded);
  242. }
  243. let overlayPromise;
  244. function ensureOverlay() {
  245. if (!overlayPromise) {
  246. overlayManager = _app.PDFViewerApplication.overlayManager;
  247. if (!overlayManager) {
  248. throw new Error("The overlay manager has not yet been initialized.");
  249. }
  250. dialog ||= document.getElementById("printServiceDialog");
  251. overlayPromise = overlayManager.register(dialog, true);
  252. document.getElementById("printCancel").onclick = abort;
  253. dialog.addEventListener("close", abort);
  254. }
  255. return overlayPromise;
  256. }
  257. _app.PDFPrintServiceFactory.instance = {
  258. supportsPrinting: true,
  259. createPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, l10n) {
  260. if (activeService) {
  261. throw new Error("The print service is created and active.");
  262. }
  263. activeService = new PDFPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, l10n);
  264. return activeService;
  265. }
  266. };