api_spec.js 45 KB

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