api_spec.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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 _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  24. var _test_utils = require("./test_utils");
  25. var _util = require("../../shared/util");
  26. var _display_utils = require("../../display/display_utils");
  27. var _api = require("../../display/api");
  28. var _worker_options = require("../../display/worker_options");
  29. var _is_node = _interopRequireDefault(require("../../shared/is_node"));
  30. var _metadata = require("../../display/metadata");
  31. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  32. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  33. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  34. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  35. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  36. function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  37. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  38. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  39. describe('api', function () {
  40. var basicApiFileName = 'basicapi.pdf';
  41. var basicApiFileLength = 105779;
  42. var basicApiGetDocumentParams = (0, _test_utils.buildGetDocumentParams)(basicApiFileName);
  43. var CanvasFactory;
  44. beforeAll(function (done) {
  45. if ((0, _is_node["default"])()) {
  46. CanvasFactory = new _test_utils.NodeCanvasFactory();
  47. } else {
  48. CanvasFactory = new _display_utils.DOMCanvasFactory();
  49. }
  50. done();
  51. });
  52. afterAll(function (done) {
  53. CanvasFactory = null;
  54. done();
  55. });
  56. function waitSome(callback) {
  57. var WAIT_TIMEOUT = 10;
  58. setTimeout(function () {
  59. callback();
  60. }, WAIT_TIMEOUT);
  61. }
  62. describe('getDocument', function () {
  63. it('creates pdf doc from URL', function (done) {
  64. var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  65. var progressReportedCapability = (0, _util.createPromiseCapability)();
  66. loadingTask.onProgress = function (progressData) {
  67. if (!progressReportedCapability.settled) {
  68. progressReportedCapability.resolve(progressData);
  69. }
  70. };
  71. var promises = [progressReportedCapability.promise, loadingTask.promise];
  72. Promise.all(promises).then(function (data) {
  73. expect(data[0].loaded / data[0].total >= 0).toEqual(true);
  74. expect(data[1] instanceof _api.PDFDocumentProxy).toEqual(true);
  75. expect(loadingTask).toEqual(data[1].loadingTask);
  76. loadingTask.destroy().then(done);
  77. })["catch"](done.fail);
  78. });
  79. it('creates pdf doc from URL and aborts before worker initialized', function (done) {
  80. var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  81. var destroyed = loadingTask.destroy();
  82. loadingTask.promise.then(function (reason) {
  83. done.fail('shall fail loading');
  84. })["catch"](function (reason) {
  85. expect(true).toEqual(true);
  86. destroyed.then(done);
  87. });
  88. });
  89. it('creates pdf doc from URL and aborts loading after worker initialized', function (done) {
  90. var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  91. var destroyed = loadingTask._worker.promise.then(function () {
  92. return loadingTask.destroy();
  93. });
  94. destroyed.then(function (data) {
  95. expect(true).toEqual(true);
  96. done();
  97. })["catch"](done.fail);
  98. });
  99. it('creates pdf doc from typed array', function (done) {
  100. var typedArrayPdfPromise;
  101. if ((0, _is_node["default"])()) {
  102. typedArrayPdfPromise = _test_utils.NodeFileReaderFactory.fetch({
  103. path: _test_utils.TEST_PDFS_PATH.node + basicApiFileName
  104. });
  105. } else {
  106. typedArrayPdfPromise = _test_utils.DOMFileReaderFactory.fetch({
  107. path: _test_utils.TEST_PDFS_PATH.dom + basicApiFileName
  108. });
  109. }
  110. typedArrayPdfPromise.then(function (typedArrayPdf) {
  111. expect(typedArrayPdf.length).toEqual(basicApiFileLength);
  112. var loadingTask = (0, _api.getDocument)(typedArrayPdf);
  113. var progressReportedCapability = (0, _util.createPromiseCapability)();
  114. loadingTask.onProgress = function (data) {
  115. progressReportedCapability.resolve(data);
  116. };
  117. return Promise.all([loadingTask.promise, progressReportedCapability.promise]).then(function (data) {
  118. expect(data[0] instanceof _api.PDFDocumentProxy).toEqual(true);
  119. expect(data[1].loaded / data[1].total).toEqual(1);
  120. loadingTask.destroy().then(done);
  121. });
  122. })["catch"](done.fail);
  123. });
  124. it('creates pdf doc from invalid PDF file', function (done) {
  125. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug1020226.pdf'));
  126. loadingTask.promise.then(function () {
  127. done.fail('shall fail loading');
  128. })["catch"](function (error) {
  129. expect(error instanceof _util.InvalidPDFException).toEqual(true);
  130. loadingTask.destroy().then(done);
  131. });
  132. });
  133. it('creates pdf doc from non-existent URL', function (done) {
  134. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('non-existent.pdf'));
  135. loadingTask.promise.then(function (error) {
  136. done.fail('shall fail loading');
  137. })["catch"](function (error) {
  138. expect(error instanceof _util.MissingPDFException).toEqual(true);
  139. loadingTask.destroy().then(done);
  140. });
  141. });
  142. it('creates pdf doc from PDF file protected with user and owner password', function (done) {
  143. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('pr6531_1.pdf'));
  144. var passwordNeededCapability = (0, _util.createPromiseCapability)();
  145. var passwordIncorrectCapability = (0, _util.createPromiseCapability)();
  146. loadingTask.onPassword = function (updatePassword, reason) {
  147. if (reason === _util.PasswordResponses.NEED_PASSWORD && !passwordNeededCapability.settled) {
  148. passwordNeededCapability.resolve();
  149. updatePassword('qwerty');
  150. return;
  151. }
  152. if (reason === _util.PasswordResponses.INCORRECT_PASSWORD && !passwordIncorrectCapability.settled) {
  153. passwordIncorrectCapability.resolve();
  154. updatePassword('asdfasdf');
  155. return;
  156. }
  157. expect(false).toEqual(true);
  158. };
  159. var promises = [passwordNeededCapability.promise, passwordIncorrectCapability.promise, loadingTask.promise];
  160. Promise.all(promises).then(function (data) {
  161. expect(data[2] instanceof _api.PDFDocumentProxy).toEqual(true);
  162. loadingTask.destroy().then(done);
  163. })["catch"](done.fail);
  164. });
  165. it('creates pdf doc from PDF file protected with only a user password', function (done) {
  166. var filename = 'pr6531_2.pdf';
  167. var passwordNeededLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
  168. password: ''
  169. }));
  170. var result1 = passwordNeededLoadingTask.promise.then(function () {
  171. done.fail('shall fail with no password');
  172. return Promise.reject(new Error('loadingTask should be rejected'));
  173. }, function (data) {
  174. expect(data instanceof _util.PasswordException).toEqual(true);
  175. expect(data.code).toEqual(_util.PasswordResponses.NEED_PASSWORD);
  176. return passwordNeededLoadingTask.destroy();
  177. });
  178. var passwordIncorrectLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
  179. password: 'qwerty'
  180. }));
  181. var result2 = passwordIncorrectLoadingTask.promise.then(function () {
  182. done.fail('shall fail with wrong password');
  183. return Promise.reject(new Error('loadingTask should be rejected'));
  184. }, function (data) {
  185. expect(data instanceof _util.PasswordException).toEqual(true);
  186. expect(data.code).toEqual(_util.PasswordResponses.INCORRECT_PASSWORD);
  187. return passwordIncorrectLoadingTask.destroy();
  188. });
  189. var passwordAcceptedLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
  190. password: 'asdfasdf'
  191. }));
  192. var result3 = passwordAcceptedLoadingTask.promise.then(function (data) {
  193. expect(data instanceof _api.PDFDocumentProxy).toEqual(true);
  194. return passwordAcceptedLoadingTask.destroy();
  195. });
  196. Promise.all([result1, result2, result3]).then(function () {
  197. done();
  198. })["catch"](done.fail);
  199. });
  200. it('creates pdf doc from password protected PDF file and aborts/throws ' + 'in the onPassword callback (issue 7806)', function (done) {
  201. var filename = 'issue3371.pdf';
  202. var passwordNeededLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename));
  203. var passwordIncorrectLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
  204. password: 'qwerty'
  205. }));
  206. var passwordNeededDestroyed;
  207. passwordNeededLoadingTask.onPassword = function (callback, reason) {
  208. if (reason === _util.PasswordResponses.NEED_PASSWORD) {
  209. passwordNeededDestroyed = passwordNeededLoadingTask.destroy();
  210. return;
  211. }
  212. expect(false).toEqual(true);
  213. };
  214. var result1 = passwordNeededLoadingTask.promise.then(function () {
  215. done.fail('shall fail since the loadingTask should be destroyed');
  216. return Promise.reject(new Error('loadingTask should be rejected'));
  217. }, function (reason) {
  218. expect(reason instanceof _util.PasswordException).toEqual(true);
  219. expect(reason.code).toEqual(_util.PasswordResponses.NEED_PASSWORD);
  220. return passwordNeededDestroyed;
  221. });
  222. passwordIncorrectLoadingTask.onPassword = function (callback, reason) {
  223. if (reason === _util.PasswordResponses.INCORRECT_PASSWORD) {
  224. throw new Error('Incorrect password');
  225. }
  226. expect(false).toEqual(true);
  227. };
  228. var result2 = passwordIncorrectLoadingTask.promise.then(function () {
  229. done.fail('shall fail since the onPassword callback should throw');
  230. return Promise.reject(new Error('loadingTask should be rejected'));
  231. }, function (reason) {
  232. expect(reason instanceof _util.PasswordException).toEqual(true);
  233. expect(reason.code).toEqual(_util.PasswordResponses.INCORRECT_PASSWORD);
  234. return passwordIncorrectLoadingTask.destroy();
  235. });
  236. Promise.all([result1, result2]).then(function () {
  237. done();
  238. })["catch"](done.fail);
  239. });
  240. });
  241. describe('PDFWorker', function () {
  242. it('worker created or destroyed', function (done) {
  243. if ((0, _is_node["default"])()) {
  244. pending('Worker is not supported in Node.js.');
  245. }
  246. var worker = new _api.PDFWorker({
  247. name: 'test1'
  248. });
  249. worker.promise.then(function () {
  250. expect(worker.name).toEqual('test1');
  251. expect(!!worker.port).toEqual(true);
  252. expect(worker.destroyed).toEqual(false);
  253. expect(!!worker._webWorker).toEqual(true);
  254. expect(worker.port === worker._webWorker).toEqual(true);
  255. worker.destroy();
  256. expect(!!worker.port).toEqual(false);
  257. expect(worker.destroyed).toEqual(true);
  258. done();
  259. })["catch"](done.fail);
  260. });
  261. it('worker created or destroyed by getDocument', function (done) {
  262. if ((0, _is_node["default"])()) {
  263. pending('Worker is not supported in Node.js.');
  264. }
  265. var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  266. var worker;
  267. loadingTask.promise.then(function () {
  268. worker = loadingTask._worker;
  269. expect(!!worker).toEqual(true);
  270. });
  271. var destroyPromise = loadingTask.promise.then(function () {
  272. return loadingTask.destroy();
  273. });
  274. destroyPromise.then(function () {
  275. var destroyedWorker = loadingTask._worker;
  276. expect(!!destroyedWorker).toEqual(false);
  277. expect(worker.destroyed).toEqual(true);
  278. done();
  279. })["catch"](done.fail);
  280. });
  281. it('worker created and can be used in getDocument', function (done) {
  282. if ((0, _is_node["default"])()) {
  283. pending('Worker is not supported in Node.js.');
  284. }
  285. var worker = new _api.PDFWorker({
  286. name: 'test1'
  287. });
  288. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, {
  289. worker: worker
  290. }));
  291. loadingTask.promise.then(function () {
  292. var docWorker = loadingTask._worker;
  293. expect(!!docWorker).toEqual(false);
  294. var messageHandlerPort = loadingTask._transport.messageHandler.comObj;
  295. expect(messageHandlerPort === worker.port).toEqual(true);
  296. });
  297. var destroyPromise = loadingTask.promise.then(function () {
  298. return loadingTask.destroy();
  299. });
  300. destroyPromise.then(function () {
  301. expect(worker.destroyed).toEqual(false);
  302. worker.destroy();
  303. done();
  304. })["catch"](done.fail);
  305. });
  306. it('creates more than one worker', function (done) {
  307. if ((0, _is_node["default"])()) {
  308. pending('Worker is not supported in Node.js.');
  309. }
  310. var worker1 = new _api.PDFWorker({
  311. name: 'test1'
  312. });
  313. var worker2 = new _api.PDFWorker({
  314. name: 'test2'
  315. });
  316. var worker3 = new _api.PDFWorker({
  317. name: 'test3'
  318. });
  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"](done.fail);
  327. });
  328. it('gets current workerSrc', function () {
  329. if ((0, _is_node["default"])()) {
  330. pending('Worker is not supported in Node.js.');
  331. }
  332. var workerSrc = _api.PDFWorker.getWorkerSrc();
  333. expect(_typeof(workerSrc)).toEqual('string');
  334. expect(workerSrc).toEqual(_worker_options.GlobalWorkerOptions.workerSrc);
  335. });
  336. });
  337. describe('PDFDocument', function () {
  338. var loadingTask;
  339. var doc;
  340. beforeAll(function (done) {
  341. loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  342. loadingTask.promise.then(function (data) {
  343. doc = data;
  344. done();
  345. });
  346. });
  347. afterAll(function (done) {
  348. loadingTask.destroy().then(done);
  349. });
  350. it('gets number of pages', function () {
  351. expect(doc.numPages).toEqual(3);
  352. });
  353. it('gets fingerprint', function () {
  354. var fingerprint = doc.fingerprint;
  355. expect(_typeof(fingerprint)).toEqual('string');
  356. expect(fingerprint.length > 0).toEqual(true);
  357. });
  358. it('gets page', function (done) {
  359. var promise = doc.getPage(1);
  360. promise.then(function (data) {
  361. expect(data instanceof _api.PDFPageProxy).toEqual(true);
  362. expect(data.pageIndex).toEqual(0);
  363. done();
  364. })["catch"](done.fail);
  365. });
  366. it('gets non-existent page', function (done) {
  367. var outOfRangePromise = doc.getPage(100);
  368. var nonIntegerPromise = doc.getPage(2.5);
  369. var nonNumberPromise = doc.getPage('1');
  370. outOfRangePromise = outOfRangePromise.then(function () {
  371. throw new Error('shall fail for out-of-range pageNumber parameter');
  372. }, function (reason) {
  373. expect(reason instanceof Error).toEqual(true);
  374. });
  375. nonIntegerPromise = nonIntegerPromise.then(function () {
  376. throw new Error('shall fail for non-integer pageNumber parameter');
  377. }, function (reason) {
  378. expect(reason instanceof Error).toEqual(true);
  379. });
  380. nonNumberPromise = nonNumberPromise.then(function () {
  381. throw new Error('shall fail for non-number pageNumber parameter');
  382. }, function (reason) {
  383. expect(reason instanceof Error).toEqual(true);
  384. });
  385. Promise.all([outOfRangePromise, nonIntegerPromise, nonNumberPromise]).then(function () {
  386. done();
  387. })["catch"](done.fail);
  388. });
  389. it('gets page index', function (done) {
  390. var ref = {
  391. num: 17,
  392. gen: 0
  393. };
  394. var promise = doc.getPageIndex(ref);
  395. promise.then(function (pageIndex) {
  396. expect(pageIndex).toEqual(1);
  397. done();
  398. })["catch"](done.fail);
  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. }, {
  421. name: 'XYZ'
  422. }, 0, 841.89, null]
  423. });
  424. done();
  425. })["catch"](done.fail);
  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. }, {
  434. name: 'XYZ'
  435. }, 0, 841.89, null]);
  436. done();
  437. })["catch"](done.fail);
  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"](done.fail);
  445. });
  446. it('gets destinations, from /Names (NameTree) dictionary', function (done) {
  447. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
  448. var promise = loadingTask.promise.then(function (pdfDocument) {
  449. return pdfDocument.getDestinations();
  450. });
  451. promise.then(function (destinations) {
  452. expect(destinations).toEqual({
  453. 'Page.1': [{
  454. num: 1,
  455. gen: 0
  456. }, {
  457. name: 'XYZ'
  458. }, 0, 375, null],
  459. 'Page.2': [{
  460. num: 6,
  461. gen: 0
  462. }, {
  463. name: 'XYZ'
  464. }, 0, 375, null]
  465. });
  466. loadingTask.destroy().then(done);
  467. })["catch"](done.fail);
  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. }, {
  479. name: 'XYZ'
  480. }, 0, 375, null]);
  481. loadingTask.destroy().then(done);
  482. })["catch"](done.fail);
  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"](done.fail);
  493. });
  494. it('gets non-string destination', function (done) {
  495. var numberPromise = doc.getDestination(4.3);
  496. var booleanPromise = doc.getDestination(true);
  497. var arrayPromise = doc.getDestination([{
  498. num: 17,
  499. gen: 0
  500. }, {
  501. name: 'XYZ'
  502. }, 0, 841.89, null]);
  503. numberPromise = numberPromise.then(function () {
  504. throw new Error('shall fail for non-string destination.');
  505. }, function (reason) {
  506. expect(reason instanceof Error).toEqual(true);
  507. });
  508. booleanPromise = booleanPromise.then(function () {
  509. throw new Error('shall fail for non-string destination.');
  510. }, function (reason) {
  511. expect(reason instanceof Error).toEqual(true);
  512. });
  513. arrayPromise = arrayPromise.then(function () {
  514. throw new Error('shall fail for non-string destination.');
  515. }, function (reason) {
  516. expect(reason instanceof Error).toEqual(true);
  517. });
  518. Promise.all([numberPromise, booleanPromise, arrayPromise]).then(done, done.fail);
  519. });
  520. it('gets non-existent page labels', function (done) {
  521. var promise = doc.getPageLabels();
  522. promise.then(function (data) {
  523. expect(data).toEqual(null);
  524. done();
  525. })["catch"](done.fail);
  526. });
  527. it('gets page labels', function (done) {
  528. var loadingTask0 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug793632.pdf'));
  529. var promise0 = loadingTask0.promise.then(function (pdfDoc) {
  530. return pdfDoc.getPageLabels();
  531. });
  532. var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue1453.pdf'));
  533. var promise1 = loadingTask1.promise.then(function (pdfDoc) {
  534. return pdfDoc.getPageLabels();
  535. });
  536. var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('rotation.pdf'));
  537. var promise2 = loadingTask2.promise.then(function (pdfDoc) {
  538. return pdfDoc.getPageLabels();
  539. });
  540. var loadingTask3 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bad-PageLabels.pdf'));
  541. var promise3 = loadingTask3.promise.then(function (pdfDoc) {
  542. return pdfDoc.getPageLabels();
  543. });
  544. Promise.all([promise0, promise1, promise2, promise3]).then(function (pageLabels) {
  545. expect(pageLabels[0]).toEqual(['i', 'ii', 'iii', '1']);
  546. expect(pageLabels[1]).toEqual(['Front Page1']);
  547. expect(pageLabels[2]).toEqual(['1', '2']);
  548. expect(pageLabels[3]).toEqual(['X3']);
  549. Promise.all([loadingTask0.destroy(), loadingTask1.destroy(), loadingTask2.destroy(), loadingTask3.destroy()]).then(done);
  550. })["catch"](done.fail);
  551. });
  552. it('gets default page layout', function (done) {
  553. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  554. loadingTask.promise.then(function (pdfDocument) {
  555. return pdfDocument.getPageLayout();
  556. }).then(function (mode) {
  557. expect(mode).toEqual('');
  558. loadingTask.destroy().then(done);
  559. })["catch"](done.fail);
  560. });
  561. it('gets non-default page layout', function (done) {
  562. doc.getPageLayout().then(function (mode) {
  563. expect(mode).toEqual('SinglePage');
  564. done();
  565. })["catch"](done.fail);
  566. });
  567. it('gets default page mode', function (done) {
  568. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  569. loadingTask.promise.then(function (pdfDocument) {
  570. return pdfDocument.getPageMode();
  571. }).then(function (mode) {
  572. expect(mode).toEqual('UseNone');
  573. loadingTask.destroy().then(done);
  574. })["catch"](done.fail);
  575. });
  576. it('gets non-default page mode', function (done) {
  577. doc.getPageMode().then(function (mode) {
  578. expect(mode).toEqual('UseOutlines');
  579. done();
  580. })["catch"](done.fail);
  581. });
  582. it('gets default viewer preferences', function (done) {
  583. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  584. loadingTask.promise.then(function (pdfDocument) {
  585. return pdfDocument.getViewerPreferences();
  586. }).then(function (prefs) {
  587. expect(_typeof(prefs) === 'object' && prefs !== null && (0, _util.isEmptyObj)(prefs)).toEqual(true);
  588. loadingTask.destroy().then(done);
  589. })["catch"](done.fail);
  590. });
  591. it('gets non-default viewer preferences', function (done) {
  592. doc.getViewerPreferences().then(function (prefs) {
  593. expect(prefs).toEqual({
  594. Direction: 'L2R'
  595. });
  596. done();
  597. })["catch"](done.fail);
  598. });
  599. it('gets default open action destination', function (done) {
  600. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  601. loadingTask.promise.then(function (pdfDocument) {
  602. return pdfDocument.getOpenActionDestination();
  603. }).then(function (dest) {
  604. expect(dest).toEqual(null);
  605. loadingTask.destroy().then(done);
  606. })["catch"](done.fail);
  607. });
  608. it('gets non-default open action destination', function (done) {
  609. doc.getOpenActionDestination().then(function (dest) {
  610. expect(dest).toEqual([{
  611. num: 15,
  612. gen: 0
  613. }, {
  614. name: 'FitH'
  615. }, null]);
  616. done();
  617. })["catch"](done.fail);
  618. });
  619. it('gets non-existent attachments', function (done) {
  620. var promise = doc.getAttachments();
  621. promise.then(function (data) {
  622. expect(data).toEqual(null);
  623. done();
  624. })["catch"](done.fail);
  625. });
  626. it('gets attachments', function (done) {
  627. if ((0, _is_node["default"])()) {
  628. pending('TODO: Use a non-linked test-case.');
  629. }
  630. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug766138.pdf'));
  631. var promise = loadingTask.promise.then(function (pdfDoc) {
  632. return pdfDoc.getAttachments();
  633. });
  634. promise.then(function (data) {
  635. var attachment = data['Press Quality.joboptions'];
  636. expect(attachment.filename).toEqual('Press Quality.joboptions');
  637. expect(attachment.content instanceof Uint8Array).toBeTruthy();
  638. expect(attachment.content.length).toEqual(30098);
  639. loadingTask.destroy().then(done);
  640. })["catch"](done.fail);
  641. });
  642. it('gets javascript', function (done) {
  643. var promise = doc.getJavaScript();
  644. promise.then(function (data) {
  645. expect(data).toEqual(null);
  646. done();
  647. })["catch"](done.fail);
  648. });
  649. var viewerPrintRegExp = /\bprint\s*\(/;
  650. it('gets javascript with printing instructions (Print action)', function (done) {
  651. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug1001080.pdf'));
  652. var promise = loadingTask.promise.then(function (doc) {
  653. return doc.getJavaScript();
  654. });
  655. promise.then(function (data) {
  656. expect(data).toEqual(['print({});']);
  657. expect(data[0]).toMatch(viewerPrintRegExp);
  658. loadingTask.destroy().then(done);
  659. })["catch"](done.fail);
  660. });
  661. it('gets javascript with printing instructions (JS action)', function (done) {
  662. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6106.pdf'));
  663. var promise = loadingTask.promise.then(function (doc) {
  664. return doc.getJavaScript();
  665. });
  666. promise.then(function (data) {
  667. expect(data).toEqual(['this.print({bUI:true,bSilent:false,bShrinkToFit:true});']);
  668. expect(data[0]).toMatch(viewerPrintRegExp);
  669. loadingTask.destroy().then(done);
  670. })["catch"](done.fail);
  671. });
  672. it('gets non-existent outline', function (done) {
  673. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  674. var promise = loadingTask.promise.then(function (pdfDocument) {
  675. return pdfDocument.getOutline();
  676. });
  677. promise.then(function (outline) {
  678. expect(outline).toEqual(null);
  679. loadingTask.destroy().then(done);
  680. })["catch"](done.fail);
  681. });
  682. it('gets outline', function (done) {
  683. var promise = doc.getOutline();
  684. promise.then(function (outline) {
  685. expect(Array.isArray(outline)).toEqual(true);
  686. expect(outline.length).toEqual(2);
  687. var outlineItem = outline[1];
  688. expect(outlineItem.title).toEqual('Chapter 1');
  689. expect(Array.isArray(outlineItem.dest)).toEqual(true);
  690. expect(outlineItem.url).toEqual(null);
  691. expect(outlineItem.unsafeUrl).toBeUndefined();
  692. expect(outlineItem.newWindow).toBeUndefined();
  693. expect(outlineItem.bold).toEqual(true);
  694. expect(outlineItem.italic).toEqual(false);
  695. expect(outlineItem.color).toEqual(new Uint8ClampedArray([0, 64, 128]));
  696. expect(outlineItem.items.length).toEqual(1);
  697. expect(outlineItem.items[0].title).toEqual('Paragraph 1.1');
  698. done();
  699. })["catch"](done.fail);
  700. });
  701. it('gets outline containing a url', function (done) {
  702. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue3214.pdf'));
  703. loadingTask.promise.then(function (pdfDocument) {
  704. pdfDocument.getOutline().then(function (outline) {
  705. expect(Array.isArray(outline)).toEqual(true);
  706. expect(outline.length).toEqual(5);
  707. var outlineItemTwo = outline[2];
  708. expect(_typeof(outlineItemTwo.title)).toEqual('string');
  709. expect(outlineItemTwo.dest).toEqual(null);
  710. expect(outlineItemTwo.url).toEqual('http://google.com/');
  711. expect(outlineItemTwo.unsafeUrl).toEqual('http://google.com');
  712. expect(outlineItemTwo.newWindow).toBeUndefined();
  713. var outlineItemOne = outline[1];
  714. expect(outlineItemOne.bold).toEqual(false);
  715. expect(outlineItemOne.italic).toEqual(true);
  716. expect(outlineItemOne.color).toEqual(new Uint8ClampedArray([0, 0, 0]));
  717. loadingTask.destroy().then(done);
  718. });
  719. })["catch"](done.fail);
  720. });
  721. it('gets non-existent permissions', function (done) {
  722. doc.getPermissions().then(function (permissions) {
  723. expect(permissions).toEqual(null);
  724. done();
  725. })["catch"](done.fail);
  726. });
  727. it('gets permissions', function (done) {
  728. var loadingTask0 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue9972-1.pdf'));
  729. var promise0 = loadingTask0.promise.then(function (pdfDocument) {
  730. return pdfDocument.getPermissions();
  731. });
  732. var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue9972-2.pdf'));
  733. var promise1 = loadingTask1.promise.then(function (pdfDocument) {
  734. return pdfDocument.getPermissions();
  735. });
  736. var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue9972-3.pdf'));
  737. var promise2 = loadingTask2.promise.then(function (pdfDocument) {
  738. return pdfDocument.getPermissions();
  739. });
  740. var totalPermissionCount = Object.keys(_util.PermissionFlag).length;
  741. Promise.all([promise0, promise1, promise2]).then(function (permissions) {
  742. expect(permissions[0].length).toEqual(totalPermissionCount - 1);
  743. expect(permissions[0].includes(_util.PermissionFlag.MODIFY_CONTENTS)).toBeFalsy();
  744. expect(permissions[1].length).toEqual(totalPermissionCount - 2);
  745. expect(permissions[1].includes(_util.PermissionFlag.PRINT)).toBeFalsy();
  746. expect(permissions[1].includes(_util.PermissionFlag.PRINT_HIGH_QUALITY)).toBeFalsy();
  747. expect(permissions[2].length).toEqual(totalPermissionCount - 1);
  748. expect(permissions[2].includes(_util.PermissionFlag.COPY)).toBeFalsy();
  749. Promise.all([loadingTask0.destroy(), loadingTask1.destroy(), loadingTask2.destroy()]).then(done);
  750. })["catch"](done.fail);
  751. });
  752. it('gets metadata', function (done) {
  753. var promise = doc.getMetadata();
  754. promise.then(function (_ref) {
  755. var info = _ref.info,
  756. metadata = _ref.metadata,
  757. contentDispositionFilename = _ref.contentDispositionFilename;
  758. expect(info['Title']).toEqual('Basic API Test');
  759. expect(info['Custom']).toEqual(undefined);
  760. expect(info['PDFFormatVersion']).toEqual('1.7');
  761. expect(info['IsLinearized']).toEqual(false);
  762. expect(info['IsAcroFormPresent']).toEqual(false);
  763. expect(info['IsXFAPresent']).toEqual(false);
  764. expect(info['IsCollectionPresent']).toEqual(false);
  765. expect(metadata instanceof _metadata.Metadata).toEqual(true);
  766. expect(metadata.get('dc:title')).toEqual('Basic API Test');
  767. expect(contentDispositionFilename).toEqual(null);
  768. done();
  769. })["catch"](done.fail);
  770. });
  771. it('gets metadata, with custom info dict entries', function (done) {
  772. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  773. loadingTask.promise.then(function (pdfDocument) {
  774. return pdfDocument.getMetadata();
  775. }).then(function (_ref2) {
  776. var info = _ref2.info,
  777. metadata = _ref2.metadata,
  778. contentDispositionFilename = _ref2.contentDispositionFilename;
  779. expect(info['Creator']).toEqual('TeX');
  780. expect(info['Producer']).toEqual('pdfeTeX-1.21a');
  781. expect(info['CreationDate']).toEqual('D:20090401163925-07\'00\'');
  782. var custom = info['Custom'];
  783. expect(_typeof(custom) === 'object' && custom !== null).toEqual(true);
  784. expect(custom['PTEX.Fullbanner']).toEqual('This is pdfeTeX, ' + 'Version 3.141592-1.21a-2.2 (Web2C 7.5.4) kpathsea version 3.5.6');
  785. expect(info['PDFFormatVersion']).toEqual('1.4');
  786. expect(info['IsLinearized']).toEqual(false);
  787. expect(info['IsAcroFormPresent']).toEqual(false);
  788. expect(info['IsXFAPresent']).toEqual(false);
  789. expect(info['IsCollectionPresent']).toEqual(false);
  790. expect(metadata).toEqual(null);
  791. expect(contentDispositionFilename).toEqual(null);
  792. loadingTask.destroy().then(done);
  793. })["catch"](done.fail);
  794. });
  795. it('gets data', function (done) {
  796. var promise = doc.getData();
  797. promise.then(function (data) {
  798. expect(data instanceof Uint8Array).toEqual(true);
  799. expect(data.length).toEqual(basicApiFileLength);
  800. done();
  801. })["catch"](done.fail);
  802. });
  803. it('gets download info', function (done) {
  804. var promise = doc.getDownloadInfo();
  805. promise.then(function (data) {
  806. expect(data).toEqual({
  807. length: basicApiFileLength
  808. });
  809. done();
  810. })["catch"](done.fail);
  811. });
  812. it('gets document stats', function (done) {
  813. var promise = doc.getStats();
  814. promise.then(function (stats) {
  815. expect(stats).toEqual({
  816. streamTypes: [],
  817. fontTypes: []
  818. });
  819. done();
  820. })["catch"](done.fail);
  821. });
  822. it('checks that fingerprints are unique', function (done) {
  823. var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue4436r.pdf'));
  824. var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue4575.pdf'));
  825. var promises = [loadingTask1.promise, loadingTask2.promise];
  826. Promise.all(promises).then(function (data) {
  827. var fingerprint1 = data[0].fingerprint;
  828. expect(_typeof(fingerprint1)).toEqual('string');
  829. expect(fingerprint1.length > 0).toEqual(true);
  830. var fingerprint2 = data[1].fingerprint;
  831. expect(_typeof(fingerprint2)).toEqual('string');
  832. expect(fingerprint2.length > 0).toEqual(true);
  833. expect(fingerprint1).not.toEqual(fingerprint2);
  834. Promise.all([loadingTask1.destroy(), loadingTask2.destroy()]).then(done);
  835. })["catch"](done.fail);
  836. });
  837. describe('Cross-origin', function () {
  838. var loadingTask;
  839. function _checkCanLoad(expectSuccess, filename, options) {
  840. if ((0, _is_node["default"])()) {
  841. pending('Cannot simulate cross-origin requests in Node.js');
  842. }
  843. var params = (0, _test_utils.buildGetDocumentParams)(filename, options);
  844. var url = new URL(params.url);
  845. if (url.hostname === 'localhost') {
  846. url.hostname = '127.0.0.1';
  847. } else if (params.url.hostname === '127.0.0.1') {
  848. url.hostname = 'localhost';
  849. } else {
  850. pending('Can only run cross-origin test on localhost!');
  851. }
  852. params.url = url.href;
  853. loadingTask = (0, _api.getDocument)(params);
  854. return loadingTask.promise.then(function (pdf) {
  855. return pdf.destroy();
  856. }).then(function () {
  857. expect(expectSuccess).toEqual(true);
  858. }, function (error) {
  859. if (expectSuccess) {
  860. expect(error).toEqual('There should not be any error');
  861. }
  862. expect(expectSuccess).toEqual(false);
  863. });
  864. }
  865. function testCanLoad(filename, options) {
  866. return _checkCanLoad(true, filename, options);
  867. }
  868. function testCannotLoad(filename, options) {
  869. return _checkCanLoad(false, filename, options);
  870. }
  871. afterEach(function (done) {
  872. if (loadingTask && !loadingTask.destroyed) {
  873. loadingTask.destroy().then(done);
  874. } else {
  875. done();
  876. }
  877. });
  878. it('server disallows cors', function (done) {
  879. testCannotLoad('basicapi.pdf').then(done);
  880. });
  881. it('server allows cors without credentials, default withCredentials', function (done) {
  882. testCanLoad('basicapi.pdf?cors=withoutCredentials').then(done);
  883. });
  884. it('server allows cors without credentials, and withCredentials=false', function (done) {
  885. testCanLoad('basicapi.pdf?cors=withoutCredentials', {
  886. withCredentials: false
  887. }).then(done);
  888. });
  889. it('server allows cors without credentials, but withCredentials=true', function (done) {
  890. testCannotLoad('basicapi.pdf?cors=withoutCredentials', {
  891. withCredentials: true
  892. }).then(done);
  893. });
  894. it('server allows cors with credentials, and withCredentials=true', function (done) {
  895. testCanLoad('basicapi.pdf?cors=withCredentials', {
  896. withCredentials: true
  897. }).then(done);
  898. });
  899. it('server allows cors with credentials, and withCredentials=false', function (done) {
  900. testCanLoad('basicapi.pdf?cors=withCredentials', {
  901. withCredentials: false
  902. }).then(done);
  903. });
  904. });
  905. });
  906. describe('Page', function () {
  907. var loadingTask;
  908. var pdfDocument, page;
  909. beforeAll(function (done) {
  910. loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
  911. loadingTask.promise.then(function (doc) {
  912. pdfDocument = doc;
  913. pdfDocument.getPage(1).then(function (data) {
  914. page = data;
  915. done();
  916. });
  917. })["catch"](done.fail);
  918. });
  919. afterAll(function (done) {
  920. loadingTask.destroy().then(done);
  921. });
  922. it('gets page number', function () {
  923. expect(page.pageNumber).toEqual(1);
  924. });
  925. it('gets rotate', function () {
  926. expect(page.rotate).toEqual(0);
  927. });
  928. it('gets ref', function () {
  929. expect(page.ref).toEqual({
  930. num: 15,
  931. gen: 0
  932. });
  933. });
  934. it('gets userUnit', function () {
  935. expect(page.userUnit).toEqual(1.0);
  936. });
  937. it('gets view', function () {
  938. expect(page.view).toEqual([0, 0, 595.28, 841.89]);
  939. });
  940. it('gets viewport', function () {
  941. var viewport = page.getViewport({
  942. scale: 1.5,
  943. rotation: 90
  944. });
  945. expect(viewport.viewBox).toEqual(page.view);
  946. expect(viewport.scale).toEqual(1.5);
  947. expect(viewport.rotation).toEqual(90);
  948. expect(viewport.transform).toEqual([0, 1.5, 1.5, 0, 0, 0]);
  949. expect(viewport.width).toEqual(1262.835);
  950. expect(viewport.height).toEqual(892.92);
  951. });
  952. it('gets viewport respecting "dontFlip" argument', function () {
  953. var scale = 1;
  954. var rotation = 135;
  955. var viewport = page.getViewport({
  956. scale: scale,
  957. rotation: rotation
  958. });
  959. var dontFlipViewport = page.getViewport({
  960. scale: scale,
  961. rotation: rotation,
  962. dontFlip: true
  963. });
  964. expect(dontFlipViewport).not.toEqual(viewport);
  965. expect(dontFlipViewport).toEqual(viewport.clone({
  966. dontFlip: true
  967. }));
  968. expect(viewport.transform).toEqual([1, 0, 0, -1, 0, 841.89]);
  969. expect(dontFlipViewport.transform).toEqual([1, 0, -0, 1, 0, 0]);
  970. });
  971. it('gets annotations', function (done) {
  972. var defaultPromise = page.getAnnotations().then(function (data) {
  973. expect(data.length).toEqual(4);
  974. });
  975. var displayPromise = page.getAnnotations({
  976. intent: 'display'
  977. }).then(function (data) {
  978. expect(data.length).toEqual(4);
  979. });
  980. var printPromise = page.getAnnotations({
  981. intent: 'print'
  982. }).then(function (data) {
  983. expect(data.length).toEqual(4);
  984. });
  985. Promise.all([defaultPromise, displayPromise, printPromise]).then(function () {
  986. done();
  987. })["catch"](done.fail);
  988. });
  989. it('gets annotations containing relative URLs (bug 766086)', function (done) {
  990. var filename = 'bug766086.pdf';
  991. var defaultLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename));
  992. var defaultPromise = defaultLoadingTask.promise.then(function (pdfDoc) {
  993. return pdfDoc.getPage(1).then(function (pdfPage) {
  994. return pdfPage.getAnnotations();
  995. });
  996. });
  997. var docBaseUrlLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
  998. docBaseUrl: 'http://www.example.com/test/pdfs/qwerty.pdf'
  999. }));
  1000. var docBaseUrlPromise = docBaseUrlLoadingTask.promise.then(function (pdfDoc) {
  1001. return pdfDoc.getPage(1).then(function (pdfPage) {
  1002. return pdfPage.getAnnotations();
  1003. });
  1004. });
  1005. var invalidDocBaseUrlLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
  1006. docBaseUrl: 'qwerty.pdf'
  1007. }));
  1008. var invalidDocBaseUrlPromise = invalidDocBaseUrlLoadingTask.promise.then(function (pdfDoc) {
  1009. return pdfDoc.getPage(1).then(function (pdfPage) {
  1010. return pdfPage.getAnnotations();
  1011. });
  1012. });
  1013. Promise.all([defaultPromise, docBaseUrlPromise, invalidDocBaseUrlPromise]).then(function (data) {
  1014. var defaultAnnotations = data[0];
  1015. var docBaseUrlAnnotations = data[1];
  1016. var invalidDocBaseUrlAnnotations = data[2];
  1017. expect(defaultAnnotations[0].url).toBeUndefined();
  1018. expect(defaultAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
  1019. expect(docBaseUrlAnnotations[0].url).toEqual('http://www.example.com/0021/002156/215675E.pdf#15');
  1020. expect(docBaseUrlAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
  1021. expect(invalidDocBaseUrlAnnotations[0].url).toBeUndefined();
  1022. expect(invalidDocBaseUrlAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
  1023. Promise.all([defaultLoadingTask.destroy(), docBaseUrlLoadingTask.destroy(), invalidDocBaseUrlLoadingTask.destroy()]).then(done);
  1024. })["catch"](done.fail);
  1025. });
  1026. it('gets text content', function (done) {
  1027. var defaultPromise = page.getTextContent();
  1028. var parametersPromise = page.getTextContent({
  1029. normalizeWhitespace: true,
  1030. disableCombineTextItems: true
  1031. });
  1032. var promises = [defaultPromise, parametersPromise];
  1033. Promise.all(promises).then(function (data) {
  1034. expect(!!data[0].items).toEqual(true);
  1035. expect(data[0].items.length).toEqual(7);
  1036. expect(!!data[0].styles).toEqual(true);
  1037. expect(JSON.stringify(data[0])).toEqual(JSON.stringify(data[1]));
  1038. done();
  1039. })["catch"](done.fail);
  1040. });
  1041. it('gets text content, with correct properties (issue 8276)', function (done) {
  1042. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue8276_reduced.pdf'));
  1043. loadingTask.promise.then(function (pdfDoc) {
  1044. pdfDoc.getPage(1).then(function (pdfPage) {
  1045. pdfPage.getTextContent().then(function (_ref3) {
  1046. var items = _ref3.items,
  1047. styles = _ref3.styles;
  1048. expect(items.length).toEqual(1);
  1049. expect(Object.keys(styles)).toEqual(['Times']);
  1050. expect(items[0]).toEqual({
  1051. dir: 'ltr',
  1052. fontName: 'Times',
  1053. height: 18,
  1054. str: 'Issue 8276',
  1055. transform: [18, 0, 0, 18, 441.81, 708.4499999999999],
  1056. width: 77.49
  1057. });
  1058. expect(styles.Times).toEqual({
  1059. fontFamily: 'serif',
  1060. ascent: NaN,
  1061. descent: NaN,
  1062. vertical: false
  1063. });
  1064. loadingTask.destroy().then(done);
  1065. });
  1066. });
  1067. })["catch"](done.fail);
  1068. });
  1069. it('gets operator list', function (done) {
  1070. var promise = page.getOperatorList();
  1071. promise.then(function (oplist) {
  1072. expect(!!oplist.fnArray).toEqual(true);
  1073. expect(!!oplist.argsArray).toEqual(true);
  1074. expect(oplist.lastChunk).toEqual(true);
  1075. done();
  1076. })["catch"](done.fail);
  1077. });
  1078. it('gets operatorList with JPEG image (issue 4888)', function (done) {
  1079. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('cmykjpeg.pdf'));
  1080. loadingTask.promise.then(function (pdfDoc) {
  1081. pdfDoc.getPage(1).then(function (pdfPage) {
  1082. pdfPage.getOperatorList().then(function (opList) {
  1083. var imgIndex = opList.fnArray.indexOf(_util.OPS.paintImageXObject);
  1084. var imgArgs = opList.argsArray[imgIndex];
  1085. var _pdfPage$objs$get = pdfPage.objs.get(imgArgs[0]),
  1086. data = _pdfPage$objs$get.data;
  1087. expect(data instanceof Uint8ClampedArray).toEqual(true);
  1088. expect(data.length).toEqual(90000);
  1089. loadingTask.destroy().then(done);
  1090. });
  1091. });
  1092. })["catch"](done.fail);
  1093. });
  1094. it('gets document stats after parsing page', function (done) {
  1095. var promise = page.getOperatorList().then(function () {
  1096. return pdfDocument.getStats();
  1097. });
  1098. var expectedStreamTypes = [];
  1099. expectedStreamTypes[_util.StreamType.FLATE] = true;
  1100. var expectedFontTypes = [];
  1101. expectedFontTypes[_util.FontType.TYPE1] = true;
  1102. expectedFontTypes[_util.FontType.CIDFONTTYPE2] = true;
  1103. promise.then(function (stats) {
  1104. expect(stats).toEqual({
  1105. streamTypes: expectedStreamTypes,
  1106. fontTypes: expectedFontTypes
  1107. });
  1108. done();
  1109. })["catch"](done.fail);
  1110. });
  1111. it('gets page stats after parsing page, without `pdfBug` set', function (done) {
  1112. page.getOperatorList().then(function (opList) {
  1113. return page.stats;
  1114. }).then(function (stats) {
  1115. expect(stats).toEqual(null);
  1116. done();
  1117. }, done.fail);
  1118. });
  1119. it('gets page stats after parsing page, with `pdfBug` set', function (done) {
  1120. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, {
  1121. pdfBug: true
  1122. }));
  1123. loadingTask.promise.then(function (pdfDoc) {
  1124. return pdfDoc.getPage(1).then(function (pdfPage) {
  1125. return pdfPage.getOperatorList().then(function (opList) {
  1126. return pdfPage.stats;
  1127. });
  1128. });
  1129. }).then(function (stats) {
  1130. expect(stats instanceof _display_utils.StatTimer).toEqual(true);
  1131. expect(stats.times.length).toEqual(1);
  1132. var _stats$times = _slicedToArray(stats.times, 1),
  1133. statEntry = _stats$times[0];
  1134. expect(statEntry.name).toEqual('Page Request');
  1135. expect(statEntry.end - statEntry.start).toBeGreaterThan(0);
  1136. loadingTask.destroy().then(done);
  1137. }, done.fail);
  1138. });
  1139. it('gets page stats after rendering page, with `pdfBug` set', function (done) {
  1140. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, {
  1141. pdfBug: true
  1142. }));
  1143. var canvasAndCtx;
  1144. loadingTask.promise.then(function (pdfDoc) {
  1145. return pdfDoc.getPage(1).then(function (pdfPage) {
  1146. var viewport = pdfPage.getViewport({
  1147. scale: 1
  1148. });
  1149. canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  1150. var renderTask = pdfPage.render({
  1151. canvasContext: canvasAndCtx.context,
  1152. canvasFactory: CanvasFactory,
  1153. viewport: viewport
  1154. });
  1155. return renderTask.promise.then(function () {
  1156. return pdfPage.stats;
  1157. });
  1158. });
  1159. }).then(function (stats) {
  1160. expect(stats instanceof _display_utils.StatTimer).toEqual(true);
  1161. expect(stats.times.length).toEqual(3);
  1162. var _stats$times2 = _slicedToArray(stats.times, 3),
  1163. statEntryOne = _stats$times2[0],
  1164. statEntryTwo = _stats$times2[1],
  1165. statEntryThree = _stats$times2[2];
  1166. expect(statEntryOne.name).toEqual('Page Request');
  1167. expect(statEntryOne.end - statEntryOne.start).toBeGreaterThan(0);
  1168. expect(statEntryTwo.name).toEqual('Rendering');
  1169. expect(statEntryTwo.end - statEntryTwo.start).toBeGreaterThan(0);
  1170. expect(statEntryThree.name).toEqual('Overall');
  1171. expect(statEntryThree.end - statEntryThree.start).toBeGreaterThan(0);
  1172. CanvasFactory.destroy(canvasAndCtx);
  1173. loadingTask.destroy().then(done);
  1174. }, done.fail);
  1175. });
  1176. it('cancels rendering of page', function (done) {
  1177. var viewport = page.getViewport({
  1178. scale: 1
  1179. });
  1180. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  1181. var renderTask = page.render({
  1182. canvasContext: canvasAndCtx.context,
  1183. canvasFactory: CanvasFactory,
  1184. viewport: viewport
  1185. });
  1186. renderTask.cancel();
  1187. renderTask.promise.then(function () {
  1188. done.fail('shall cancel rendering');
  1189. })["catch"](function (error) {
  1190. expect(error instanceof _display_utils.RenderingCancelledException).toEqual(true);
  1191. expect(error.type).toEqual('canvas');
  1192. CanvasFactory.destroy(canvasAndCtx);
  1193. done();
  1194. });
  1195. });
  1196. it('re-render page, using the same canvas, after cancelling rendering', function (done) {
  1197. var viewport = page.getViewport({
  1198. scale: 1
  1199. });
  1200. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  1201. var renderTask = page.render({
  1202. canvasContext: canvasAndCtx.context,
  1203. canvasFactory: CanvasFactory,
  1204. viewport: viewport
  1205. });
  1206. renderTask.cancel();
  1207. renderTask.promise.then(function () {
  1208. throw new Error('shall cancel rendering');
  1209. }, function (reason) {
  1210. expect(reason instanceof _display_utils.RenderingCancelledException).toEqual(true);
  1211. }).then(function () {
  1212. var reRenderTask = page.render({
  1213. canvasContext: canvasAndCtx.context,
  1214. canvasFactory: CanvasFactory,
  1215. viewport: viewport
  1216. });
  1217. return reRenderTask.promise;
  1218. }).then(function () {
  1219. CanvasFactory.destroy(canvasAndCtx);
  1220. done();
  1221. }, done.fail);
  1222. });
  1223. it('multiple render() on the same canvas', function (done) {
  1224. var viewport = page.getViewport({
  1225. scale: 1
  1226. });
  1227. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  1228. var renderTask1 = page.render({
  1229. canvasContext: canvasAndCtx.context,
  1230. canvasFactory: CanvasFactory,
  1231. viewport: viewport
  1232. });
  1233. var renderTask2 = page.render({
  1234. canvasContext: canvasAndCtx.context,
  1235. canvasFactory: CanvasFactory,
  1236. viewport: viewport
  1237. });
  1238. Promise.all([renderTask1.promise, renderTask2.promise.then(function () {
  1239. done.fail('shall fail rendering');
  1240. }, function (reason) {
  1241. expect(/multiple render\(\)/.test(reason.message)).toEqual(true);
  1242. })]).then(done);
  1243. });
  1244. });
  1245. describe('Multiple `getDocument` instances', function () {
  1246. var pdf1 = (0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf');
  1247. var pdf2 = (0, _test_utils.buildGetDocumentParams)('TAMReview.pdf');
  1248. var pdf3 = (0, _test_utils.buildGetDocumentParams)('issue6068.pdf');
  1249. var loadingTasks = [];
  1250. function renderPDF(_x) {
  1251. return _renderPDF.apply(this, arguments);
  1252. }
  1253. function _renderPDF() {
  1254. _renderPDF = _asyncToGenerator(
  1255. /*#__PURE__*/
  1256. _regenerator["default"].mark(function _callee(filename) {
  1257. var loadingTask, pdf, page, viewport, canvasAndCtx, renderTask, data;
  1258. return _regenerator["default"].wrap(function _callee$(_context) {
  1259. while (1) {
  1260. switch (_context.prev = _context.next) {
  1261. case 0:
  1262. loadingTask = (0, _api.getDocument)(filename);
  1263. loadingTasks.push(loadingTask);
  1264. _context.next = 4;
  1265. return loadingTask.promise;
  1266. case 4:
  1267. pdf = _context.sent;
  1268. _context.next = 7;
  1269. return pdf.getPage(1);
  1270. case 7:
  1271. page = _context.sent;
  1272. viewport = page.getViewport({
  1273. scale: 1.2
  1274. });
  1275. canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  1276. renderTask = page.render({
  1277. canvasContext: canvasAndCtx.context,
  1278. canvasFactory: CanvasFactory,
  1279. viewport: viewport
  1280. });
  1281. _context.next = 13;
  1282. return renderTask.promise;
  1283. case 13:
  1284. data = canvasAndCtx.canvas.toDataURL();
  1285. CanvasFactory.destroy(canvasAndCtx);
  1286. return _context.abrupt("return", data);
  1287. case 16:
  1288. case "end":
  1289. return _context.stop();
  1290. }
  1291. }
  1292. }, _callee);
  1293. }));
  1294. return _renderPDF.apply(this, arguments);
  1295. }
  1296. afterEach(function (done) {
  1297. var destroyPromises = loadingTasks.map(function (loadingTask) {
  1298. return loadingTask.destroy();
  1299. });
  1300. Promise.all(destroyPromises).then(done);
  1301. });
  1302. it('should correctly render PDFs in parallel', function (done) {
  1303. var baseline1, baseline2, baseline3;
  1304. var promiseDone = renderPDF(pdf1).then(function (data1) {
  1305. baseline1 = data1;
  1306. return renderPDF(pdf2);
  1307. }).then(function (data2) {
  1308. baseline2 = data2;
  1309. return renderPDF(pdf3);
  1310. }).then(function (data3) {
  1311. baseline3 = data3;
  1312. return Promise.all([renderPDF(pdf1), renderPDF(pdf2), renderPDF(pdf3)]);
  1313. }).then(function (dataUrls) {
  1314. expect(dataUrls[0]).toEqual(baseline1);
  1315. expect(dataUrls[1]).toEqual(baseline2);
  1316. expect(dataUrls[2]).toEqual(baseline3);
  1317. return true;
  1318. });
  1319. promiseDone.then(function () {
  1320. done();
  1321. })["catch"](done.fail);
  1322. });
  1323. });
  1324. describe('PDFDataRangeTransport', function () {
  1325. var dataPromise;
  1326. beforeAll(function (done) {
  1327. var fileName = 'tracemonkey.pdf';
  1328. if ((0, _is_node["default"])()) {
  1329. dataPromise = _test_utils.NodeFileReaderFactory.fetch({
  1330. path: _test_utils.TEST_PDFS_PATH.node + fileName
  1331. });
  1332. } else {
  1333. dataPromise = _test_utils.DOMFileReaderFactory.fetch({
  1334. path: _test_utils.TEST_PDFS_PATH.dom + fileName
  1335. });
  1336. }
  1337. done();
  1338. });
  1339. afterAll(function () {
  1340. dataPromise = null;
  1341. });
  1342. it('should fetch document info and page using ranges', function (done) {
  1343. var initialDataLength = 4000;
  1344. var fetches = 0,
  1345. loadingTask;
  1346. dataPromise.then(function (data) {
  1347. var initialData = data.subarray(0, initialDataLength);
  1348. var transport = new _api.PDFDataRangeTransport(data.length, initialData);
  1349. transport.requestDataRange = function (begin, end) {
  1350. fetches++;
  1351. waitSome(function () {
  1352. transport.onDataProgress(4000);
  1353. transport.onDataRange(begin, data.subarray(begin, end));
  1354. });
  1355. };
  1356. loadingTask = (0, _api.getDocument)(transport);
  1357. return loadingTask.promise;
  1358. }).then(function (pdfDocument) {
  1359. expect(pdfDocument.numPages).toEqual(14);
  1360. return pdfDocument.getPage(10);
  1361. }).then(function (pdfPage) {
  1362. expect(pdfPage.rotate).toEqual(0);
  1363. expect(fetches).toBeGreaterThan(2);
  1364. loadingTask.destroy().then(done);
  1365. })["catch"](done.fail);
  1366. });
  1367. it('should fetch document info and page using range and streaming', function (done) {
  1368. var initialDataLength = 4000;
  1369. var fetches = 0,
  1370. loadingTask;
  1371. dataPromise.then(function (data) {
  1372. var initialData = data.subarray(0, initialDataLength);
  1373. var transport = new _api.PDFDataRangeTransport(data.length, initialData);
  1374. transport.requestDataRange = function (begin, end) {
  1375. fetches++;
  1376. if (fetches === 1) {
  1377. transport.onDataProgressiveRead(data.subarray(initialDataLength));
  1378. }
  1379. waitSome(function () {
  1380. transport.onDataRange(begin, data.subarray(begin, end));
  1381. });
  1382. };
  1383. loadingTask = (0, _api.getDocument)(transport);
  1384. return loadingTask.promise;
  1385. }).then(function (pdfDocument) {
  1386. expect(pdfDocument.numPages).toEqual(14);
  1387. return pdfDocument.getPage(10);
  1388. }).then(function (pdfPage) {
  1389. expect(pdfPage.rotate).toEqual(0);
  1390. expect(fetches).toEqual(1);
  1391. waitSome(function () {
  1392. loadingTask.destroy().then(done);
  1393. });
  1394. })["catch"](done.fail);
  1395. });
  1396. it('should fetch document info and page, without range, ' + 'using complete initialData', function (done) {
  1397. var fetches = 0,
  1398. loadingTask;
  1399. dataPromise.then(function (data) {
  1400. var transport = new _api.PDFDataRangeTransport(data.length, data, true);
  1401. transport.requestDataRange = function (begin, end) {
  1402. fetches++;
  1403. };
  1404. loadingTask = (0, _api.getDocument)({
  1405. disableRange: true,
  1406. range: transport
  1407. });
  1408. return loadingTask.promise;
  1409. }).then(function (pdfDocument) {
  1410. expect(pdfDocument.numPages).toEqual(14);
  1411. return pdfDocument.getPage(10);
  1412. }).then(function (pdfPage) {
  1413. expect(pdfPage.rotate).toEqual(0);
  1414. expect(fetches).toEqual(0);
  1415. loadingTask.destroy().then(done);
  1416. })["catch"](done.fail);
  1417. });
  1418. });
  1419. });