2
0

pdf_print_service.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, printAnnotationStoragePromise) {
  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 Promise.all([pdfDocument.getPage(pageNumber), printAnnotationStoragePromise]).then(function ([pdfPage, printAnnotationStorage]) {
  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. printAnnotationStorage
  55. };
  56. return pdfPage.render(renderContext).promise;
  57. });
  58. }
  59. function PDFPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise = null, printAnnotationStoragePromise = null, l10n) {
  60. this.pdfDocument = pdfDocument;
  61. this.pagesOverview = pagesOverview;
  62. this.printContainer = printContainer;
  63. this._printResolution = printResolution || 150;
  64. this._optionalContentConfigPromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
  65. this._printAnnotationStoragePromise = printAnnotationStoragePromise || Promise.resolve();
  66. this.l10n = l10n;
  67. this.currentPage = -1;
  68. this.scratchCanvas = document.createElement("canvas");
  69. }
  70. PDFPrintService.prototype = {
  71. layout() {
  72. this.throwIfInactive();
  73. const body = document.querySelector("body");
  74. body.setAttribute("data-pdfjsprinting", true);
  75. const hasEqualPageSizes = this.pagesOverview.every(function (size) {
  76. return size.width === this.pagesOverview[0].width && size.height === this.pagesOverview[0].height;
  77. }, this);
  78. if (!hasEqualPageSizes) {
  79. console.warn("Not all pages have the same size. The printed " + "result may be incorrect!");
  80. }
  81. this.pageStyleSheet = document.createElement("style");
  82. const pageSize = this.pagesOverview[0];
  83. this.pageStyleSheet.textContent = "@page { size: " + pageSize.width + "pt " + pageSize.height + "pt;}";
  84. body.append(this.pageStyleSheet);
  85. },
  86. destroy() {
  87. if (activeService !== this) {
  88. return;
  89. }
  90. this.printContainer.textContent = "";
  91. const body = document.querySelector("body");
  92. body.removeAttribute("data-pdfjsprinting");
  93. if (this.pageStyleSheet) {
  94. this.pageStyleSheet.remove();
  95. this.pageStyleSheet = null;
  96. }
  97. this.scratchCanvas.width = this.scratchCanvas.height = 0;
  98. this.scratchCanvas = null;
  99. activeService = null;
  100. ensureOverlay().then(function () {
  101. if (overlayManager.active === dialog) {
  102. overlayManager.close(dialog);
  103. }
  104. });
  105. },
  106. renderPages() {
  107. if (this.pdfDocument.isPureXfa) {
  108. (0, _print_utils.getXfaHtmlForPrinting)(this.printContainer, this.pdfDocument);
  109. return Promise.resolve();
  110. }
  111. const pageCount = this.pagesOverview.length;
  112. const renderNextPage = (resolve, reject) => {
  113. this.throwIfInactive();
  114. if (++this.currentPage >= pageCount) {
  115. renderProgress(pageCount, pageCount, this.l10n);
  116. resolve();
  117. return;
  118. }
  119. const index = this.currentPage;
  120. renderProgress(index, pageCount, this.l10n);
  121. renderPage(this, this.pdfDocument, index + 1, this.pagesOverview[index], this._printResolution, this._optionalContentConfigPromise, this._printAnnotationStoragePromise).then(this.useRenderedPage.bind(this)).then(function () {
  122. renderNextPage(resolve, reject);
  123. }, reject);
  124. };
  125. return new Promise(renderNextPage);
  126. },
  127. useRenderedPage() {
  128. this.throwIfInactive();
  129. const img = document.createElement("img");
  130. const scratchCanvas = this.scratchCanvas;
  131. if ("toBlob" in scratchCanvas) {
  132. scratchCanvas.toBlob(function (blob) {
  133. img.src = URL.createObjectURL(blob);
  134. });
  135. } else {
  136. img.src = scratchCanvas.toDataURL();
  137. }
  138. const wrapper = document.createElement("div");
  139. wrapper.className = "printedPage";
  140. wrapper.append(img);
  141. this.printContainer.append(wrapper);
  142. return new Promise(function (resolve, reject) {
  143. img.onload = resolve;
  144. img.onerror = reject;
  145. });
  146. },
  147. performPrint() {
  148. this.throwIfInactive();
  149. return new Promise(resolve => {
  150. setTimeout(() => {
  151. if (!this.active) {
  152. resolve();
  153. return;
  154. }
  155. print.call(window);
  156. setTimeout(resolve, 20);
  157. }, 0);
  158. });
  159. },
  160. get active() {
  161. return this === activeService;
  162. },
  163. throwIfInactive() {
  164. if (!this.active) {
  165. throw new Error("This print request was cancelled or completed.");
  166. }
  167. }
  168. };
  169. const print = window.print;
  170. window.print = function () {
  171. if (activeService) {
  172. console.warn("Ignored window.print() because of a pending print job.");
  173. return;
  174. }
  175. ensureOverlay().then(function () {
  176. if (activeService) {
  177. overlayManager.open(dialog);
  178. }
  179. });
  180. try {
  181. dispatchEvent("beforeprint");
  182. } finally {
  183. if (!activeService) {
  184. console.error("Expected print service to be initialized.");
  185. ensureOverlay().then(function () {
  186. if (overlayManager.active === dialog) {
  187. overlayManager.close(dialog);
  188. }
  189. });
  190. return;
  191. }
  192. const activeServiceOnEntry = activeService;
  193. activeService.renderPages().then(function () {
  194. return activeServiceOnEntry.performPrint();
  195. }).catch(function () {}).then(function () {
  196. if (activeServiceOnEntry.active) {
  197. abort();
  198. }
  199. });
  200. }
  201. };
  202. function dispatchEvent(eventType) {
  203. const event = document.createEvent("CustomEvent");
  204. event.initCustomEvent(eventType, false, false, "custom");
  205. window.dispatchEvent(event);
  206. }
  207. function abort() {
  208. if (activeService) {
  209. activeService.destroy();
  210. dispatchEvent("afterprint");
  211. }
  212. }
  213. function renderProgress(index, total, l10n) {
  214. dialog ||= document.getElementById("printServiceDialog");
  215. const progress = Math.round(100 * index / total);
  216. const progressBar = dialog.querySelector("progress");
  217. const progressPerc = dialog.querySelector(".relative-progress");
  218. progressBar.value = progress;
  219. l10n.get("print_progress_percent", {
  220. progress
  221. }).then(msg => {
  222. progressPerc.textContent = msg;
  223. });
  224. }
  225. window.addEventListener("keydown", function (event) {
  226. if (event.keyCode === 80 && (event.ctrlKey || event.metaKey) && !event.altKey && (!event.shiftKey || window.chrome || window.opera)) {
  227. window.print();
  228. event.preventDefault();
  229. event.stopImmediatePropagation();
  230. }
  231. }, true);
  232. if ("onbeforeprint" in window) {
  233. const stopPropagationIfNeeded = function (event) {
  234. if (event.detail !== "custom") {
  235. event.stopImmediatePropagation();
  236. }
  237. };
  238. window.addEventListener("beforeprint", stopPropagationIfNeeded);
  239. window.addEventListener("afterprint", stopPropagationIfNeeded);
  240. }
  241. let overlayPromise;
  242. function ensureOverlay() {
  243. if (!overlayPromise) {
  244. overlayManager = _app.PDFViewerApplication.overlayManager;
  245. if (!overlayManager) {
  246. throw new Error("The overlay manager has not yet been initialized.");
  247. }
  248. dialog ||= document.getElementById("printServiceDialog");
  249. overlayPromise = overlayManager.register(dialog, true);
  250. document.getElementById("printCancel").onclick = abort;
  251. dialog.addEventListener("close", abort);
  252. }
  253. return overlayPromise;
  254. }
  255. _app.PDFPrintServiceFactory.instance = {
  256. supportsPrinting: true,
  257. createPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, printAnnotationStoragePromise, l10n) {
  258. if (activeService) {
  259. throw new Error("The print service is created and active.");
  260. }
  261. activeService = new PDFPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, printAnnotationStoragePromise, l10n);
  262. return activeService;
  263. }
  264. };