2
0

pdf_manager.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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 _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  28. var _util = require("../shared/util");
  29. var _chunked_stream = require("./chunked_stream");
  30. var _document = require("./document");
  31. var _stream = require("./stream");
  32. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  33. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  34. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  35. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  36. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  37. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  38. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  39. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  40. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  41. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  42. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  43. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  44. var BasePdfManager =
  45. /*#__PURE__*/
  46. function () {
  47. function BasePdfManager() {
  48. _classCallCheck(this, BasePdfManager);
  49. if (this.constructor === BasePdfManager) {
  50. (0, _util.unreachable)('Cannot initialize BasePdfManager.');
  51. }
  52. }
  53. _createClass(BasePdfManager, [{
  54. key: "onLoadedStream",
  55. value: function onLoadedStream() {
  56. (0, _util.unreachable)('Abstract method `onLoadedStream` called');
  57. }
  58. }, {
  59. key: "ensureDoc",
  60. value: function ensureDoc(prop, args) {
  61. return this.ensure(this.pdfDocument, prop, args);
  62. }
  63. }, {
  64. key: "ensureXRef",
  65. value: function ensureXRef(prop, args) {
  66. return this.ensure(this.pdfDocument.xref, prop, args);
  67. }
  68. }, {
  69. key: "ensureCatalog",
  70. value: function ensureCatalog(prop, args) {
  71. return this.ensure(this.pdfDocument.catalog, prop, args);
  72. }
  73. }, {
  74. key: "getPage",
  75. value: function getPage(pageIndex) {
  76. return this.pdfDocument.getPage(pageIndex);
  77. }
  78. }, {
  79. key: "fontFallback",
  80. value: function fontFallback(id, handler) {
  81. return this.pdfDocument.fontFallback(id, handler);
  82. }
  83. }, {
  84. key: "cleanup",
  85. value: function cleanup() {
  86. return this.pdfDocument.cleanup();
  87. }
  88. }, {
  89. key: "ensure",
  90. value: function () {
  91. var _ensure = _asyncToGenerator(
  92. /*#__PURE__*/
  93. _regenerator.default.mark(function _callee(obj, prop, args) {
  94. return _regenerator.default.wrap(function _callee$(_context) {
  95. while (1) {
  96. switch (_context.prev = _context.next) {
  97. case 0:
  98. (0, _util.unreachable)('Abstract method `ensure` called');
  99. case 1:
  100. case "end":
  101. return _context.stop();
  102. }
  103. }
  104. }, _callee, this);
  105. }));
  106. function ensure(_x, _x2, _x3) {
  107. return _ensure.apply(this, arguments);
  108. }
  109. return ensure;
  110. }()
  111. }, {
  112. key: "requestRange",
  113. value: function requestRange(begin, end) {
  114. (0, _util.unreachable)('Abstract method `requestRange` called');
  115. }
  116. }, {
  117. key: "requestLoadedStream",
  118. value: function requestLoadedStream() {
  119. (0, _util.unreachable)('Abstract method `requestLoadedStream` called');
  120. }
  121. }, {
  122. key: "sendProgressiveData",
  123. value: function sendProgressiveData(chunk) {
  124. (0, _util.unreachable)('Abstract method `sendProgressiveData` called');
  125. }
  126. }, {
  127. key: "updatePassword",
  128. value: function updatePassword(password) {
  129. this._password = password;
  130. }
  131. }, {
  132. key: "terminate",
  133. value: function terminate() {
  134. (0, _util.unreachable)('Abstract method `terminate` called');
  135. }
  136. }, {
  137. key: "docId",
  138. get: function get() {
  139. return this._docId;
  140. }
  141. }, {
  142. key: "password",
  143. get: function get() {
  144. return this._password;
  145. }
  146. }, {
  147. key: "docBaseUrl",
  148. get: function get() {
  149. var docBaseUrl = null;
  150. if (this._docBaseUrl) {
  151. var absoluteUrl = (0, _util.createValidAbsoluteUrl)(this._docBaseUrl);
  152. if (absoluteUrl) {
  153. docBaseUrl = absoluteUrl.href;
  154. } else {
  155. (0, _util.warn)("Invalid absolute docBaseUrl: \"".concat(this._docBaseUrl, "\"."));
  156. }
  157. }
  158. return (0, _util.shadow)(this, 'docBaseUrl', docBaseUrl);
  159. }
  160. }]);
  161. return BasePdfManager;
  162. }();
  163. var LocalPdfManager =
  164. /*#__PURE__*/
  165. function (_BasePdfManager) {
  166. _inherits(LocalPdfManager, _BasePdfManager);
  167. function LocalPdfManager(docId, data, password, evaluatorOptions, docBaseUrl) {
  168. var _this;
  169. _classCallCheck(this, LocalPdfManager);
  170. _this = _possibleConstructorReturn(this, _getPrototypeOf(LocalPdfManager).call(this));
  171. _this._docId = docId;
  172. _this._password = password;
  173. _this._docBaseUrl = docBaseUrl;
  174. _this.evaluatorOptions = evaluatorOptions;
  175. var stream = new _stream.Stream(data);
  176. _this.pdfDocument = new _document.PDFDocument(_assertThisInitialized(_assertThisInitialized(_this)), stream);
  177. _this._loadedStreamPromise = Promise.resolve(stream);
  178. return _this;
  179. }
  180. _createClass(LocalPdfManager, [{
  181. key: "ensure",
  182. value: function () {
  183. var _ensure2 = _asyncToGenerator(
  184. /*#__PURE__*/
  185. _regenerator.default.mark(function _callee2(obj, prop, args) {
  186. var value;
  187. return _regenerator.default.wrap(function _callee2$(_context2) {
  188. while (1) {
  189. switch (_context2.prev = _context2.next) {
  190. case 0:
  191. value = obj[prop];
  192. if (!(typeof value === 'function')) {
  193. _context2.next = 3;
  194. break;
  195. }
  196. return _context2.abrupt("return", value.apply(obj, args));
  197. case 3:
  198. return _context2.abrupt("return", value);
  199. case 4:
  200. case "end":
  201. return _context2.stop();
  202. }
  203. }
  204. }, _callee2, this);
  205. }));
  206. function ensure(_x4, _x5, _x6) {
  207. return _ensure2.apply(this, arguments);
  208. }
  209. return ensure;
  210. }()
  211. }, {
  212. key: "requestRange",
  213. value: function requestRange(begin, end) {
  214. return Promise.resolve();
  215. }
  216. }, {
  217. key: "requestLoadedStream",
  218. value: function requestLoadedStream() {}
  219. }, {
  220. key: "onLoadedStream",
  221. value: function onLoadedStream() {
  222. return this._loadedStreamPromise;
  223. }
  224. }, {
  225. key: "terminate",
  226. value: function terminate() {}
  227. }]);
  228. return LocalPdfManager;
  229. }(BasePdfManager);
  230. exports.LocalPdfManager = LocalPdfManager;
  231. var NetworkPdfManager =
  232. /*#__PURE__*/
  233. function (_BasePdfManager2) {
  234. _inherits(NetworkPdfManager, _BasePdfManager2);
  235. function NetworkPdfManager(docId, pdfNetworkStream, args, evaluatorOptions, docBaseUrl) {
  236. var _this2;
  237. _classCallCheck(this, NetworkPdfManager);
  238. _this2 = _possibleConstructorReturn(this, _getPrototypeOf(NetworkPdfManager).call(this));
  239. _this2._docId = docId;
  240. _this2._password = args.password;
  241. _this2._docBaseUrl = docBaseUrl;
  242. _this2.msgHandler = args.msgHandler;
  243. _this2.evaluatorOptions = evaluatorOptions;
  244. _this2.streamManager = new _chunked_stream.ChunkedStreamManager(pdfNetworkStream, {
  245. msgHandler: args.msgHandler,
  246. length: args.length,
  247. disableAutoFetch: args.disableAutoFetch,
  248. rangeChunkSize: args.rangeChunkSize
  249. });
  250. _this2.pdfDocument = new _document.PDFDocument(_assertThisInitialized(_assertThisInitialized(_this2)), _this2.streamManager.getStream());
  251. return _this2;
  252. }
  253. _createClass(NetworkPdfManager, [{
  254. key: "ensure",
  255. value: function () {
  256. var _ensure3 = _asyncToGenerator(
  257. /*#__PURE__*/
  258. _regenerator.default.mark(function _callee3(obj, prop, args) {
  259. var value;
  260. return _regenerator.default.wrap(function _callee3$(_context3) {
  261. while (1) {
  262. switch (_context3.prev = _context3.next) {
  263. case 0:
  264. _context3.prev = 0;
  265. value = obj[prop];
  266. if (!(typeof value === 'function')) {
  267. _context3.next = 4;
  268. break;
  269. }
  270. return _context3.abrupt("return", value.apply(obj, args));
  271. case 4:
  272. return _context3.abrupt("return", value);
  273. case 7:
  274. _context3.prev = 7;
  275. _context3.t0 = _context3["catch"](0);
  276. if (_context3.t0 instanceof _util.MissingDataException) {
  277. _context3.next = 11;
  278. break;
  279. }
  280. throw _context3.t0;
  281. case 11:
  282. _context3.next = 13;
  283. return this.requestRange(_context3.t0.begin, _context3.t0.end);
  284. case 13:
  285. return _context3.abrupt("return", this.ensure(obj, prop, args));
  286. case 14:
  287. case "end":
  288. return _context3.stop();
  289. }
  290. }
  291. }, _callee3, this, [[0, 7]]);
  292. }));
  293. function ensure(_x7, _x8, _x9) {
  294. return _ensure3.apply(this, arguments);
  295. }
  296. return ensure;
  297. }()
  298. }, {
  299. key: "requestRange",
  300. value: function requestRange(begin, end) {
  301. return this.streamManager.requestRange(begin, end);
  302. }
  303. }, {
  304. key: "requestLoadedStream",
  305. value: function requestLoadedStream() {
  306. this.streamManager.requestAllChunks();
  307. }
  308. }, {
  309. key: "sendProgressiveData",
  310. value: function sendProgressiveData(chunk) {
  311. this.streamManager.onReceiveData({
  312. chunk: chunk
  313. });
  314. }
  315. }, {
  316. key: "onLoadedStream",
  317. value: function onLoadedStream() {
  318. return this.streamManager.onLoadedStream();
  319. }
  320. }, {
  321. key: "terminate",
  322. value: function terminate() {
  323. this.streamManager.abort();
  324. }
  325. }]);
  326. return NetworkPdfManager;
  327. }(BasePdfManager);
  328. exports.NetworkPdfManager = NetworkPdfManager;