2
0

api_spec.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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('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 _api.PDFWorker({ name: '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 _api.PDFWorker({ name: '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 _api.PDFWorker({ name: 'test1' });
  314. var worker2 = new _api.PDFWorker({ name: 'test2' });
  315. var worker3 = new _api.PDFWorker({ name: '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(_worker_options.GlobalWorkerOptions.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 operatorList with JPEG image (issue 4888)', function (done) {
  927. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('cmykjpeg.pdf'));
  928. loadingTask.promise.then(function (pdfDoc) {
  929. pdfDoc.getPage(1).then(function (pdfPage) {
  930. pdfPage.getOperatorList().then(function (opList) {
  931. var imgIndex = opList.fnArray.indexOf(_util.OPS.paintImageXObject);
  932. var imgArgs = opList.argsArray[imgIndex];
  933. var _pdfPage$objs$get = pdfPage.objs.get(imgArgs[0]),
  934. imgData = _pdfPage$objs$get.data;
  935. expect(imgData instanceof Uint8ClampedArray).toEqual(true);
  936. expect(imgData.length).toEqual(90000);
  937. done();
  938. });
  939. });
  940. }).catch(function (reason) {
  941. done.fail(reason);
  942. });
  943. });
  944. it('gets stats after parsing page', function (done) {
  945. var promise = page.getOperatorList().then(function () {
  946. return pdfDocument.getStats();
  947. });
  948. var expectedStreamTypes = [];
  949. expectedStreamTypes[_util.StreamType.FLATE] = true;
  950. var expectedFontTypes = [];
  951. expectedFontTypes[_util.FontType.TYPE1] = true;
  952. expectedFontTypes[_util.FontType.CIDFONTTYPE2] = true;
  953. promise.then(function (stats) {
  954. expect(stats).toEqual({
  955. streamTypes: expectedStreamTypes,
  956. fontTypes: expectedFontTypes
  957. });
  958. done();
  959. }).catch(function (reason) {
  960. done.fail(reason);
  961. });
  962. });
  963. it('cancels rendering of page', function (done) {
  964. if ((0, _is_node2.default)()) {
  965. pending('TODO: Support Canvas testing in Node.js.');
  966. }
  967. var viewport = page.getViewport(1);
  968. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  969. var renderTask = page.render({
  970. canvasContext: canvasAndCtx.context,
  971. viewport: viewport
  972. });
  973. renderTask.cancel();
  974. renderTask.promise.then(function () {
  975. done.fail('shall cancel rendering');
  976. }).catch(function (error) {
  977. expect(error instanceof _dom_utils.RenderingCancelledException).toEqual(true);
  978. expect(error.type).toEqual('canvas');
  979. CanvasFactory.destroy(canvasAndCtx);
  980. done();
  981. });
  982. });
  983. it('multiple render() on the same canvas', function (done) {
  984. if ((0, _is_node2.default)()) {
  985. pending('TODO: Support Canvas testing in Node.js.');
  986. }
  987. var viewport = page.getViewport(1);
  988. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  989. var renderTask1 = page.render({
  990. canvasContext: canvasAndCtx.context,
  991. viewport: viewport
  992. });
  993. var renderTask2 = page.render({
  994. canvasContext: canvasAndCtx.context,
  995. viewport: viewport
  996. });
  997. Promise.all([renderTask1.promise, renderTask2.promise.then(function () {
  998. done.fail('shall fail rendering');
  999. }, function (reason) {
  1000. expect(/multiple render\(\)/.test(reason.message)).toEqual(true);
  1001. })]).then(done);
  1002. });
  1003. });
  1004. describe('Multiple PDFJS instances', function () {
  1005. if ((0, _is_node2.default)()) {
  1006. pending('TODO: Support Canvas testing in Node.js.');
  1007. }
  1008. var pdf1 = (0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf');
  1009. var pdf2 = (0, _test_utils.buildGetDocumentParams)('TAMReview.pdf');
  1010. var pdf3 = (0, _test_utils.buildGetDocumentParams)('issue6068.pdf');
  1011. var loadingTasks = [];
  1012. var pdfDocuments = [];
  1013. function renderPDF(filename) {
  1014. var loadingTask = (0, _api.getDocument)(filename);
  1015. loadingTasks.push(loadingTask);
  1016. return loadingTask.promise.then(function (pdf) {
  1017. pdfDocuments.push(pdf);
  1018. return pdf.getPage(1);
  1019. }).then(function (page) {
  1020. var viewport = page.getViewport(1.2);
  1021. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  1022. return page.render({
  1023. canvasContext: canvasAndCtx.context,
  1024. viewport: viewport
  1025. }).then(function () {
  1026. var data = canvasAndCtx.canvas.toDataURL();
  1027. CanvasFactory.destroy(canvasAndCtx);
  1028. return data;
  1029. });
  1030. });
  1031. }
  1032. afterEach(function (done) {
  1033. var destroyPromises = pdfDocuments.map(function (pdfDocument) {
  1034. return pdfDocument.destroy();
  1035. });
  1036. var destroyPromises2 = loadingTasks.map(function (loadingTask) {
  1037. return loadingTask.destroy();
  1038. });
  1039. Promise.all(destroyPromises.concat(destroyPromises2)).then(function () {
  1040. done();
  1041. });
  1042. });
  1043. it('should correctly render PDFs in parallel', function (done) {
  1044. var baseline1, baseline2, baseline3;
  1045. var promiseDone = renderPDF(pdf1).then(function (data1) {
  1046. baseline1 = data1;
  1047. return renderPDF(pdf2);
  1048. }).then(function (data2) {
  1049. baseline2 = data2;
  1050. return renderPDF(pdf3);
  1051. }).then(function (data3) {
  1052. baseline3 = data3;
  1053. return Promise.all([renderPDF(pdf1), renderPDF(pdf2), renderPDF(pdf3)]);
  1054. }).then(function (dataUrls) {
  1055. expect(dataUrls[0]).toEqual(baseline1);
  1056. expect(dataUrls[1]).toEqual(baseline2);
  1057. expect(dataUrls[2]).toEqual(baseline3);
  1058. return true;
  1059. });
  1060. promiseDone.then(function () {
  1061. done();
  1062. }).catch(function (reason) {
  1063. done.fail(reason);
  1064. });
  1065. });
  1066. });
  1067. describe('PDFDataRangeTransport', function () {
  1068. if ((0, _is_node2.default)()) {
  1069. pending('XMLHttpRequest is not supported in Node.js.');
  1070. }
  1071. var pdfPath = new URL('../pdfs/tracemonkey.pdf', window.location).href;
  1072. var loadPromise;
  1073. function getDocumentData() {
  1074. if (loadPromise) {
  1075. return loadPromise;
  1076. }
  1077. loadPromise = new Promise(function (resolve, reject) {
  1078. var xhr = new XMLHttpRequest(pdfPath);
  1079. xhr.open('GET', pdfPath);
  1080. xhr.responseType = 'arraybuffer';
  1081. xhr.onload = function () {
  1082. resolve(new Uint8Array(xhr.response));
  1083. };
  1084. xhr.onerror = function () {
  1085. reject(new Error('PDF is not loaded'));
  1086. };
  1087. xhr.send();
  1088. });
  1089. return loadPromise;
  1090. }
  1091. it('should fetch document info and page using ranges', function (done) {
  1092. var transport;
  1093. var initialDataLength = 4000;
  1094. var fetches = 0;
  1095. var getDocumentPromise = getDocumentData().then(function (data) {
  1096. var initialData = data.subarray(0, initialDataLength);
  1097. transport = new _api.PDFDataRangeTransport(data.length, initialData);
  1098. transport.requestDataRange = function (begin, end) {
  1099. fetches++;
  1100. waitSome(function () {
  1101. transport.onDataProgress(4000);
  1102. transport.onDataRange(begin, data.subarray(begin, end));
  1103. });
  1104. };
  1105. var loadingTask = (0, _api.getDocument)(transport);
  1106. return loadingTask.promise;
  1107. });
  1108. var pdfDocument;
  1109. var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
  1110. pdfDocument = pdfDocument_;
  1111. var pagePromise = pdfDocument.getPage(10);
  1112. return pagePromise;
  1113. });
  1114. getPagePromise.then(function (page) {
  1115. expect(pdfDocument.numPages).toEqual(14);
  1116. expect(page.rotate).toEqual(0);
  1117. expect(fetches).toBeGreaterThan(2);
  1118. done();
  1119. }).catch(function (reason) {
  1120. done.fail(reason);
  1121. });
  1122. });
  1123. it('should fetch document info and page using range and streaming', function (done) {
  1124. var transport;
  1125. var initialDataLength = 4000;
  1126. var fetches = 0;
  1127. var getDocumentPromise = getDocumentData().then(function (data) {
  1128. var initialData = data.subarray(0, initialDataLength);
  1129. transport = new _api.PDFDataRangeTransport(data.length, initialData);
  1130. transport.requestDataRange = function (begin, end) {
  1131. fetches++;
  1132. if (fetches === 1) {
  1133. transport.onDataProgressiveRead(data.subarray(initialDataLength));
  1134. }
  1135. waitSome(function () {
  1136. transport.onDataRange(begin, data.subarray(begin, end));
  1137. });
  1138. };
  1139. var loadingTask = (0, _api.getDocument)(transport);
  1140. return loadingTask.promise;
  1141. });
  1142. var pdfDocument;
  1143. var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
  1144. pdfDocument = pdfDocument_;
  1145. var pagePromise = pdfDocument.getPage(10);
  1146. return pagePromise;
  1147. });
  1148. getPagePromise.then(function (page) {
  1149. expect(pdfDocument.numPages).toEqual(14);
  1150. expect(page.rotate).toEqual(0);
  1151. expect(fetches).toEqual(1);
  1152. done();
  1153. }).catch(function (reason) {
  1154. done.fail(reason);
  1155. });
  1156. });
  1157. });
  1158. });