api_spec.js 65 KB

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