api_spec.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  24. var _test_utils = require('./test_utils');
  25. var _util = require('../../shared/util');
  26. var _dom_utils = require('../../display/dom_utils');
  27. var _api = require('../../display/api');
  28. var _is_node = require('../../shared/is_node');
  29. var _is_node2 = _interopRequireDefault(_is_node);
  30. var _global = require('../../display/global');
  31. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  32. describe('api', function () {
  33. var basicApiFileName = 'basicapi.pdf';
  34. var basicApiFileLength = 105779;
  35. var basicApiGetDocumentParams = (0, _test_utils.buildGetDocumentParams)(basicApiFileName);
  36. var CanvasFactory = void 0;
  37. beforeAll(function (done) {
  38. if ((0, _is_node2.default)()) {} else {
  39. CanvasFactory = new _dom_utils.DOMCanvasFactory();
  40. }
  41. done();
  42. });
  43. afterAll(function (done) {
  44. CanvasFactory = null;
  45. done();
  46. });
  47. function waitSome(callback) {
  48. var WAIT_TIMEOUT = 10;
  49. setTimeout(function () {
  50. callback();
  51. }, WAIT_TIMEOUT);
  52. }
  53. describe('PDFJS', function () {
  54. describe('getDocument', function () {
  55. it('creates pdf doc from URL', function (done) {
  56. if ((0, _is_node2.default)()) {
  57. pending('XMLHttpRequest is not supported in Node.js.');
  58. }
  59. var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  60. var isProgressReportedResolved = false;
  61. var progressReportedCapability = (0, _util.createPromiseCapability)();
  62. loadingTask.onProgress = function (progressData) {
  63. if (!isProgressReportedResolved) {
  64. isProgressReportedResolved = true;
  65. progressReportedCapability.resolve(progressData);
  66. }
  67. };
  68. var promises = [progressReportedCapability.promise, loadingTask.promise];
  69. Promise.all(promises).then(function (data) {
  70. expect(data[0].loaded / data[0].total > 0).toEqual(true);
  71. expect(data[1] instanceof _api.PDFDocumentProxy).toEqual(true);
  72. expect(loadingTask).toEqual(data[1].loadingTask);
  73. loadingTask.destroy().then(done);
  74. }).catch(function (reason) {
  75. done.fail(reason);
  76. });
  77. });
  78. it('creates pdf doc from URL and aborts before worker initialized', function (done) {
  79. if ((0, _is_node2.default)()) {
  80. pending('XMLHttpRequest is not supported in Node.js.');
  81. }
  82. var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  83. var destroyed = loadingTask.destroy();
  84. loadingTask.promise.then(function (reason) {
  85. done.fail('shall fail loading');
  86. }).catch(function (reason) {
  87. expect(true).toEqual(true);
  88. destroyed.then(done);
  89. });
  90. });
  91. it('creates pdf doc from URL and aborts loading after worker initialized', function (done) {
  92. if ((0, _is_node2.default)()) {
  93. pending('XMLHttpRequest is not supported in Node.js.');
  94. }
  95. var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  96. var destroyed = loadingTask._worker.promise.then(function () {
  97. return loadingTask.destroy();
  98. });
  99. destroyed.then(function (data) {
  100. expect(true).toEqual(true);
  101. done();
  102. }).catch(function (reason) {
  103. done.fail(reason);
  104. });
  105. });
  106. it('creates pdf doc from typed array', function (done) {
  107. var typedArrayPdf;
  108. if ((0, _is_node2.default)()) {
  109. typedArrayPdf = _test_utils.NodeFileReaderFactory.fetch({ path: _test_utils.TEST_PDFS_PATH.node + basicApiFileName });
  110. } else {
  111. var nonBinaryRequest = _global.PDFJS.disableWorker;
  112. var request = new XMLHttpRequest();
  113. request.open('GET', _test_utils.TEST_PDFS_PATH.dom + basicApiFileName, false);
  114. if (!nonBinaryRequest) {
  115. try {
  116. request.responseType = 'arraybuffer';
  117. nonBinaryRequest = request.responseType !== 'arraybuffer';
  118. } catch (e) {
  119. nonBinaryRequest = true;
  120. }
  121. }
  122. if (nonBinaryRequest && request.overrideMimeType) {
  123. request.overrideMimeType('text/plain; charset=x-user-defined');
  124. }
  125. request.send(null);
  126. if (nonBinaryRequest) {
  127. typedArrayPdf = (0, _util.stringToBytes)(request.responseText);
  128. } else {
  129. typedArrayPdf = new Uint8Array(request.response);
  130. }
  131. }
  132. expect(typedArrayPdf.length).toEqual(basicApiFileLength);
  133. var loadingTask = (0, _api.getDocument)(typedArrayPdf);
  134. loadingTask.promise.then(function (data) {
  135. expect(data instanceof _api.PDFDocumentProxy).toEqual(true);
  136. loadingTask.destroy().then(done);
  137. }).catch(function (reason) {
  138. done.fail(reason);
  139. });
  140. });
  141. it('creates pdf doc from invalid PDF file', function (done) {
  142. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug1020226.pdf'));
  143. loadingTask.promise.then(function () {
  144. done.fail('shall fail loading');
  145. }).catch(function (error) {
  146. expect(error instanceof _util.InvalidPDFException).toEqual(true);
  147. loadingTask.destroy().then(done);
  148. });
  149. });
  150. it('creates pdf doc from non-existent URL', function (done) {
  151. if ((0, _is_node2.default)()) {
  152. pending('XMLHttpRequest is not supported in Node.js.');
  153. }
  154. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('non-existent.pdf'));
  155. loadingTask.promise.then(function (error) {
  156. done.fail('shall fail loading');
  157. }).catch(function (error) {
  158. expect(error instanceof _util.MissingPDFException).toEqual(true);
  159. loadingTask.destroy().then(done);
  160. });
  161. });
  162. it('creates pdf doc from PDF file protected with user and owner password', function (done) {
  163. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('pr6531_1.pdf'));
  164. var isPasswordNeededResolved = false;
  165. var passwordNeededCapability = (0, _util.createPromiseCapability)();
  166. var isPasswordIncorrectResolved = false;
  167. var passwordIncorrectCapability = (0, _util.createPromiseCapability)();
  168. loadingTask.onPassword = function (updatePassword, reason) {
  169. if (reason === _util.PasswordResponses.NEED_PASSWORD && !isPasswordNeededResolved) {
  170. isPasswordNeededResolved = true;
  171. passwordNeededCapability.resolve();
  172. updatePassword('qwerty');
  173. return;
  174. }
  175. if (reason === _util.PasswordResponses.INCORRECT_PASSWORD && !isPasswordIncorrectResolved) {
  176. isPasswordIncorrectResolved = true;
  177. passwordIncorrectCapability.resolve();
  178. updatePassword('asdfasdf');
  179. return;
  180. }
  181. expect(false).toEqual(true);
  182. };
  183. var promises = [passwordNeededCapability.promise, passwordIncorrectCapability.promise, loadingTask.promise];
  184. Promise.all(promises).then(function (data) {
  185. expect(data[2] instanceof _api.PDFDocumentProxy).toEqual(true);
  186. loadingTask.destroy().then(done);
  187. }).catch(function (reason) {
  188. done.fail(reason);
  189. });
  190. });
  191. it('creates pdf doc from PDF file protected with only a user password', function (done) {
  192. var filename = 'pr6531_2.pdf';
  193. var passwordNeededLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { password: '' }));
  194. var result1 = passwordNeededLoadingTask.promise.then(function () {
  195. done.fail('shall fail with no password');
  196. return Promise.reject(new Error('loadingTask should be rejected'));
  197. }, function (data) {
  198. expect(data instanceof _util.PasswordException).toEqual(true);
  199. expect(data.code).toEqual(_util.PasswordResponses.NEED_PASSWORD);
  200. return passwordNeededLoadingTask.destroy();
  201. });
  202. var passwordIncorrectLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { password: 'qwerty' }));
  203. var result2 = passwordIncorrectLoadingTask.promise.then(function () {
  204. done.fail('shall fail with wrong password');
  205. return Promise.reject(new Error('loadingTask should be rejected'));
  206. }, function (data) {
  207. expect(data instanceof _util.PasswordException).toEqual(true);
  208. expect(data.code).toEqual(_util.PasswordResponses.INCORRECT_PASSWORD);
  209. return passwordIncorrectLoadingTask.destroy();
  210. });
  211. var passwordAcceptedLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { password: 'asdfasdf' }));
  212. var result3 = passwordAcceptedLoadingTask.promise.then(function (data) {
  213. expect(data instanceof _api.PDFDocumentProxy).toEqual(true);
  214. return passwordAcceptedLoadingTask.destroy();
  215. });
  216. Promise.all([result1, result2, result3]).then(function () {
  217. done();
  218. }).catch(function (reason) {
  219. done.fail(reason);
  220. });
  221. });
  222. it('creates pdf doc from password protected PDF file and aborts/throws ' + 'in the onPassword callback (issue 7806)', function (done) {
  223. var filename = 'issue3371.pdf';
  224. var passwordNeededLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename));
  225. var passwordIncorrectLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { password: 'qwerty' }));
  226. var passwordNeededDestroyed = void 0;
  227. passwordNeededLoadingTask.onPassword = function (callback, reason) {
  228. if (reason === _util.PasswordResponses.NEED_PASSWORD) {
  229. passwordNeededDestroyed = passwordNeededLoadingTask.destroy();
  230. return;
  231. }
  232. expect(false).toEqual(true);
  233. };
  234. var result1 = passwordNeededLoadingTask.promise.then(function () {
  235. done.fail('shall fail since the loadingTask should be destroyed');
  236. return Promise.reject(new Error('loadingTask should be rejected'));
  237. }, function (reason) {
  238. expect(reason instanceof _util.PasswordException).toEqual(true);
  239. expect(reason.code).toEqual(_util.PasswordResponses.NEED_PASSWORD);
  240. return passwordNeededDestroyed;
  241. });
  242. passwordIncorrectLoadingTask.onPassword = function (callback, reason) {
  243. if (reason === _util.PasswordResponses.INCORRECT_PASSWORD) {
  244. throw new Error('Incorrect password');
  245. }
  246. expect(false).toEqual(true);
  247. };
  248. var result2 = passwordIncorrectLoadingTask.promise.then(function () {
  249. done.fail('shall fail since the onPassword callback should throw');
  250. return Promise.reject(new Error('loadingTask should be rejected'));
  251. }, function (reason) {
  252. expect(reason instanceof _util.PasswordException).toEqual(true);
  253. expect(reason.code).toEqual(_util.PasswordResponses.INCORRECT_PASSWORD);
  254. return passwordIncorrectLoadingTask.destroy();
  255. });
  256. Promise.all([result1, result2]).then(function () {
  257. done();
  258. }).catch(function (reason) {
  259. done.fail(reason);
  260. });
  261. });
  262. });
  263. });
  264. describe('PDFWorker', function () {
  265. if ((0, _is_node2.default)()) {
  266. pending('Worker is not supported in Node.js.');
  267. }
  268. it('worker created or destroyed', function (done) {
  269. var worker = new _global.PDFJS.PDFWorker('test1');
  270. worker.promise.then(function () {
  271. expect(worker.name).toEqual('test1');
  272. expect(!!worker.port).toEqual(true);
  273. expect(worker.destroyed).toEqual(false);
  274. expect(!!worker._webWorker).toEqual(true);
  275. expect(worker.port === worker._webWorker).toEqual(true);
  276. worker.destroy();
  277. expect(!!worker.port).toEqual(false);
  278. expect(worker.destroyed).toEqual(true);
  279. done();
  280. }).catch(function (reason) {
  281. done.fail(reason);
  282. });
  283. });
  284. it('worker created or destroyed by getDocument', function (done) {
  285. var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  286. var worker;
  287. loadingTask.promise.then(function () {
  288. worker = loadingTask._worker;
  289. expect(!!worker).toEqual(true);
  290. });
  291. var destroyPromise = loadingTask.promise.then(function () {
  292. return loadingTask.destroy();
  293. });
  294. destroyPromise.then(function () {
  295. var destroyedWorker = loadingTask._worker;
  296. expect(!!destroyedWorker).toEqual(false);
  297. expect(worker.destroyed).toEqual(true);
  298. done();
  299. }).catch(function (reason) {
  300. done.fail(reason);
  301. });
  302. });
  303. it('worker created and can be used in getDocument', function (done) {
  304. var worker = new _global.PDFJS.PDFWorker('test1');
  305. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, { worker: worker }));
  306. loadingTask.promise.then(function () {
  307. var docWorker = loadingTask._worker;
  308. expect(!!docWorker).toEqual(false);
  309. var messageHandlerPort = loadingTask._transport.messageHandler.comObj;
  310. expect(messageHandlerPort === worker.port).toEqual(true);
  311. });
  312. var destroyPromise = loadingTask.promise.then(function () {
  313. return loadingTask.destroy();
  314. });
  315. destroyPromise.then(function () {
  316. expect(worker.destroyed).toEqual(false);
  317. worker.destroy();
  318. done();
  319. }).catch(function (reason) {
  320. done.fail(reason);
  321. });
  322. });
  323. it('creates more than one worker', function (done) {
  324. var worker1 = new _global.PDFJS.PDFWorker('test1');
  325. var worker2 = new _global.PDFJS.PDFWorker('test2');
  326. var worker3 = new _global.PDFJS.PDFWorker('test3');
  327. var ready = Promise.all([worker1.promise, worker2.promise, worker3.promise]);
  328. ready.then(function () {
  329. expect(worker1.port !== worker2.port && worker1.port !== worker3.port && worker2.port !== worker3.port).toEqual(true);
  330. worker1.destroy();
  331. worker2.destroy();
  332. worker3.destroy();
  333. done();
  334. }).catch(function (reason) {
  335. done.fail(reason);
  336. });
  337. });
  338. });
  339. describe('PDFDocument', function () {
  340. var loadingTask;
  341. var doc;
  342. beforeAll(function (done) {
  343. loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  344. loadingTask.promise.then(function (data) {
  345. doc = data;
  346. done();
  347. });
  348. });
  349. afterAll(function (done) {
  350. loadingTask.destroy().then(done);
  351. });
  352. it('gets number of pages', function () {
  353. expect(doc.numPages).toEqual(3);
  354. });
  355. it('gets fingerprint', function () {
  356. var fingerprint = doc.fingerprint;
  357. expect(typeof fingerprint === 'undefined' ? 'undefined' : _typeof(fingerprint)).toEqual('string');
  358. expect(fingerprint.length > 0).toEqual(true);
  359. });
  360. it('gets page', function (done) {
  361. var promise = doc.getPage(1);
  362. promise.then(function (data) {
  363. expect(data instanceof _api.PDFPageProxy).toEqual(true);
  364. expect(data.pageIndex).toEqual(0);
  365. done();
  366. }).catch(function (reason) {
  367. done.fail(reason);
  368. });
  369. });
  370. it('gets non-existent page', function (done) {
  371. var outOfRangePromise = doc.getPage(100);
  372. var nonIntegerPromise = doc.getPage(2.5);
  373. var nonNumberPromise = doc.getPage('1');
  374. outOfRangePromise = outOfRangePromise.then(function () {
  375. throw new Error('shall fail for out-of-range pageNumber parameter');
  376. }, function (reason) {
  377. expect(reason instanceof Error).toEqual(true);
  378. });
  379. nonIntegerPromise = nonIntegerPromise.then(function () {
  380. throw new Error('shall fail for non-integer pageNumber parameter');
  381. }, function (reason) {
  382. expect(reason instanceof Error).toEqual(true);
  383. });
  384. nonNumberPromise = nonNumberPromise.then(function () {
  385. throw new Error('shall fail for non-number pageNumber parameter');
  386. }, function (reason) {
  387. expect(reason instanceof Error).toEqual(true);
  388. });
  389. Promise.all([outOfRangePromise, nonIntegerPromise, nonNumberPromise]).then(function () {
  390. done();
  391. }).catch(function (reason) {
  392. done.fail(reason);
  393. });
  394. });
  395. it('gets page index', function (done) {
  396. var ref = {
  397. num: 17,
  398. gen: 0
  399. };
  400. var promise = doc.getPageIndex(ref);
  401. promise.then(function (pageIndex) {
  402. expect(pageIndex).toEqual(1);
  403. done();
  404. }).catch(function (reason) {
  405. done.fail(reason);
  406. });
  407. });
  408. it('gets invalid page index', function (done) {
  409. var ref = {
  410. num: 3,
  411. gen: 0
  412. };
  413. var promise = doc.getPageIndex(ref);
  414. promise.then(function () {
  415. done.fail('shall fail for invalid page reference.');
  416. }).catch(function (reason) {
  417. expect(reason instanceof Error).toEqual(true);
  418. done();
  419. });
  420. });
  421. it('gets destinations, from /Dests dictionary', function (done) {
  422. var promise = doc.getDestinations();
  423. promise.then(function (data) {
  424. expect(data).toEqual({
  425. chapter1: [{
  426. gen: 0,
  427. num: 17
  428. }, { name: 'XYZ' }, 0, 841.89, null]
  429. });
  430. done();
  431. }).catch(function (reason) {
  432. done.fail(reason);
  433. });
  434. });
  435. it('gets a destination, from /Dests dictionary', function (done) {
  436. var promise = doc.getDestination('chapter1');
  437. promise.then(function (data) {
  438. expect(data).toEqual([{
  439. gen: 0,
  440. num: 17
  441. }, { name: 'XYZ' }, 0, 841.89, null]);
  442. done();
  443. }).catch(function (reason) {
  444. done.fail(reason);
  445. });
  446. });
  447. it('gets a non-existent destination, from /Dests dictionary', function (done) {
  448. var promise = doc.getDestination('non-existent-named-destination');
  449. promise.then(function (data) {
  450. expect(data).toEqual(null);
  451. done();
  452. }).catch(function (reason) {
  453. done.fail(reason);
  454. });
  455. });
  456. it('gets destinations, from /Names (NameTree) dictionary', function (done) {
  457. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
  458. var promise = loadingTask.promise.then(function (pdfDocument) {
  459. return pdfDocument.getDestinations();
  460. });
  461. promise.then(function (destinations) {
  462. expect(destinations).toEqual({
  463. 'Page.1': [{
  464. num: 1,
  465. gen: 0
  466. }, { name: 'XYZ' }, 0, 375, null],
  467. 'Page.2': [{
  468. num: 6,
  469. gen: 0
  470. }, { name: 'XYZ' }, 0, 375, null]
  471. });
  472. loadingTask.destroy().then(done);
  473. }).catch(function (reason) {
  474. done.fail(reason);
  475. });
  476. });
  477. it('gets a destination, from /Names (NameTree) dictionary', function (done) {
  478. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
  479. var promise = loadingTask.promise.then(function (pdfDocument) {
  480. return pdfDocument.getDestination('Page.1');
  481. });
  482. promise.then(function (destination) {
  483. expect(destination).toEqual([{
  484. num: 1,
  485. gen: 0
  486. }, { name: 'XYZ' }, 0, 375, null]);
  487. loadingTask.destroy().then(done);
  488. }).catch(function (reason) {
  489. done.fail(reason);
  490. });
  491. });
  492. it('gets a non-existent destination, from /Names (NameTree) dictionary', function (done) {
  493. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
  494. var promise = loadingTask.promise.then(function (pdfDocument) {
  495. return pdfDocument.getDestination('non-existent-named-destination');
  496. });
  497. promise.then(function (destination) {
  498. expect(destination).toEqual(null);
  499. loadingTask.destroy().then(done);
  500. }).catch(function (reason) {
  501. done.fail(reason);
  502. });
  503. });
  504. it('gets non-existent page labels', function (done) {
  505. var promise = doc.getPageLabels();
  506. promise.then(function (data) {
  507. expect(data).toEqual(null);
  508. done();
  509. }).catch(function (reason) {
  510. done.fail(reason);
  511. });
  512. });
  513. it('gets page labels', function (done) {
  514. var loadingTask0 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug793632.pdf'));
  515. var promise0 = loadingTask0.promise.then(function (pdfDoc) {
  516. return pdfDoc.getPageLabels();
  517. });
  518. var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue1453.pdf'));
  519. var promise1 = loadingTask1.promise.then(function (pdfDoc) {
  520. return pdfDoc.getPageLabels();
  521. });
  522. var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('rotation.pdf'));
  523. var promise2 = loadingTask2.promise.then(function (pdfDoc) {
  524. return pdfDoc.getPageLabels();
  525. });
  526. var loadingTask3 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bad-PageLabels.pdf'));
  527. var promise3 = loadingTask3.promise.then(function (pdfDoc) {
  528. return pdfDoc.getPageLabels();
  529. });
  530. Promise.all([promise0, promise1, promise2, promise3]).then(function (pageLabels) {
  531. expect(pageLabels[0]).toEqual(['i', 'ii', 'iii', '1']);
  532. expect(pageLabels[1]).toEqual(['Front Page1']);
  533. expect(pageLabels[2]).toEqual(['1', '2']);
  534. expect(pageLabels[3]).toEqual(['X3']);
  535. Promise.all([loadingTask0.destroy(), loadingTask1.destroy(), loadingTask2.destroy(), loadingTask3.destroy()]).then(done);
  536. }).catch(function (reason) {
  537. done.fail(reason);
  538. });
  539. });
  540. it('gets default page mode', function (done) {
  541. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  542. loadingTask.promise.then(function (pdfDocument) {
  543. return pdfDocument.getPageMode();
  544. }).then(function (mode) {
  545. expect(mode).toEqual('UseNone');
  546. loadingTask.destroy().then(done);
  547. }).catch(function (reason) {
  548. done.fail(reason);
  549. });
  550. });
  551. it('gets non-default page mode', function (done) {
  552. doc.getPageMode().then(function (mode) {
  553. expect(mode).toEqual('UseOutlines');
  554. done();
  555. }).catch(function (reason) {
  556. done.fail(reason);
  557. });
  558. });
  559. it('gets non-existent attachments', function (done) {
  560. var promise = doc.getAttachments();
  561. promise.then(function (data) {
  562. expect(data).toEqual(null);
  563. done();
  564. }).catch(function (reason) {
  565. done.fail(reason);
  566. });
  567. });
  568. it('gets attachments', function (done) {
  569. if ((0, _is_node2.default)()) {
  570. pending('TODO: Use a non-linked test-case.');
  571. }
  572. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug766138.pdf'));
  573. var promise = loadingTask.promise.then(function (pdfDoc) {
  574. return pdfDoc.getAttachments();
  575. });
  576. promise.then(function (data) {
  577. var attachment = data['Press Quality.joboptions'];
  578. expect(attachment.filename).toEqual('Press Quality.joboptions');
  579. expect(attachment.content instanceof Uint8Array).toBeTruthy();
  580. expect(attachment.content.length).toEqual(30098);
  581. loadingTask.destroy().then(done);
  582. }).catch(function (reason) {
  583. done.fail(reason);
  584. });
  585. });
  586. it('gets javascript', function (done) {
  587. var promise = doc.getJavaScript();
  588. promise.then(function (data) {
  589. expect(data).toEqual(null);
  590. done();
  591. }).catch(function (reason) {
  592. done.fail(reason);
  593. });
  594. });
  595. var viewerPrintRegExp = /\bprint\s*\(/;
  596. it('gets javascript with printing instructions (Print action)', function (done) {
  597. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug1001080.pdf'));
  598. var promise = loadingTask.promise.then(function (doc) {
  599. return doc.getJavaScript();
  600. });
  601. promise.then(function (data) {
  602. expect(data).toEqual(['print({});']);
  603. expect(data[0]).toMatch(viewerPrintRegExp);
  604. loadingTask.destroy().then(done);
  605. }).catch(function (reason) {
  606. done.fail(reason);
  607. });
  608. });
  609. it('gets javascript with printing instructions (JS action)', function (done) {
  610. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6106.pdf'));
  611. var promise = loadingTask.promise.then(function (doc) {
  612. return doc.getJavaScript();
  613. });
  614. promise.then(function (data) {
  615. expect(data).toEqual(['this.print({bUI:true,bSilent:false,bShrinkToFit:true});']);
  616. expect(data[0]).toMatch(viewerPrintRegExp);
  617. loadingTask.destroy().then(done);
  618. }).catch(function (reason) {
  619. done.fail(reason);
  620. });
  621. });
  622. it('gets non-existent outline', function (done) {
  623. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  624. var promise = loadingTask.promise.then(function (pdfDocument) {
  625. return pdfDocument.getOutline();
  626. });
  627. promise.then(function (outline) {
  628. expect(outline).toEqual(null);
  629. loadingTask.destroy().then(done);
  630. }).catch(function (reason) {
  631. done.fail(reason);
  632. });
  633. });
  634. it('gets outline', function (done) {
  635. var promise = doc.getOutline();
  636. promise.then(function (outline) {
  637. expect(outline instanceof Array).toEqual(true);
  638. expect(outline.length).toEqual(2);
  639. var outlineItem = outline[1];
  640. expect(outlineItem.title).toEqual('Chapter 1');
  641. expect(outlineItem.dest instanceof Array).toEqual(true);
  642. expect(outlineItem.url).toEqual(null);
  643. expect(outlineItem.unsafeUrl).toBeUndefined();
  644. expect(outlineItem.newWindow).toBeUndefined();
  645. expect(outlineItem.bold).toEqual(true);
  646. expect(outlineItem.italic).toEqual(false);
  647. expect(outlineItem.color).toEqual(new Uint8Array([0, 64, 128]));
  648. expect(outlineItem.items.length).toEqual(1);
  649. expect(outlineItem.items[0].title).toEqual('Paragraph 1.1');
  650. done();
  651. }).catch(function (reason) {
  652. done.fail(reason);
  653. });
  654. });
  655. it('gets outline containing a url', function (done) {
  656. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue3214.pdf'));
  657. loadingTask.promise.then(function (pdfDocument) {
  658. pdfDocument.getOutline().then(function (outline) {
  659. expect(outline instanceof Array).toEqual(true);
  660. expect(outline.length).toEqual(5);
  661. var outlineItemTwo = outline[2];
  662. expect(_typeof(outlineItemTwo.title)).toEqual('string');
  663. expect(outlineItemTwo.dest).toEqual(null);
  664. expect(outlineItemTwo.url).toEqual('http://google.com/');
  665. expect(outlineItemTwo.unsafeUrl).toEqual('http://google.com');
  666. expect(outlineItemTwo.newWindow).toBeUndefined();
  667. var outlineItemOne = outline[1];
  668. expect(outlineItemOne.bold).toEqual(false);
  669. expect(outlineItemOne.italic).toEqual(true);
  670. expect(outlineItemOne.color).toEqual(new Uint8Array([0, 0, 0]));
  671. loadingTask.destroy().then(done);
  672. });
  673. }).catch(function (reason) {
  674. done.fail(reason);
  675. });
  676. });
  677. it('gets metadata', function (done) {
  678. var promise = doc.getMetadata();
  679. promise.then(function (metadata) {
  680. expect(metadata.info['Title']).toEqual('Basic API Test');
  681. expect(metadata.info['PDFFormatVersion']).toEqual('1.7');
  682. expect(metadata.metadata.get('dc:title')).toEqual('Basic API Test');
  683. done();
  684. }).catch(function (reason) {
  685. done.fail(reason);
  686. });
  687. });
  688. it('gets data', function (done) {
  689. var promise = doc.getData();
  690. promise.then(function (data) {
  691. expect(data instanceof Uint8Array).toEqual(true);
  692. expect(data.length).toEqual(basicApiFileLength);
  693. done();
  694. }).catch(function (reason) {
  695. done.fail(reason);
  696. });
  697. });
  698. it('gets download info', function (done) {
  699. var promise = doc.getDownloadInfo();
  700. promise.then(function (data) {
  701. expect(data).toEqual({ length: basicApiFileLength });
  702. done();
  703. }).catch(function (reason) {
  704. done.fail(reason);
  705. });
  706. });
  707. it('gets stats', function (done) {
  708. var promise = doc.getStats();
  709. promise.then(function (stats) {
  710. expect(stats).toEqual({
  711. streamTypes: [],
  712. fontTypes: []
  713. });
  714. done();
  715. }).catch(function (reason) {
  716. done.fail(reason);
  717. });
  718. });
  719. it('checks that fingerprints are unique', function (done) {
  720. var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue4436r.pdf'));
  721. var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue4575.pdf'));
  722. var promises = [loadingTask1.promise, loadingTask2.promise];
  723. Promise.all(promises).then(function (data) {
  724. var fingerprint1 = data[0].fingerprint;
  725. expect(typeof fingerprint1 === 'undefined' ? 'undefined' : _typeof(fingerprint1)).toEqual('string');
  726. expect(fingerprint1.length > 0).toEqual(true);
  727. var fingerprint2 = data[1].fingerprint;
  728. expect(typeof fingerprint2 === 'undefined' ? 'undefined' : _typeof(fingerprint2)).toEqual('string');
  729. expect(fingerprint2.length > 0).toEqual(true);
  730. expect(fingerprint1).not.toEqual(fingerprint2);
  731. Promise.all([loadingTask1.destroy(), loadingTask2.destroy()]).then(done);
  732. }).catch(function (reason) {
  733. done.fail(reason);
  734. });
  735. });
  736. describe('Cross-origin', function () {
  737. var loadingTask;
  738. function _checkCanLoad(expectSuccess, filename, options) {
  739. if ((0, _is_node2.default)()) {
  740. pending('Cannot simulate cross-origin requests in Node.js');
  741. }
  742. var params = (0, _test_utils.buildGetDocumentParams)(filename, options);
  743. var url = new URL(params.url);
  744. if (url.hostname === 'localhost') {
  745. url.hostname = '127.0.0.1';
  746. } else if (params.url.hostname === '127.0.0.1') {
  747. url.hostname = 'localhost';
  748. } else {
  749. pending('Can only run cross-origin test on localhost!');
  750. }
  751. params.url = url.href;
  752. loadingTask = (0, _api.getDocument)(params);
  753. return loadingTask.promise.then(function (pdf) {
  754. return pdf.destroy();
  755. }).then(function () {
  756. expect(expectSuccess).toEqual(true);
  757. }, function (error) {
  758. if (expectSuccess) {
  759. expect(error).toEqual('There should not be any error');
  760. }
  761. expect(expectSuccess).toEqual(false);
  762. });
  763. }
  764. function testCanLoad(filename, options) {
  765. return _checkCanLoad(true, filename, options);
  766. }
  767. function testCannotLoad(filename, options) {
  768. return _checkCanLoad(false, filename, options);
  769. }
  770. afterEach(function (done) {
  771. if (loadingTask) {
  772. loadingTask.destroy().then(done);
  773. } else {
  774. done();
  775. }
  776. });
  777. it('server disallows cors', function (done) {
  778. testCannotLoad('basicapi.pdf').then(done);
  779. });
  780. it('server allows cors without credentials, default withCredentials', function (done) {
  781. testCanLoad('basicapi.pdf?cors=withoutCredentials').then(done);
  782. });
  783. it('server allows cors without credentials, and withCredentials=false', function (done) {
  784. testCanLoad('basicapi.pdf?cors=withoutCredentials', { withCredentials: false }).then(done);
  785. });
  786. it('server allows cors without credentials, but withCredentials=true', function (done) {
  787. testCannotLoad('basicapi.pdf?cors=withoutCredentials', { withCredentials: true }).then(done);
  788. });
  789. it('server allows cors with credentials, and withCredentials=true', function (done) {
  790. testCanLoad('basicapi.pdf?cors=withCredentials', { withCredentials: true }).then(done);
  791. });
  792. it('server allows cors with credentials, and withCredentials=false', function (done) {
  793. testCanLoad('basicapi.pdf?cors=withCredentials', { withCredentials: false }).then(done);
  794. });
  795. });
  796. });
  797. describe('Page', function () {
  798. var loadingTask;
  799. var pdfDocument, page;
  800. beforeAll(function (done) {
  801. loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  802. loadingTask.promise.then(function (doc) {
  803. pdfDocument = doc;
  804. pdfDocument.getPage(1).then(function (data) {
  805. page = data;
  806. done();
  807. });
  808. }).catch(function (reason) {
  809. done.fail(reason);
  810. });
  811. });
  812. afterAll(function (done) {
  813. loadingTask.destroy().then(done);
  814. });
  815. it('gets page number', function () {
  816. expect(page.pageNumber).toEqual(1);
  817. });
  818. it('gets rotate', function () {
  819. expect(page.rotate).toEqual(0);
  820. });
  821. it('gets ref', function () {
  822. expect(page.ref).toEqual({
  823. num: 15,
  824. gen: 0
  825. });
  826. });
  827. it('gets userUnit', function () {
  828. expect(page.userUnit).toEqual(1.0);
  829. });
  830. it('gets view', function () {
  831. expect(page.view).toEqual([0, 0, 595.28, 841.89]);
  832. });
  833. it('gets viewport', function () {
  834. var viewport = page.getViewport(1.5, 90);
  835. expect(viewport.viewBox).toEqual(page.view);
  836. expect(viewport.scale).toEqual(1.5);
  837. expect(viewport.rotation).toEqual(90);
  838. expect(viewport.transform).toEqual([0, 1.5, 1.5, 0, 0, 0]);
  839. expect(viewport.width).toEqual(1262.835);
  840. expect(viewport.height).toEqual(892.92);
  841. });
  842. it('gets viewport respecting "dontFlip" argument', function () {
  843. var scale = 1;
  844. var rotation = 135;
  845. var viewport = page.getViewport(scale, rotation);
  846. var dontFlipViewport = page.getViewport(scale, rotation, true);
  847. expect(dontFlipViewport).not.toEqual(viewport);
  848. expect(dontFlipViewport).toEqual(viewport.clone({ dontFlip: true }));
  849. expect(viewport.transform).toEqual([1, 0, 0, -1, 0, 841.89]);
  850. expect(dontFlipViewport.transform).toEqual([1, 0, -0, 1, 0, 0]);
  851. });
  852. it('gets annotations', function (done) {
  853. var defaultPromise = page.getAnnotations().then(function (data) {
  854. expect(data.length).toEqual(4);
  855. });
  856. var displayPromise = page.getAnnotations({ intent: 'display' }).then(function (data) {
  857. expect(data.length).toEqual(4);
  858. });
  859. var printPromise = page.getAnnotations({ intent: 'print' }).then(function (data) {
  860. expect(data.length).toEqual(4);
  861. });
  862. Promise.all([defaultPromise, displayPromise, printPromise]).then(function () {
  863. done();
  864. }).catch(function (reason) {
  865. done.fail(reason);
  866. });
  867. });
  868. it('gets annotations containing relative URLs (bug 766086)', function (done) {
  869. var filename = 'bug766086.pdf';
  870. var defaultLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename));
  871. var defaultPromise = defaultLoadingTask.promise.then(function (pdfDoc) {
  872. return pdfDoc.getPage(1).then(function (pdfPage) {
  873. return pdfPage.getAnnotations();
  874. });
  875. });
  876. var docBaseUrlLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { docBaseUrl: 'http://www.example.com/test/pdfs/qwerty.pdf' }));
  877. var docBaseUrlPromise = docBaseUrlLoadingTask.promise.then(function (pdfDoc) {
  878. return pdfDoc.getPage(1).then(function (pdfPage) {
  879. return pdfPage.getAnnotations();
  880. });
  881. });
  882. var invalidDocBaseUrlLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { docBaseUrl: 'qwerty.pdf' }));
  883. var invalidDocBaseUrlPromise = invalidDocBaseUrlLoadingTask.promise.then(function (pdfDoc) {
  884. return pdfDoc.getPage(1).then(function (pdfPage) {
  885. return pdfPage.getAnnotations();
  886. });
  887. });
  888. Promise.all([defaultPromise, docBaseUrlPromise, invalidDocBaseUrlPromise]).then(function (data) {
  889. var defaultAnnotations = data[0];
  890. var docBaseUrlAnnotations = data[1];
  891. var invalidDocBaseUrlAnnotations = data[2];
  892. expect(defaultAnnotations[0].url).toBeUndefined();
  893. expect(defaultAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
  894. expect(docBaseUrlAnnotations[0].url).toEqual('http://www.example.com/0021/002156/215675E.pdf#15');
  895. expect(docBaseUrlAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
  896. expect(invalidDocBaseUrlAnnotations[0].url).toBeUndefined();
  897. expect(invalidDocBaseUrlAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
  898. Promise.all([defaultLoadingTask.destroy(), docBaseUrlLoadingTask.destroy(), invalidDocBaseUrlLoadingTask.destroy()]).then(done);
  899. }).catch(function (reason) {
  900. done.fail(reason);
  901. });
  902. });
  903. it('gets text content', function (done) {
  904. var defaultPromise = page.getTextContent();
  905. var parametersPromise = page.getTextContent({
  906. normalizeWhitespace: true,
  907. disableCombineTextItems: true
  908. });
  909. var promises = [defaultPromise, parametersPromise];
  910. Promise.all(promises).then(function (data) {
  911. expect(!!data[0].items).toEqual(true);
  912. expect(data[0].items.length).toEqual(7);
  913. expect(!!data[0].styles).toEqual(true);
  914. expect(JSON.stringify(data[0])).toEqual(JSON.stringify(data[1]));
  915. done();
  916. }).catch(function (reason) {
  917. done.fail(reason);
  918. });
  919. });
  920. it('gets operator list', function (done) {
  921. var promise = page.getOperatorList();
  922. promise.then(function (oplist) {
  923. expect(!!oplist.fnArray).toEqual(true);
  924. expect(!!oplist.argsArray).toEqual(true);
  925. expect(oplist.lastChunk).toEqual(true);
  926. done();
  927. }).catch(function (reason) {
  928. done.fail(reason);
  929. });
  930. });
  931. it('gets stats after parsing page', function (done) {
  932. var promise = page.getOperatorList().then(function () {
  933. return pdfDocument.getStats();
  934. });
  935. var expectedStreamTypes = [];
  936. expectedStreamTypes[_util.StreamType.FLATE] = true;
  937. var expectedFontTypes = [];
  938. expectedFontTypes[_util.FontType.TYPE1] = true;
  939. expectedFontTypes[_util.FontType.CIDFONTTYPE2] = true;
  940. promise.then(function (stats) {
  941. expect(stats).toEqual({
  942. streamTypes: expectedStreamTypes,
  943. fontTypes: expectedFontTypes
  944. });
  945. done();
  946. }).catch(function (reason) {
  947. done.fail(reason);
  948. });
  949. });
  950. it('cancels rendering of page', function (done) {
  951. if ((0, _is_node2.default)()) {
  952. pending('TODO: Support Canvas testing in Node.js.');
  953. }
  954. var viewport = page.getViewport(1);
  955. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  956. var renderTask = page.render({
  957. canvasContext: canvasAndCtx.context,
  958. viewport: viewport
  959. });
  960. renderTask.cancel();
  961. renderTask.promise.then(function () {
  962. done.fail('shall cancel rendering');
  963. }).catch(function (error) {
  964. expect(error instanceof _dom_utils.RenderingCancelledException).toEqual(true);
  965. expect(error.type).toEqual('canvas');
  966. CanvasFactory.destroy(canvasAndCtx);
  967. done();
  968. });
  969. });
  970. it('multiple render() on the same canvas', function (done) {
  971. if ((0, _is_node2.default)()) {
  972. pending('TODO: Support Canvas testing in Node.js.');
  973. }
  974. var viewport = page.getViewport(1);
  975. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  976. var renderTask1 = page.render({
  977. canvasContext: canvasAndCtx.context,
  978. viewport: viewport
  979. });
  980. var renderTask2 = page.render({
  981. canvasContext: canvasAndCtx.context,
  982. viewport: viewport
  983. });
  984. Promise.all([renderTask1.promise, renderTask2.promise.then(function () {
  985. done.fail('shall fail rendering');
  986. }, function (reason) {
  987. expect(/multiple render\(\)/.test(reason.message)).toEqual(true);
  988. })]).then(done);
  989. });
  990. });
  991. describe('Multiple PDFJS instances', function () {
  992. if ((0, _is_node2.default)()) {
  993. pending('TODO: Support Canvas testing in Node.js.');
  994. }
  995. var pdf1 = (0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf');
  996. var pdf2 = (0, _test_utils.buildGetDocumentParams)('TAMReview.pdf');
  997. var pdf3 = (0, _test_utils.buildGetDocumentParams)('issue6068.pdf');
  998. var loadingTasks = [];
  999. var pdfDocuments = [];
  1000. function renderPDF(filename) {
  1001. var loadingTask = (0, _api.getDocument)(filename);
  1002. loadingTasks.push(loadingTask);
  1003. return loadingTask.promise.then(function (pdf) {
  1004. pdfDocuments.push(pdf);
  1005. return pdf.getPage(1);
  1006. }).then(function (page) {
  1007. var viewport = page.getViewport(1.2);
  1008. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  1009. return page.render({
  1010. canvasContext: canvasAndCtx.context,
  1011. viewport: viewport
  1012. }).then(function () {
  1013. var data = canvasAndCtx.canvas.toDataURL();
  1014. CanvasFactory.destroy(canvasAndCtx);
  1015. return data;
  1016. });
  1017. });
  1018. }
  1019. afterEach(function (done) {
  1020. var destroyPromises = pdfDocuments.map(function (pdfDocument) {
  1021. return pdfDocument.destroy();
  1022. });
  1023. var destroyPromises2 = loadingTasks.map(function (loadingTask) {
  1024. return loadingTask.destroy();
  1025. });
  1026. Promise.all(destroyPromises.concat(destroyPromises2)).then(function () {
  1027. done();
  1028. });
  1029. });
  1030. it('should correctly render PDFs in parallel', function (done) {
  1031. var baseline1, baseline2, baseline3;
  1032. var promiseDone = renderPDF(pdf1).then(function (data1) {
  1033. baseline1 = data1;
  1034. return renderPDF(pdf2);
  1035. }).then(function (data2) {
  1036. baseline2 = data2;
  1037. return renderPDF(pdf3);
  1038. }).then(function (data3) {
  1039. baseline3 = data3;
  1040. return Promise.all([renderPDF(pdf1), renderPDF(pdf2), renderPDF(pdf3)]);
  1041. }).then(function (dataUrls) {
  1042. expect(dataUrls[0]).toEqual(baseline1);
  1043. expect(dataUrls[1]).toEqual(baseline2);
  1044. expect(dataUrls[2]).toEqual(baseline3);
  1045. return true;
  1046. });
  1047. promiseDone.then(function () {
  1048. done();
  1049. }).catch(function (reason) {
  1050. done.fail(reason);
  1051. });
  1052. });
  1053. });
  1054. describe('PDFDataRangeTransport', function () {
  1055. if ((0, _is_node2.default)()) {
  1056. pending('XMLHttpRequest is not supported in Node.js.');
  1057. }
  1058. var pdfPath = new URL('../pdfs/tracemonkey.pdf', window.location).href;
  1059. var loadPromise;
  1060. function getDocumentData() {
  1061. if (loadPromise) {
  1062. return loadPromise;
  1063. }
  1064. loadPromise = new Promise(function (resolve, reject) {
  1065. var xhr = new XMLHttpRequest(pdfPath);
  1066. xhr.open('GET', pdfPath);
  1067. xhr.responseType = 'arraybuffer';
  1068. xhr.onload = function () {
  1069. resolve(new Uint8Array(xhr.response));
  1070. };
  1071. xhr.onerror = function () {
  1072. reject(new Error('PDF is not loaded'));
  1073. };
  1074. xhr.send();
  1075. });
  1076. return loadPromise;
  1077. }
  1078. it('should fetch document info and page using ranges', function (done) {
  1079. var transport;
  1080. var initialDataLength = 4000;
  1081. var fetches = 0;
  1082. var getDocumentPromise = getDocumentData().then(function (data) {
  1083. var initialData = data.subarray(0, initialDataLength);
  1084. transport = new _global.PDFJS.PDFDataRangeTransport(data.length, initialData);
  1085. transport.requestDataRange = function (begin, end) {
  1086. fetches++;
  1087. waitSome(function () {
  1088. transport.onDataProgress(4000);
  1089. transport.onDataRange(begin, data.subarray(begin, end));
  1090. });
  1091. };
  1092. var loadingTask = (0, _api.getDocument)(transport);
  1093. return loadingTask.promise;
  1094. });
  1095. var pdfDocument;
  1096. var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
  1097. pdfDocument = pdfDocument_;
  1098. var pagePromise = pdfDocument.getPage(10);
  1099. return pagePromise;
  1100. });
  1101. getPagePromise.then(function (page) {
  1102. expect(pdfDocument.numPages).toEqual(14);
  1103. expect(page.rotate).toEqual(0);
  1104. expect(fetches).toBeGreaterThan(2);
  1105. done();
  1106. }).catch(function (reason) {
  1107. done.fail(reason);
  1108. });
  1109. });
  1110. it('should fetch document info and page using range and streaming', function (done) {
  1111. var transport;
  1112. var initialDataLength = 4000;
  1113. var fetches = 0;
  1114. var getDocumentPromise = getDocumentData().then(function (data) {
  1115. var initialData = data.subarray(0, initialDataLength);
  1116. transport = new _global.PDFJS.PDFDataRangeTransport(data.length, initialData);
  1117. transport.requestDataRange = function (begin, end) {
  1118. fetches++;
  1119. if (fetches === 1) {
  1120. transport.onDataProgressiveRead(data.subarray(initialDataLength));
  1121. }
  1122. waitSome(function () {
  1123. transport.onDataRange(begin, data.subarray(begin, end));
  1124. });
  1125. };
  1126. var loadingTask = (0, _api.getDocument)(transport);
  1127. return loadingTask.promise;
  1128. });
  1129. var pdfDocument;
  1130. var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
  1131. pdfDocument = pdfDocument_;
  1132. var pagePromise = pdfDocument.getPage(10);
  1133. return pagePromise;
  1134. });
  1135. getPagePromise.then(function (page) {
  1136. expect(pdfDocument.numPages).toEqual(14);
  1137. expect(page.rotate).toEqual(0);
  1138. expect(fetches).toEqual(1);
  1139. done();
  1140. }).catch(function (reason) {
  1141. done.fail(reason);
  1142. });
  1143. });
  1144. });
  1145. });