pdf_print_service.js 9.3 KB

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