pdf_manager.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. onLoadedStream() {
  59. (0, _util.unreachable)("Abstract method `onLoadedStream` called");
  60. }
  61. ensureDoc(prop, args) {
  62. return this.ensure(this.pdfDocument, prop, args);
  63. }
  64. ensureXRef(prop, args) {
  65. return this.ensure(this.pdfDocument.xref, prop, args);
  66. }
  67. ensureCatalog(prop, args) {
  68. return this.ensure(this.pdfDocument.catalog, prop, args);
  69. }
  70. getPage(pageIndex) {
  71. return this.pdfDocument.getPage(pageIndex);
  72. }
  73. fontFallback(id, handler) {
  74. return this.pdfDocument.fontFallback(id, handler);
  75. }
  76. loadXfaFonts(handler, task) {
  77. return this.pdfDocument.loadXfaFonts(handler, task);
  78. }
  79. loadXfaImages() {
  80. return this.pdfDocument.loadXfaImages();
  81. }
  82. serializeXfaData(annotationStorage) {
  83. return this.pdfDocument.serializeXfaData(annotationStorage);
  84. }
  85. cleanup(manuallyTriggered = false) {
  86. return this.pdfDocument.cleanup(manuallyTriggered);
  87. }
  88. async ensure(obj, prop, args) {
  89. (0, _util.unreachable)("Abstract method `ensure` called");
  90. }
  91. requestRange(begin, end) {
  92. (0, _util.unreachable)("Abstract method `requestRange` called");
  93. }
  94. requestLoadedStream() {
  95. (0, _util.unreachable)("Abstract method `requestLoadedStream` called");
  96. }
  97. sendProgressiveData(chunk) {
  98. (0, _util.unreachable)("Abstract method `sendProgressiveData` called");
  99. }
  100. updatePassword(password) {
  101. this._password = password;
  102. }
  103. terminate(reason) {
  104. (0, _util.unreachable)("Abstract method `terminate` called");
  105. }
  106. }
  107. class LocalPdfManager extends BasePdfManager {
  108. constructor(docId, data, password, msgHandler, evaluatorOptions, enableXfa, docBaseUrl) {
  109. super();
  110. this._docId = docId;
  111. this._password = password;
  112. this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
  113. this.msgHandler = msgHandler;
  114. this.evaluatorOptions = evaluatorOptions;
  115. this.enableXfa = enableXfa;
  116. const stream = new _stream.Stream(data);
  117. this.pdfDocument = new _document.PDFDocument(this, stream);
  118. this._loadedStreamPromise = Promise.resolve(stream);
  119. }
  120. async ensure(obj, prop, args) {
  121. const value = obj[prop];
  122. if (typeof value === "function") {
  123. return value.apply(obj, args);
  124. }
  125. return value;
  126. }
  127. requestRange(begin, end) {
  128. return Promise.resolve();
  129. }
  130. requestLoadedStream() {}
  131. onLoadedStream() {
  132. return this._loadedStreamPromise;
  133. }
  134. terminate(reason) {}
  135. }
  136. exports.LocalPdfManager = LocalPdfManager;
  137. class NetworkPdfManager extends BasePdfManager {
  138. constructor(docId, pdfNetworkStream, args, evaluatorOptions, enableXfa, docBaseUrl) {
  139. super();
  140. this._docId = docId;
  141. this._password = args.password;
  142. this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
  143. this.msgHandler = args.msgHandler;
  144. this.evaluatorOptions = evaluatorOptions;
  145. this.enableXfa = enableXfa;
  146. this.streamManager = new _chunked_stream.ChunkedStreamManager(pdfNetworkStream, {
  147. msgHandler: args.msgHandler,
  148. length: args.length,
  149. disableAutoFetch: args.disableAutoFetch,
  150. rangeChunkSize: args.rangeChunkSize
  151. });
  152. this.pdfDocument = new _document.PDFDocument(this, this.streamManager.getStream());
  153. }
  154. async ensure(obj, prop, args) {
  155. try {
  156. const value = obj[prop];
  157. if (typeof value === "function") {
  158. return value.apply(obj, args);
  159. }
  160. return value;
  161. } catch (ex) {
  162. if (!(ex instanceof _core_utils.MissingDataException)) {
  163. throw ex;
  164. }
  165. await this.requestRange(ex.begin, ex.end);
  166. return this.ensure(obj, prop, args);
  167. }
  168. }
  169. requestRange(begin, end) {
  170. return this.streamManager.requestRange(begin, end);
  171. }
  172. requestLoadedStream() {
  173. this.streamManager.requestAllChunks();
  174. }
  175. sendProgressiveData(chunk) {
  176. this.streamManager.onReceiveData({
  177. chunk
  178. });
  179. }
  180. onLoadedStream() {
  181. return this.streamManager.onLoadedStream();
  182. }
  183. terminate(reason) {
  184. this.streamManager.abort(reason);
  185. }
  186. }
  187. exports.NetworkPdfManager = NetworkPdfManager;