UNPKG

5.04 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 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 = FirefoxPrintService;
28
29var _pdf = require("../pdf");
30
31var _print_utils = require("./print_utils.js");
32
33var _app = require("./app.js");
34
35function composePage(pdfDocument, pageNumber, size, printContainer, printResolution, optionalContentConfigPromise, printAnnotationStoragePromise) {
36 const canvas = document.createElement("canvas");
37 const PRINT_UNITS = printResolution / _pdf.PixelsPerInch.PDF;
38 canvas.width = Math.floor(size.width * PRINT_UNITS);
39 canvas.height = Math.floor(size.height * PRINT_UNITS);
40 const canvasWrapper = document.createElement("div");
41 canvasWrapper.className = "printedPage";
42 canvasWrapper.append(canvas);
43 printContainer.append(canvasWrapper);
44 let currentRenderTask = null;
45
46 canvas.mozPrintCallback = function (obj) {
47 const 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 let thisRenderTask = null;
53 Promise.all([pdfDocument.getPage(pageNumber), printAnnotationStoragePromise]).then(function ([pdfPage, printAnnotationStorage]) {
54 if (currentRenderTask) {
55 currentRenderTask.cancel();
56 currentRenderTask = null;
57 }
58
59 const renderContext = {
60 canvasContext: ctx,
61 transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
62 viewport: pdfPage.getViewport({
63 scale: 1,
64 rotation: size.rotation
65 }),
66 intent: "print",
67 annotationMode: _pdf.AnnotationMode.ENABLE_STORAGE,
68 optionalContentConfigPromise,
69 printAnnotationStorage
70 };
71 currentRenderTask = thisRenderTask = pdfPage.render(renderContext);
72 return thisRenderTask.promise;
73 }).then(function () {
74 if (currentRenderTask === thisRenderTask) {
75 currentRenderTask = null;
76 }
77
78 obj.done();
79 }, function (reason) {
80 if (!(reason instanceof _pdf.RenderingCancelledException)) {
81 console.error(reason);
82 }
83
84 if (currentRenderTask === thisRenderTask) {
85 currentRenderTask.cancel();
86 currentRenderTask = null;
87 }
88
89 if ("abort" in obj) {
90 obj.abort();
91 } else {
92 obj.done();
93 }
94 });
95 };
96}
97
98function FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise = null, printAnnotationStoragePromise = null) {
99 this.pdfDocument = pdfDocument;
100 this.pagesOverview = pagesOverview;
101 this.printContainer = printContainer;
102 this._printResolution = printResolution || 150;
103 this._optionalContentConfigPromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
104 this._optionalContentConfigPromise = printAnnotationStoragePromise || Promise.resolve();
105}
106
107FirefoxPrintService.prototype = {
108 layout() {
109 const {
110 pdfDocument,
111 pagesOverview,
112 printContainer,
113 _printResolution,
114 _optionalContentConfigPromise,
115 _printAnnotationStoragePromise
116 } = this;
117 const body = document.querySelector("body");
118 body.setAttribute("data-pdfjsprinting", true);
119
120 if (pdfDocument.isPureXfa) {
121 (0, _print_utils.getXfaHtmlForPrinting)(printContainer, pdfDocument);
122 return;
123 }
124
125 for (let i = 0, ii = pagesOverview.length; i < ii; ++i) {
126 composePage(pdfDocument, i + 1, pagesOverview[i], printContainer, _printResolution, _optionalContentConfigPromise, _printAnnotationStoragePromise);
127 }
128 },
129
130 destroy() {
131 this.printContainer.textContent = "";
132 const body = document.querySelector("body");
133 body.removeAttribute("data-pdfjsprinting");
134 }
135
136};
137_app.PDFPrintServiceFactory.instance = {
138 get supportsPrinting() {
139 const canvas = document.createElement("canvas");
140 const value = ("mozPrintCallback" in canvas);
141 return (0, _pdf.shadow)(this, "supportsPrinting", value);
142 },
143
144 createPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, printAnnotationStoragePromise) {
145 return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, printAnnotationStoragePromise);
146 }
147
148};
\No newline at end of file