pdf_manager.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import {
  16. createValidAbsoluteUrl,
  17. shadow,
  18. unreachable,
  19. warn,
  20. } from "../shared/util.js";
  21. import { ChunkedStreamManager } from "./chunked_stream.js";
  22. import { MissingDataException } from "./core_utils.js";
  23. import { PDFDocument } from "./document.js";
  24. import { Stream } from "./stream.js";
  25. function parseDocBaseUrl(url) {
  26. if (url) {
  27. const absoluteUrl = createValidAbsoluteUrl(url);
  28. if (absoluteUrl) {
  29. return absoluteUrl.href;
  30. }
  31. warn(`Invalid absolute docBaseUrl: "${url}".`);
  32. }
  33. return null;
  34. }
  35. class BasePdfManager {
  36. constructor() {
  37. if (this.constructor === BasePdfManager) {
  38. unreachable("Cannot initialize BasePdfManager.");
  39. }
  40. }
  41. get docId() {
  42. return this._docId;
  43. }
  44. get password() {
  45. return this._password;
  46. }
  47. get docBaseUrl() {
  48. const catalog = this.pdfDocument.catalog;
  49. return shadow(this, "docBaseUrl", catalog.baseUrl || this._docBaseUrl);
  50. }
  51. ensureDoc(prop, args) {
  52. return this.ensure(this.pdfDocument, prop, args);
  53. }
  54. ensureXRef(prop, args) {
  55. return this.ensure(this.pdfDocument.xref, prop, args);
  56. }
  57. ensureCatalog(prop, args) {
  58. return this.ensure(this.pdfDocument.catalog, prop, args);
  59. }
  60. getPage(pageIndex) {
  61. return this.pdfDocument.getPage(pageIndex);
  62. }
  63. fontFallback(id, handler) {
  64. return this.pdfDocument.fontFallback(id, handler);
  65. }
  66. loadXfaFonts(handler, task) {
  67. return this.pdfDocument.loadXfaFonts(handler, task);
  68. }
  69. loadXfaImages() {
  70. return this.pdfDocument.loadXfaImages();
  71. }
  72. serializeXfaData(annotationStorage) {
  73. return this.pdfDocument.serializeXfaData(annotationStorage);
  74. }
  75. cleanup(manuallyTriggered = false) {
  76. return this.pdfDocument.cleanup(manuallyTriggered);
  77. }
  78. async ensure(obj, prop, args) {
  79. unreachable("Abstract method `ensure` called");
  80. }
  81. requestRange(begin, end) {
  82. unreachable("Abstract method `requestRange` called");
  83. }
  84. requestLoadedStream(noFetch = false) {
  85. unreachable("Abstract method `requestLoadedStream` called");
  86. }
  87. sendProgressiveData(chunk) {
  88. unreachable("Abstract method `sendProgressiveData` called");
  89. }
  90. updatePassword(password) {
  91. this._password = password;
  92. }
  93. terminate(reason) {
  94. unreachable("Abstract method `terminate` called");
  95. }
  96. }
  97. class LocalPdfManager extends BasePdfManager {
  98. constructor(
  99. docId,
  100. data,
  101. password,
  102. msgHandler,
  103. evaluatorOptions,
  104. enableXfa,
  105. docBaseUrl
  106. ) {
  107. super();
  108. this._docId = docId;
  109. this._password = password;
  110. this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
  111. this.msgHandler = msgHandler;
  112. this.evaluatorOptions = evaluatorOptions;
  113. this.enableXfa = enableXfa;
  114. const stream = new Stream(data);
  115. this.pdfDocument = new 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(noFetch = false) {
  129. return this._loadedStreamPromise;
  130. }
  131. terminate(reason) {}
  132. }
  133. class NetworkPdfManager extends BasePdfManager {
  134. constructor(
  135. docId,
  136. pdfNetworkStream,
  137. args,
  138. evaluatorOptions,
  139. enableXfa,
  140. docBaseUrl
  141. ) {
  142. super();
  143. this._docId = docId;
  144. this._password = args.password;
  145. this._docBaseUrl = parseDocBaseUrl(docBaseUrl);
  146. this.msgHandler = args.msgHandler;
  147. this.evaluatorOptions = evaluatorOptions;
  148. this.enableXfa = enableXfa;
  149. this.streamManager = new ChunkedStreamManager(pdfNetworkStream, {
  150. msgHandler: args.msgHandler,
  151. length: args.length,
  152. disableAutoFetch: args.disableAutoFetch,
  153. rangeChunkSize: args.rangeChunkSize,
  154. });
  155. this.pdfDocument = new PDFDocument(this, this.streamManager.getStream());
  156. }
  157. async ensure(obj, prop, args) {
  158. try {
  159. const value = obj[prop];
  160. if (typeof value === "function") {
  161. return value.apply(obj, args);
  162. }
  163. return value;
  164. } catch (ex) {
  165. if (!(ex instanceof MissingDataException)) {
  166. throw ex;
  167. }
  168. await this.requestRange(ex.begin, ex.end);
  169. return this.ensure(obj, prop, args);
  170. }
  171. }
  172. requestRange(begin, end) {
  173. return this.streamManager.requestRange(begin, end);
  174. }
  175. requestLoadedStream(noFetch = false) {
  176. return this.streamManager.requestAllChunks(noFetch);
  177. }
  178. sendProgressiveData(chunk) {
  179. this.streamManager.onReceiveData({ chunk });
  180. }
  181. terminate(reason) {
  182. this.streamManager.abort(reason);
  183. }
  184. }
  185. export { LocalPdfManager, NetworkPdfManager };