2
0

api_spec.js 46 KB

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