pdf_print_service.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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 _app = require("./app.js");
  28. var _print_utils = require("./print_utils.js");
  29. var _viewer_compatibility = require("./viewer_compatibility.js");
  30. let activeService = null;
  31. let overlayManager = null;
  32. function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size, printResolution, optionalContentConfigPromise) {
  33. const scratchCanvas = activeService.scratchCanvas;
  34. const PRINT_UNITS = printResolution / 72.0;
  35. scratchCanvas.width = Math.floor(size.width * PRINT_UNITS);
  36. scratchCanvas.height = Math.floor(size.height * PRINT_UNITS);
  37. const ctx = scratchCanvas.getContext("2d");
  38. ctx.save();
  39. ctx.fillStyle = "rgb(255, 255, 255)";
  40. ctx.fillRect(0, 0, scratchCanvas.width, scratchCanvas.height);
  41. ctx.restore();
  42. return pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  43. const renderContext = {
  44. canvasContext: ctx,
  45. transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
  46. viewport: pdfPage.getViewport({
  47. scale: 1,
  48. rotation: size.rotation
  49. }),
  50. intent: "print",
  51. includeAnnotationStorage: true,
  52. optionalContentConfigPromise
  53. };
  54. return pdfPage.render(renderContext).promise;
  55. });
  56. }
  57. function PDFPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise = null, l10n) {
  58. this.pdfDocument = pdfDocument;
  59. this.pagesOverview = pagesOverview;
  60. this.printContainer = printContainer;
  61. this._printResolution = printResolution || 150;
  62. this._optionalContentConfigPromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
  63. this.l10n = l10n;
  64. this.currentPage = -1;
  65. this.scratchCanvas = document.createElement("canvas");
  66. }
  67. PDFPrintService.prototype = {
  68. layout() {
  69. this.throwIfInactive();
  70. const body = document.querySelector("body");
  71. body.setAttribute("data-pdfjsprinting", true);
  72. const hasEqualPageSizes = this.pagesOverview.every(function (size) {
  73. return size.width === this.pagesOverview[0].width && size.height === this.pagesOverview[0].height;
  74. }, this);
  75. if (!hasEqualPageSizes) {
  76. console.warn("Not all pages have the same size. The printed " + "result may be incorrect!");
  77. }
  78. this.pageStyleSheet = document.createElement("style");
  79. const pageSize = this.pagesOverview[0];
  80. this.pageStyleSheet.textContent = "@page { size: " + pageSize.width + "pt " + pageSize.height + "pt;}";
  81. body.appendChild(this.pageStyleSheet);
  82. },
  83. destroy() {
  84. if (activeService !== this) {
  85. return;
  86. }
  87. this.printContainer.textContent = "";
  88. const body = document.querySelector("body");
  89. body.removeAttribute("data-pdfjsprinting");
  90. if (this.pageStyleSheet) {
  91. this.pageStyleSheet.remove();
  92. this.pageStyleSheet = null;
  93. }
  94. this.scratchCanvas.width = this.scratchCanvas.height = 0;
  95. this.scratchCanvas = null;
  96. activeService = null;
  97. ensureOverlay().then(function () {
  98. if (overlayManager.active !== "printServiceOverlay") {
  99. return;
  100. }
  101. overlayManager.close("printServiceOverlay");
  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 && !_viewer_compatibility.viewerCompatibilityParams.disableCreateObjectURL) {
  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("printServiceOverlay");
  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 === "printServiceOverlay") {
  185. overlayManager.close("printServiceOverlay");
  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. const progressContainer = document.getElementById("printServiceOverlay");
  213. const progress = Math.round(100 * index / total);
  214. const progressBar = progressContainer.querySelector("progress");
  215. const progressPerc = progressContainer.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. overlayPromise = overlayManager.register("printServiceOverlay", document.getElementById("printServiceOverlay"), abort, true);
  251. document.getElementById("printCancel").onclick = abort;
  252. }
  253. return overlayPromise;
  254. }
  255. _app.PDFPrintServiceFactory.instance = {
  256. supportsPrinting: true,
  257. createPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, 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, l10n);
  262. return activeService;
  263. }
  264. };