api_spec.js 41 KB

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