api_spec.js 47 KB

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