api_spec.js 46 KB

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