UNPKG

3.48 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2017 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
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.FirefoxPrintService = undefined;
28
29var _ui_utils = require('./ui_utils');
30
31var _app = require('./app');
32
33var _pdf = require('../pdf');
34
35function composePage(pdfDocument, pageNumber, size, printContainer) {
36 var canvas = document.createElement('canvas');
37 var PRINT_RESOLUTION = 150;
38 var PRINT_UNITS = PRINT_RESOLUTION / 72.0;
39 canvas.width = Math.floor(size.width * PRINT_UNITS);
40 canvas.height = Math.floor(size.height * PRINT_UNITS);
41 canvas.style.width = Math.floor(size.width * _ui_utils.CSS_UNITS) + 'px';
42 canvas.style.height = Math.floor(size.height * _ui_utils.CSS_UNITS) + 'px';
43 var canvasWrapper = document.createElement('div');
44 canvasWrapper.appendChild(canvas);
45 printContainer.appendChild(canvasWrapper);
46 canvas.mozPrintCallback = function (obj) {
47 var ctx = obj.context;
48 ctx.save();
49 ctx.fillStyle = 'rgb(255, 255, 255)';
50 ctx.fillRect(0, 0, canvas.width, canvas.height);
51 ctx.restore();
52 pdfDocument.getPage(pageNumber).then(function (pdfPage) {
53 var renderContext = {
54 canvasContext: ctx,
55 transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
56 viewport: pdfPage.getViewport(1, size.rotation),
57 intent: 'print'
58 };
59 return pdfPage.render(renderContext).promise;
60 }).then(function () {
61 obj.done();
62 }, function (error) {
63 console.error(error);
64 if ('abort' in obj) {
65 obj.abort();
66 } else {
67 obj.done();
68 }
69 });
70 };
71}
72function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) {
73 this.pdfDocument = pdfDocument;
74 this.pagesOverview = pagesOverview;
75 this.printContainer = printContainer;
76}
77FirefoxPrintService.prototype = {
78 layout: function layout() {
79 var pdfDocument = this.pdfDocument;
80 var printContainer = this.printContainer;
81 var body = document.querySelector('body');
82 body.setAttribute('data-pdfjsprinting', true);
83 for (var i = 0, ii = this.pagesOverview.length; i < ii; ++i) {
84 composePage(pdfDocument, i + 1, this.pagesOverview[i], printContainer);
85 }
86 },
87 destroy: function destroy() {
88 this.printContainer.textContent = '';
89 }
90};
91_app.PDFPrintServiceFactory.instance = {
92 get supportsPrinting() {
93 var canvas = document.createElement('canvas');
94 var value = 'mozPrintCallback' in canvas;
95 return (0, _pdf.shadow)(this, 'supportsPrinting', value);
96 },
97 createPrintService: function createPrintService(pdfDocument, pagesOverview, printContainer) {
98 return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
99 }
100};
101exports.FirefoxPrintService = FirefoxPrintService;
\No newline at end of file