1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.FirefoxPrintService = FirefoxPrintService;
|
28 |
|
29 | var _pdf = require("../pdf");
|
30 |
|
31 | var _print_utils = require("./print_utils.js");
|
32 |
|
33 | var _app = require("./app.js");
|
34 |
|
35 | function 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 |
|
98 | function 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 |
|
107 | FirefoxPrintService.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 |