pdf_manager.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.NetworkPdfManager = exports.LocalPdfManager = void 0;
  27. var _util = require("../shared/util.js");
  28. var _chunked_stream = require("./chunked_stream.js");
  29. var _core_utils = require("./core_utils.js");
  30. var _document = require("./document.js");
  31. var _stream = require("./stream.js");
  32. function parseDocBaseUrl(url) {
  33. if (url) {
  34. const absoluteUrl = (0, _util.createValidAbsoluteUrl)(url);
  35. if (absoluteUrl) {
  36. return absoluteUrl.href;
  37. }
  38. (0, _util.warn)(`Invalid absolute docBaseUrl: "${url}".`);
  39. }
  40. return null;
  41. }
  42. class BasePdfManager {
  43. constructor() {
  44. if (this.constructor === BasePdfManager) {
  45. (0, _util.unreachable)("Cannot initialize BasePdfManager.");
  46. }
  47. }
  48. get docId() {
  49. return this._docId;
  50. }
  51. get password() {
  52. return this._password;
  53. }
  54. get docBaseUrl() {
  55. const catalog = this.pdfDocument.catalog;
  56. return (0, _util.shadow)(this, "docBaseUrl", catalog.baseUrl || this._docBaseUrl);
  57. }
  58. ensureDoc(prop, args) {
  59. return this.ensure(this.pdfDocument, prop, args);
  60. }
  61. ensureXRef(prop, args) {
  62. return this.ensure(this.pdfDocument.xref, prop, args);
  63. }
  64. ensureCatalog(prop, args) {
  65. return this.ensure(this.pdfDocument.catalog, prop, args);
  66. }
  67. getPage(pageIndex) {
  68. return this.pdfDocument.getPage(pageIndex);
  69. }
  70. fontFallback(id, handler) {
  71. return this.pdfDocument.fontFallback(id, handler);
  72. }
  73. loadXfaFonts(handler, task) {
  74. return this.pdfDocument.loadXfaFonts(handler, task);
  75. }
  76. loadXfaImages() {
  77. return this.pdfDocument.loadXfaImages();
  78. }
  79. serializeXfaData(annotationStorage) {
  80. return this.pdfDocument.serializeXfaData(annotationStorage);
  81. }
  82. cleanup(manuallyTriggered = false) {
  83. return this.pdfDocument.cleanup(manuallyTriggered);
  84. }
  85. async ensure(obj, prop, args) {
  86. (0, _util.unreachable)("Abstract method `ensure` called");
  87. }
  88. requestRange(begin, end) {
  89. (0, _util.unreachable)("Abstract method `requestRange` called");
  90. }
  91. requestLoadedStream(noFetch = false) {
  92. (0, _util.unreachable)("Abstract method `requestLoadedStream` called");
  93. }
  94. sendProgressiveData(chunk) {
  95. (0, _util.unreachable)("Abstract method `sendProgressiveData` called");
  96. }
  97. updatePassword(password) {
  98. this._password = password;
  99. }
  100. terminate(reason) {
  101. (0, _util.unreachable)("Abstract method `terminate` called");
  102. }
  103. }
  104. class LocalPdfManager extends BasePdfManager {
  105. constructor(docId, data, password, msgHandler, evaluatorOptions, enableXfa, docBaseUrl) {
  106. super();
  107. this._docId = docId;
  108. this._password = password;
  109. this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
  110. this.msgHandler = msgHandler;
  111. this.evaluatorOptions = evaluatorOptions;
  112. this.enableXfa = enableXfa;
  113. const stream = new _stream.Stream(data);
  114. this.pdfDocument = new _document.PDFDocument(this, stream);
  115. this._loadedStreamPromise = Promise.resolve(stream);
  116. }
  117. async ensure(obj, prop, args) {
  118. const value = obj[prop];
  119. if (typeof value === "function") {
  120. return value.apply(obj, args);
  121. }
  122. return value;
  123. }
  124. requestRange(begin, end) {
  125. return Promise.resolve();
  126. }
  127. requestLoadedStream(noFetch = false) {
  128. return this._loadedStreamPromise;
  129. }
  130. terminate(reason) {}
  131. }
  132. exports.LocalPdfManager = LocalPdfManager;
  133. class NetworkPdfManager extends BasePdfManager {
  134. constructor(docId, pdfNetworkStream, args, evaluatorOptions, enableXfa, docBaseUrl) {
  135. super();
  136. this._docId = docId;
  137. this._password = args.password;
  138. this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
  139. this.msgHandler = args.msgHandler;
  140. this.evaluatorOptions = evaluatorOptions;
  141. this.enableXfa = enableXfa;
  142. this.streamManager = new _chunked_stream.ChunkedStreamManager(pdfNetworkStream, {
  143. msgHandler: args.msgHandler,
  144. length: args.length,
  145. disableAutoFetch: args.disableAutoFetch,
  146. rangeChunkSize: args.rangeChunkSize
  147. });
  148. this.pdfDocument = new _document.PDFDocument(this, this.streamManager.getStream());
  149. }
  150. async ensure(obj, prop, args) {
  151. try {
  152. const value = obj[prop];
  153. if (typeof value === "function") {
  154. return value.apply(obj, args);
  155. }
  156. return value;
  157. } catch (ex) {
  158. if (!(ex instanceof _core_utils.MissingDataException)) {
  159. throw ex;
  160. }
  161. await this.requestRange(ex.begin, ex.end);
  162. return this.ensure(obj, prop, args);
  163. }
  164. }
  165. requestRange(begin, end) {
  166. return this.streamManager.requestRange(begin, end);
  167. }
  168. requestLoadedStream(noFetch = false) {
  169. return this.streamManager.requestAllChunks(noFetch);
  170. }
  171. sendProgressiveData(chunk) {
  172. this.streamManager.onReceiveData({
  173. chunk
  174. });
  175. }
  176. terminate(reason) {
  177. this.streamManager.abort(reason);
  178. }
  179. }
  180. exports.NetworkPdfManager = NetworkPdfManager;