UNPKG

5.84 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.NetworkPdfManager = exports.LocalPdfManager = void 0;
28
29var _util = require("../shared/util.js");
30
31var _chunked_stream = require("./chunked_stream.js");
32
33var _core_utils = require("./core_utils.js");
34
35var _document = require("./document.js");
36
37var _stream = require("./stream.js");
38
39function parseDocBaseUrl(url) {
40 if (url) {
41 const absoluteUrl = (0, _util.createValidAbsoluteUrl)(url);
42
43 if (absoluteUrl) {
44 return absoluteUrl.href;
45 }
46
47 (0, _util.warn)(`Invalid absolute docBaseUrl: "${url}".`);
48 }
49
50 return null;
51}
52
53class BasePdfManager {
54 constructor() {
55 if (this.constructor === BasePdfManager) {
56 (0, _util.unreachable)("Cannot initialize BasePdfManager.");
57 }
58 }
59
60 get docId() {
61 return this._docId;
62 }
63
64 get password() {
65 return this._password;
66 }
67
68 get docBaseUrl() {
69 const catalog = this.pdfDocument.catalog;
70 return (0, _util.shadow)(this, "docBaseUrl", catalog.baseUrl || this._docBaseUrl);
71 }
72
73 onLoadedStream() {
74 (0, _util.unreachable)("Abstract method `onLoadedStream` called");
75 }
76
77 ensureDoc(prop, args) {
78 return this.ensure(this.pdfDocument, prop, args);
79 }
80
81 ensureXRef(prop, args) {
82 return this.ensure(this.pdfDocument.xref, prop, args);
83 }
84
85 ensureCatalog(prop, args) {
86 return this.ensure(this.pdfDocument.catalog, prop, args);
87 }
88
89 getPage(pageIndex) {
90 return this.pdfDocument.getPage(pageIndex);
91 }
92
93 fontFallback(id, handler) {
94 return this.pdfDocument.fontFallback(id, handler);
95 }
96
97 loadXfaFonts(handler, task) {
98 return this.pdfDocument.loadXfaFonts(handler, task);
99 }
100
101 loadXfaImages() {
102 return this.pdfDocument.loadXfaImages();
103 }
104
105 serializeXfaData(annotationStorage) {
106 return this.pdfDocument.serializeXfaData(annotationStorage);
107 }
108
109 cleanup(manuallyTriggered = false) {
110 return this.pdfDocument.cleanup(manuallyTriggered);
111 }
112
113 async ensure(obj, prop, args) {
114 (0, _util.unreachable)("Abstract method `ensure` called");
115 }
116
117 requestRange(begin, end) {
118 (0, _util.unreachable)("Abstract method `requestRange` called");
119 }
120
121 requestLoadedStream() {
122 (0, _util.unreachable)("Abstract method `requestLoadedStream` called");
123 }
124
125 sendProgressiveData(chunk) {
126 (0, _util.unreachable)("Abstract method `sendProgressiveData` called");
127 }
128
129 updatePassword(password) {
130 this._password = password;
131 }
132
133 terminate(reason) {
134 (0, _util.unreachable)("Abstract method `terminate` called");
135 }
136
137}
138
139class LocalPdfManager extends BasePdfManager {
140 constructor(docId, data, password, msgHandler, evaluatorOptions, enableXfa, docBaseUrl) {
141 super();
142 this._docId = docId;
143 this._password = password;
144 this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
145 this.msgHandler = msgHandler;
146 this.evaluatorOptions = evaluatorOptions;
147 this.enableXfa = enableXfa;
148 const stream = new _stream.Stream(data);
149 this.pdfDocument = new _document.PDFDocument(this, stream);
150 this._loadedStreamPromise = Promise.resolve(stream);
151 }
152
153 async ensure(obj, prop, args) {
154 const value = obj[prop];
155
156 if (typeof value === "function") {
157 return value.apply(obj, args);
158 }
159
160 return value;
161 }
162
163 requestRange(begin, end) {
164 return Promise.resolve();
165 }
166
167 requestLoadedStream() {}
168
169 onLoadedStream() {
170 return this._loadedStreamPromise;
171 }
172
173 terminate(reason) {}
174
175}
176
177exports.LocalPdfManager = LocalPdfManager;
178
179class NetworkPdfManager extends BasePdfManager {
180 constructor(docId, pdfNetworkStream, args, evaluatorOptions, enableXfa, docBaseUrl) {
181 super();
182 this._docId = docId;
183 this._password = args.password;
184 this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
185 this.msgHandler = args.msgHandler;
186 this.evaluatorOptions = evaluatorOptions;
187 this.enableXfa = enableXfa;
188 this.streamManager = new _chunked_stream.ChunkedStreamManager(pdfNetworkStream, {
189 msgHandler: args.msgHandler,
190 length: args.length,
191 disableAutoFetch: args.disableAutoFetch,
192 rangeChunkSize: args.rangeChunkSize
193 });
194 this.pdfDocument = new _document.PDFDocument(this, this.streamManager.getStream());
195 }
196
197 async ensure(obj, prop, args) {
198 try {
199 const value = obj[prop];
200
201 if (typeof value === "function") {
202 return value.apply(obj, args);
203 }
204
205 return value;
206 } catch (ex) {
207 if (!(ex instanceof _core_utils.MissingDataException)) {
208 throw ex;
209 }
210
211 await this.requestRange(ex.begin, ex.end);
212 return this.ensure(obj, prop, args);
213 }
214 }
215
216 requestRange(begin, end) {
217 return this.streamManager.requestRange(begin, end);
218 }
219
220 requestLoadedStream() {
221 this.streamManager.requestAllChunks();
222 }
223
224 sendProgressiveData(chunk) {
225 this.streamManager.onReceiveData({
226 chunk
227 });
228 }
229
230 onLoadedStream() {
231 return this.streamManager.onLoadedStream();
232 }
233
234 terminate(reason) {
235 this.streamManager.abort(reason);
236 }
237
238}
239
240exports.NetworkPdfManager = NetworkPdfManager;
\No newline at end of file