2
0

api_spec.js 46 KB

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