pdf_manager.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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, evaluatorOptions, enableXfa, docBaseUrl) {
  108. super();
  109. this._docId = docId;
  110. this._password = password;
  111. this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
  112. this.evaluatorOptions = evaluatorOptions;
  113. this.enableXfa = enableXfa;
  114. const stream = new _stream.Stream(data);
  115. this.pdfDocument = new _document.PDFDocument(this, stream);
  116. this._loadedStreamPromise = Promise.resolve(stream);
  117. }
  118. async ensure(obj, prop, args) {
  119. const value = obj[prop];
  120. if (typeof value === "function") {
  121. return value.apply(obj, args);
  122. }
  123. return value;
  124. }
  125. requestRange(begin, end) {
  126. return Promise.resolve();
  127. }
  128. requestLoadedStream() {}
  129. onLoadedStream() {
  130. return this._loadedStreamPromise;
  131. }
  132. terminate(reason) {}
  133. }
  134. exports.LocalPdfManager = LocalPdfManager;
  135. class NetworkPdfManager extends BasePdfManager {
  136. constructor(docId, pdfNetworkStream, args, evaluatorOptions, enableXfa, docBaseUrl) {
  137. super();
  138. this._docId = docId;
  139. this._password = args.password;
  140. this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
  141. this.msgHandler = args.msgHandler;
  142. this.evaluatorOptions = evaluatorOptions;
  143. this.enableXfa = enableXfa;
  144. this.streamManager = new _chunked_stream.ChunkedStreamManager(pdfNetworkStream, {
  145. msgHandler: args.msgHandler,
  146. length: args.length,
  147. disableAutoFetch: args.disableAutoFetch,
  148. rangeChunkSize: args.rangeChunkSize
  149. });
  150. this.pdfDocument = new _document.PDFDocument(this, this.streamManager.getStream());
  151. }
  152. async ensure(obj, prop, args) {
  153. try {
  154. const value = obj[prop];
  155. if (typeof value === "function") {
  156. return value.apply(obj, args);
  157. }
  158. return value;
  159. } catch (ex) {
  160. if (!(ex instanceof _core_utils.MissingDataException)) {
  161. throw ex;
  162. }
  163. await this.requestRange(ex.begin, ex.end);
  164. return this.ensure(obj, prop, args);
  165. }
  166. }
  167. requestRange(begin, end) {
  168. return this.streamManager.requestRange(begin, end);
  169. }
  170. requestLoadedStream() {
  171. this.streamManager.requestAllChunks();
  172. }
  173. sendProgressiveData(chunk) {
  174. this.streamManager.onReceiveData({
  175. chunk
  176. });
  177. }
  178. onLoadedStream() {
  179. return this.streamManager.onLoadedStream();
  180. }
  181. terminate(reason) {
  182. this.streamManager.abort(reason);
  183. }
  184. }
  185. exports.NetworkPdfManager = NetworkPdfManager;