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