api_spec.js 42 KB

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