pdf_manager.js 5.6 KB

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