2
0

api_spec.js 45 KB

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