2
0

pdf_manager.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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. class BasePdfManager {
  33. constructor() {
  34. if (this.constructor === BasePdfManager) {
  35. (0, _util.unreachable)("Cannot initialize BasePdfManager.");
  36. }
  37. }
  38. get docId() {
  39. return this._docId;
  40. }
  41. get password() {
  42. return this._password;
  43. }
  44. get docBaseUrl() {
  45. let docBaseUrl = null;
  46. if (this._docBaseUrl) {
  47. const absoluteUrl = (0, _util.createValidAbsoluteUrl)(this._docBaseUrl);
  48. if (absoluteUrl) {
  49. docBaseUrl = absoluteUrl.href;
  50. } else {
  51. (0, _util.warn)(`Invalid absolute docBaseUrl: "${this._docBaseUrl}".`);
  52. }
  53. }
  54. return (0, _util.shadow)(this, "docBaseUrl", docBaseUrl);
  55. }
  56. onLoadedStream() {
  57. (0, _util.unreachable)("Abstract method `onLoadedStream` called");
  58. }
  59. ensureDoc(prop, args) {
  60. return this.ensure(this.pdfDocument, prop, args);
  61. }
  62. ensureXRef(prop, args) {
  63. return this.ensure(this.pdfDocument.xref, prop, args);
  64. }
  65. ensureCatalog(prop, args) {
  66. return this.ensure(this.pdfDocument.catalog, prop, args);
  67. }
  68. getPage(pageIndex) {
  69. return this.pdfDocument.getPage(pageIndex);
  70. }
  71. fontFallback(id, handler) {
  72. return this.pdfDocument.fontFallback(id, handler);
  73. }
  74. cleanup(manuallyTriggered = false) {
  75. return this.pdfDocument.cleanup(manuallyTriggered);
  76. }
  77. async ensure(obj, prop, args) {
  78. (0, _util.unreachable)("Abstract method `ensure` called");
  79. }
  80. requestRange(begin, end) {
  81. (0, _util.unreachable)("Abstract method `requestRange` called");
  82. }
  83. requestLoadedStream() {
  84. (0, _util.unreachable)("Abstract method `requestLoadedStream` called");
  85. }
  86. sendProgressiveData(chunk) {
  87. (0, _util.unreachable)("Abstract method `sendProgressiveData` called");
  88. }
  89. updatePassword(password) {
  90. this._password = password;
  91. }
  92. terminate(reason) {
  93. (0, _util.unreachable)("Abstract method `terminate` called");
  94. }
  95. }
  96. class LocalPdfManager extends BasePdfManager {
  97. constructor(docId, data, password, evaluatorOptions, docBaseUrl) {
  98. super();
  99. this._docId = docId;
  100. this._password = password;
  101. this._docBaseUrl = docBaseUrl;
  102. this.evaluatorOptions = evaluatorOptions;
  103. const stream = new _stream.Stream(data);
  104. this.pdfDocument = new _document.PDFDocument(this, stream);
  105. this._loadedStreamPromise = Promise.resolve(stream);
  106. }
  107. async ensure(obj, prop, args) {
  108. const value = obj[prop];
  109. if (typeof value === "function") {
  110. return value.apply(obj, args);
  111. }
  112. return value;
  113. }
  114. requestRange(begin, end) {
  115. return Promise.resolve();
  116. }
  117. requestLoadedStream() {}
  118. onLoadedStream() {
  119. return this._loadedStreamPromise;
  120. }
  121. terminate(reason) {}
  122. }
  123. exports.LocalPdfManager = LocalPdfManager;
  124. class NetworkPdfManager extends BasePdfManager {
  125. constructor(docId, pdfNetworkStream, args, evaluatorOptions, docBaseUrl) {
  126. super();
  127. this._docId = docId;
  128. this._password = args.password;
  129. this._docBaseUrl = docBaseUrl;
  130. this.msgHandler = args.msgHandler;
  131. this.evaluatorOptions = evaluatorOptions;
  132. this.streamManager = new _chunked_stream.ChunkedStreamManager(pdfNetworkStream, {
  133. msgHandler: args.msgHandler,
  134. length: args.length,
  135. disableAutoFetch: args.disableAutoFetch,
  136. rangeChunkSize: args.rangeChunkSize
  137. });
  138. this.pdfDocument = new _document.PDFDocument(this, this.streamManager.getStream());
  139. }
  140. async ensure(obj, prop, args) {
  141. try {
  142. const value = obj[prop];
  143. if (typeof value === "function") {
  144. return value.apply(obj, args);
  145. }
  146. return value;
  147. } catch (ex) {
  148. if (!(ex instanceof _core_utils.MissingDataException)) {
  149. throw ex;
  150. }
  151. await this.requestRange(ex.begin, ex.end);
  152. return this.ensure(obj, prop, args);
  153. }
  154. }
  155. requestRange(begin, end) {
  156. return this.streamManager.requestRange(begin, end);
  157. }
  158. requestLoadedStream() {
  159. this.streamManager.requestAllChunks();
  160. }
  161. sendProgressiveData(chunk) {
  162. this.streamManager.onReceiveData({
  163. chunk
  164. });
  165. }
  166. onLoadedStream() {
  167. return this.streamManager.onLoadedStream();
  168. }
  169. terminate(reason) {
  170. this.streamManager.abort(reason);
  171. }
  172. }
  173. exports.NetworkPdfManager = NetworkPdfManager;