api_spec.js 67 KB

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