pdf_print_service.js 9.1 KB

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