api_spec.js 66 KB

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