api_spec.js 45 KB

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