2
0

api_spec.js 47 KB

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