app.js 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFPrintServiceFactory = exports.DefaultExternalServices = exports.PDFViewerApplication = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. var _app_options = require("./app_options.js");
  29. var _pdf = require("../pdf");
  30. var _pdf_cursor_tools = require("./pdf_cursor_tools.js");
  31. var _pdf_rendering_queue = require("./pdf_rendering_queue.js");
  32. var _pdf_sidebar = require("./pdf_sidebar.js");
  33. var _overlay_manager = require("./overlay_manager.js");
  34. var _password_prompt = require("./password_prompt.js");
  35. var _pdf_attachment_viewer = require("./pdf_attachment_viewer.js");
  36. var _pdf_document_properties = require("./pdf_document_properties.js");
  37. var _pdf_find_bar = require("./pdf_find_bar.js");
  38. var _pdf_find_controller = require("./pdf_find_controller.js");
  39. var _pdf_history = require("./pdf_history.js");
  40. var _pdf_link_service = require("./pdf_link_service.js");
  41. var _pdf_outline_viewer = require("./pdf_outline_viewer.js");
  42. var _pdf_presentation_mode = require("./pdf_presentation_mode.js");
  43. var _pdf_sidebar_resizer = require("./pdf_sidebar_resizer.js");
  44. var _pdf_thumbnail_viewer = require("./pdf_thumbnail_viewer.js");
  45. var _pdf_viewer = require("./pdf_viewer.js");
  46. var _secondary_toolbar = require("./secondary_toolbar.js");
  47. var _toolbar = require("./toolbar.js");
  48. var _view_history = require("./view_history.js");
  49. const DEFAULT_SCALE_DELTA = 1.1;
  50. const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000;
  51. const FORCE_PAGES_LOADED_TIMEOUT = 10000;
  52. const WHEEL_ZOOM_DISABLED_TIMEOUT = 1000;
  53. const ENABLE_PERMISSIONS_CLASS = "enablePermissions";
  54. const ViewOnLoad = {
  55. UNKNOWN: -1,
  56. PREVIOUS: 0,
  57. INITIAL: 1
  58. };
  59. class DefaultExternalServices {
  60. constructor() {
  61. throw new Error("Cannot initialize DefaultExternalServices.");
  62. }
  63. static updateFindControlState(data) {}
  64. static updateFindMatchesCount(data) {}
  65. static initPassiveLoading(callbacks) {}
  66. static fallback(data, callback) {}
  67. static reportTelemetry(data) {}
  68. static createDownloadManager(options) {
  69. throw new Error("Not implemented: createDownloadManager");
  70. }
  71. static createPreferences() {
  72. throw new Error("Not implemented: createPreferences");
  73. }
  74. static createL10n(options) {
  75. throw new Error("Not implemented: createL10n");
  76. }
  77. static get supportsIntegratedFind() {
  78. return (0, _pdf.shadow)(this, "supportsIntegratedFind", false);
  79. }
  80. static get supportsDocumentFonts() {
  81. return (0, _pdf.shadow)(this, "supportsDocumentFonts", true);
  82. }
  83. static get supportedMouseWheelZoomModifierKeys() {
  84. return (0, _pdf.shadow)(this, "supportedMouseWheelZoomModifierKeys", {
  85. ctrlKey: true,
  86. metaKey: true
  87. });
  88. }
  89. static get isInAutomation() {
  90. return (0, _pdf.shadow)(this, "isInAutomation", false);
  91. }
  92. }
  93. exports.DefaultExternalServices = DefaultExternalServices;
  94. const PDFViewerApplication = {
  95. initialBookmark: document.location.hash.substring(1),
  96. _initializedCapability: (0, _pdf.createPromiseCapability)(),
  97. fellback: false,
  98. appConfig: null,
  99. pdfDocument: null,
  100. pdfLoadingTask: null,
  101. printService: null,
  102. pdfViewer: null,
  103. pdfThumbnailViewer: null,
  104. pdfRenderingQueue: null,
  105. pdfPresentationMode: null,
  106. pdfDocumentProperties: null,
  107. pdfLinkService: null,
  108. pdfHistory: null,
  109. pdfSidebar: null,
  110. pdfSidebarResizer: null,
  111. pdfOutlineViewer: null,
  112. pdfAttachmentViewer: null,
  113. pdfCursorTools: null,
  114. store: null,
  115. downloadManager: null,
  116. overlayManager: null,
  117. preferences: null,
  118. toolbar: null,
  119. secondaryToolbar: null,
  120. eventBus: null,
  121. l10n: null,
  122. isInitialViewSet: false,
  123. downloadComplete: false,
  124. isViewerEmbedded: window.parent !== window,
  125. url: "",
  126. baseUrl: "",
  127. externalServices: DefaultExternalServices,
  128. _boundEvents: {},
  129. contentDispositionFilename: null,
  130. async initialize(appConfig) {
  131. this.preferences = this.externalServices.createPreferences();
  132. this.appConfig = appConfig;
  133. await this._readPreferences();
  134. await this._parseHashParameters();
  135. await this._initializeL10n();
  136. if (this.isViewerEmbedded && _app_options.AppOptions.get("externalLinkTarget") === _pdf.LinkTarget.NONE) {
  137. _app_options.AppOptions.set("externalLinkTarget", _pdf.LinkTarget.TOP);
  138. }
  139. await this._initializeViewerComponents();
  140. this.bindEvents();
  141. this.bindWindowEvents();
  142. const appContainer = appConfig.appContainer || document.documentElement;
  143. this.l10n.translate(appContainer).then(() => {
  144. this.eventBus.dispatch("localized", {
  145. source: this
  146. });
  147. });
  148. this._initializedCapability.resolve();
  149. },
  150. async _readPreferences() {
  151. if (_app_options.AppOptions.get("disablePreferences")) {
  152. return;
  153. }
  154. try {
  155. const prefs = await this.preferences.getAll();
  156. for (const name in prefs) {
  157. _app_options.AppOptions.set(name, prefs[name]);
  158. }
  159. } catch (reason) {
  160. console.error(`_readPreferences: "${reason.message}".`);
  161. }
  162. },
  163. async _parseHashParameters() {
  164. if (!_app_options.AppOptions.get("pdfBugEnabled")) {
  165. return undefined;
  166. }
  167. const hash = document.location.hash.substring(1);
  168. if (!hash) {
  169. return undefined;
  170. }
  171. const hashParams = (0, _ui_utils.parseQueryString)(hash),
  172. waitOn = [];
  173. if ("disableworker" in hashParams && hashParams.disableworker === "true") {
  174. waitOn.push(loadFakeWorker());
  175. }
  176. if ("disablerange" in hashParams) {
  177. _app_options.AppOptions.set("disableRange", hashParams.disablerange === "true");
  178. }
  179. if ("disablestream" in hashParams) {
  180. _app_options.AppOptions.set("disableStream", hashParams.disablestream === "true");
  181. }
  182. if ("disableautofetch" in hashParams) {
  183. _app_options.AppOptions.set("disableAutoFetch", hashParams.disableautofetch === "true");
  184. }
  185. if ("disablefontface" in hashParams) {
  186. _app_options.AppOptions.set("disableFontFace", hashParams.disablefontface === "true");
  187. }
  188. if ("disablehistory" in hashParams) {
  189. _app_options.AppOptions.set("disableHistory", hashParams.disablehistory === "true");
  190. }
  191. if ("webgl" in hashParams) {
  192. _app_options.AppOptions.set("enableWebGL", hashParams.webgl === "true");
  193. }
  194. if ("verbosity" in hashParams) {
  195. _app_options.AppOptions.set("verbosity", hashParams.verbosity | 0);
  196. }
  197. if ("textlayer" in hashParams) {
  198. switch (hashParams.textlayer) {
  199. case "off":
  200. _app_options.AppOptions.set("textLayerMode", _ui_utils.TextLayerMode.DISABLE);
  201. break;
  202. case "visible":
  203. case "shadow":
  204. case "hover":
  205. const viewer = this.appConfig.viewerContainer;
  206. viewer.classList.add("textLayer-" + hashParams.textlayer);
  207. break;
  208. }
  209. }
  210. if ("pdfbug" in hashParams) {
  211. _app_options.AppOptions.set("pdfBug", true);
  212. _app_options.AppOptions.set("fontExtraProperties", true);
  213. const enabled = hashParams.pdfbug.split(",");
  214. waitOn.push(loadAndEnablePDFBug(enabled));
  215. }
  216. if ("locale" in hashParams) {
  217. _app_options.AppOptions.set("locale", hashParams.locale);
  218. }
  219. return Promise.all(waitOn).catch(reason => {
  220. console.error(`_parseHashParameters: "${reason.message}".`);
  221. });
  222. },
  223. async _initializeL10n() {
  224. this.l10n = this.externalServices.createL10n({
  225. locale: _app_options.AppOptions.get("locale")
  226. });
  227. const dir = await this.l10n.getDirection();
  228. document.getElementsByTagName("html")[0].dir = dir;
  229. },
  230. async _initializeViewerComponents() {
  231. const appConfig = this.appConfig;
  232. const eventBus = appConfig.eventBus || new _ui_utils.EventBus({
  233. isInAutomation: this.externalServices.isInAutomation
  234. });
  235. this.eventBus = eventBus;
  236. this.overlayManager = new _overlay_manager.OverlayManager();
  237. const pdfRenderingQueue = new _pdf_rendering_queue.PDFRenderingQueue();
  238. pdfRenderingQueue.onIdle = this.cleanup.bind(this);
  239. this.pdfRenderingQueue = pdfRenderingQueue;
  240. const pdfLinkService = new _pdf_link_service.PDFLinkService({
  241. eventBus,
  242. externalLinkTarget: _app_options.AppOptions.get("externalLinkTarget"),
  243. externalLinkRel: _app_options.AppOptions.get("externalLinkRel"),
  244. ignoreDestinationZoom: _app_options.AppOptions.get("ignoreDestinationZoom")
  245. });
  246. this.pdfLinkService = pdfLinkService;
  247. const downloadManager = this.externalServices.createDownloadManager({
  248. disableCreateObjectURL: _app_options.AppOptions.get("disableCreateObjectURL")
  249. });
  250. this.downloadManager = downloadManager;
  251. const findController = new _pdf_find_controller.PDFFindController({
  252. linkService: pdfLinkService,
  253. eventBus
  254. });
  255. this.findController = findController;
  256. const container = appConfig.mainContainer;
  257. const viewer = appConfig.viewerContainer;
  258. this.pdfViewer = new _pdf_viewer.PDFViewer({
  259. container,
  260. viewer,
  261. eventBus,
  262. renderingQueue: pdfRenderingQueue,
  263. linkService: pdfLinkService,
  264. downloadManager,
  265. findController,
  266. renderer: _app_options.AppOptions.get("renderer"),
  267. enableWebGL: _app_options.AppOptions.get("enableWebGL"),
  268. l10n: this.l10n,
  269. textLayerMode: _app_options.AppOptions.get("textLayerMode"),
  270. imageResourcesPath: _app_options.AppOptions.get("imageResourcesPath"),
  271. renderInteractiveForms: _app_options.AppOptions.get("renderInteractiveForms"),
  272. enablePrintAutoRotate: _app_options.AppOptions.get("enablePrintAutoRotate"),
  273. useOnlyCssZoom: _app_options.AppOptions.get("useOnlyCssZoom"),
  274. maxCanvasPixels: _app_options.AppOptions.get("maxCanvasPixels")
  275. });
  276. pdfRenderingQueue.setViewer(this.pdfViewer);
  277. pdfLinkService.setViewer(this.pdfViewer);
  278. this.pdfThumbnailViewer = new _pdf_thumbnail_viewer.PDFThumbnailViewer({
  279. container: appConfig.sidebar.thumbnailView,
  280. renderingQueue: pdfRenderingQueue,
  281. linkService: pdfLinkService,
  282. l10n: this.l10n
  283. });
  284. pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer);
  285. this.pdfHistory = new _pdf_history.PDFHistory({
  286. linkService: pdfLinkService,
  287. eventBus
  288. });
  289. pdfLinkService.setHistory(this.pdfHistory);
  290. if (!this.supportsIntegratedFind) {
  291. this.findBar = new _pdf_find_bar.PDFFindBar(appConfig.findBar, eventBus, this.l10n);
  292. }
  293. this.pdfDocumentProperties = new _pdf_document_properties.PDFDocumentProperties(appConfig.documentProperties, this.overlayManager, eventBus, this.l10n);
  294. this.pdfCursorTools = new _pdf_cursor_tools.PDFCursorTools({
  295. container,
  296. eventBus,
  297. cursorToolOnLoad: _app_options.AppOptions.get("cursorToolOnLoad")
  298. });
  299. this.toolbar = new _toolbar.Toolbar(appConfig.toolbar, eventBus, this.l10n);
  300. this.secondaryToolbar = new _secondary_toolbar.SecondaryToolbar(appConfig.secondaryToolbar, container, eventBus);
  301. if (this.supportsFullscreen) {
  302. this.pdfPresentationMode = new _pdf_presentation_mode.PDFPresentationMode({
  303. container,
  304. pdfViewer: this.pdfViewer,
  305. eventBus,
  306. contextMenuItems: appConfig.fullscreen
  307. });
  308. }
  309. this.passwordPrompt = new _password_prompt.PasswordPrompt(appConfig.passwordOverlay, this.overlayManager, this.l10n);
  310. this.pdfOutlineViewer = new _pdf_outline_viewer.PDFOutlineViewer({
  311. container: appConfig.sidebar.outlineView,
  312. eventBus,
  313. linkService: pdfLinkService
  314. });
  315. this.pdfAttachmentViewer = new _pdf_attachment_viewer.PDFAttachmentViewer({
  316. container: appConfig.sidebar.attachmentsView,
  317. eventBus,
  318. downloadManager
  319. });
  320. this.pdfSidebar = new _pdf_sidebar.PDFSidebar({
  321. elements: appConfig.sidebar,
  322. pdfViewer: this.pdfViewer,
  323. pdfThumbnailViewer: this.pdfThumbnailViewer,
  324. eventBus,
  325. l10n: this.l10n
  326. });
  327. this.pdfSidebar.onToggled = this.forceRendering.bind(this);
  328. this.pdfSidebarResizer = new _pdf_sidebar_resizer.PDFSidebarResizer(appConfig.sidebarResizer, eventBus, this.l10n);
  329. },
  330. run(config) {
  331. this.initialize(config).then(webViewerInitialized);
  332. },
  333. get initialized() {
  334. return this._initializedCapability.settled;
  335. },
  336. get initializedPromise() {
  337. return this._initializedCapability.promise;
  338. },
  339. zoomIn(ticks) {
  340. if (this.pdfViewer.isInPresentationMode) {
  341. return;
  342. }
  343. let newScale = this.pdfViewer.currentScale;
  344. do {
  345. newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
  346. newScale = Math.ceil(newScale * 10) / 10;
  347. newScale = Math.min(_ui_utils.MAX_SCALE, newScale);
  348. } while (--ticks > 0 && newScale < _ui_utils.MAX_SCALE);
  349. this.pdfViewer.currentScaleValue = newScale;
  350. },
  351. zoomOut(ticks) {
  352. if (this.pdfViewer.isInPresentationMode) {
  353. return;
  354. }
  355. let newScale = this.pdfViewer.currentScale;
  356. do {
  357. newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
  358. newScale = Math.floor(newScale * 10) / 10;
  359. newScale = Math.max(_ui_utils.MIN_SCALE, newScale);
  360. } while (--ticks > 0 && newScale > _ui_utils.MIN_SCALE);
  361. this.pdfViewer.currentScaleValue = newScale;
  362. },
  363. zoomReset() {
  364. if (this.pdfViewer.isInPresentationMode) {
  365. return;
  366. }
  367. this.pdfViewer.currentScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  368. },
  369. get pagesCount() {
  370. return this.pdfDocument ? this.pdfDocument.numPages : 0;
  371. },
  372. get page() {
  373. return this.pdfViewer.currentPageNumber;
  374. },
  375. set page(val) {
  376. this.pdfViewer.currentPageNumber = val;
  377. },
  378. get printing() {
  379. return !!this.printService;
  380. },
  381. get supportsPrinting() {
  382. return PDFPrintServiceFactory.instance.supportsPrinting;
  383. },
  384. get supportsFullscreen() {
  385. let support;
  386. const doc = document.documentElement;
  387. support = !!(doc.requestFullscreen || doc.mozRequestFullScreen || doc.webkitRequestFullScreen || doc.msRequestFullscreen);
  388. if (document.fullscreenEnabled === false || document.mozFullScreenEnabled === false || document.webkitFullscreenEnabled === false || document.msFullscreenEnabled === false) {
  389. support = false;
  390. }
  391. return (0, _pdf.shadow)(this, "supportsFullscreen", support);
  392. },
  393. get supportsIntegratedFind() {
  394. return this.externalServices.supportsIntegratedFind;
  395. },
  396. get supportsDocumentFonts() {
  397. return this.externalServices.supportsDocumentFonts;
  398. },
  399. get loadingBar() {
  400. const bar = new _ui_utils.ProgressBar("#loadingBar");
  401. return (0, _pdf.shadow)(this, "loadingBar", bar);
  402. },
  403. get supportedMouseWheelZoomModifierKeys() {
  404. return this.externalServices.supportedMouseWheelZoomModifierKeys;
  405. },
  406. initPassiveLoading() {
  407. throw new Error("Not implemented: initPassiveLoading");
  408. },
  409. setTitleUsingUrl(url = "") {
  410. this.url = url;
  411. this.baseUrl = url.split("#")[0];
  412. let title = (0, _ui_utils.getPDFFileNameFromURL)(url, "");
  413. if (!title) {
  414. try {
  415. title = decodeURIComponent((0, _pdf.getFilenameFromUrl)(url)) || url;
  416. } catch (ex) {
  417. title = url;
  418. }
  419. }
  420. this.setTitle(title);
  421. },
  422. setTitle(title) {
  423. if (this.isViewerEmbedded) {
  424. return;
  425. }
  426. document.title = title;
  427. },
  428. async close() {
  429. const errorWrapper = this.appConfig.errorWrapper.container;
  430. errorWrapper.setAttribute("hidden", "true");
  431. if (!this.pdfLoadingTask) {
  432. return undefined;
  433. }
  434. const promise = this.pdfLoadingTask.destroy();
  435. this.pdfLoadingTask = null;
  436. if (this.pdfDocument) {
  437. this.pdfDocument = null;
  438. this.pdfThumbnailViewer.setDocument(null);
  439. this.pdfViewer.setDocument(null);
  440. this.pdfLinkService.setDocument(null);
  441. this.pdfDocumentProperties.setDocument(null);
  442. }
  443. webViewerResetPermissions();
  444. this.store = null;
  445. this.isInitialViewSet = false;
  446. this.downloadComplete = false;
  447. this.url = "";
  448. this.baseUrl = "";
  449. this.contentDispositionFilename = null;
  450. this.pdfSidebar.reset();
  451. this.pdfOutlineViewer.reset();
  452. this.pdfAttachmentViewer.reset();
  453. if (this.pdfHistory) {
  454. this.pdfHistory.reset();
  455. }
  456. if (this.findBar) {
  457. this.findBar.reset();
  458. }
  459. this.toolbar.reset();
  460. this.secondaryToolbar.reset();
  461. if (typeof PDFBug !== "undefined") {
  462. PDFBug.cleanup();
  463. }
  464. return promise;
  465. },
  466. async open(file, args) {
  467. if (this.pdfLoadingTask) {
  468. await this.close();
  469. }
  470. const workerParameters = _app_options.AppOptions.getAll(_app_options.OptionKind.WORKER);
  471. for (const key in workerParameters) {
  472. _pdf.GlobalWorkerOptions[key] = workerParameters[key];
  473. }
  474. const parameters = Object.create(null);
  475. if (typeof file === "string") {
  476. this.setTitleUsingUrl(file);
  477. parameters.url = file;
  478. } else if (file && "byteLength" in file) {
  479. parameters.data = file;
  480. } else if (file.url && file.originalUrl) {
  481. this.setTitleUsingUrl(file.originalUrl);
  482. parameters.url = file.url;
  483. }
  484. const apiParameters = _app_options.AppOptions.getAll(_app_options.OptionKind.API);
  485. for (const key in apiParameters) {
  486. let value = apiParameters[key];
  487. if (key === "docBaseUrl" && !value) {}
  488. parameters[key] = value;
  489. }
  490. if (args) {
  491. for (const key in args) {
  492. const value = args[key];
  493. if (key === "length") {
  494. this.pdfDocumentProperties.setFileSize(value);
  495. }
  496. parameters[key] = value;
  497. }
  498. }
  499. const loadingTask = (0, _pdf.getDocument)(parameters);
  500. this.pdfLoadingTask = loadingTask;
  501. loadingTask.onPassword = (updateCallback, reason) => {
  502. this.pdfLinkService.externalLinkEnabled = false;
  503. this.passwordPrompt.setUpdateCallback(updateCallback, reason);
  504. this.passwordPrompt.open();
  505. };
  506. loadingTask.onProgress = ({
  507. loaded,
  508. total
  509. }) => {
  510. this.progress(loaded / total);
  511. };
  512. loadingTask.onUnsupportedFeature = this.fallback.bind(this);
  513. return loadingTask.promise.then(pdfDocument => {
  514. this.load(pdfDocument);
  515. }, exception => {
  516. if (loadingTask !== this.pdfLoadingTask) {
  517. return undefined;
  518. }
  519. const message = exception && exception.message;
  520. let loadingErrorMessage;
  521. if (exception instanceof _pdf.InvalidPDFException) {
  522. loadingErrorMessage = this.l10n.get("invalid_file_error", null, "Invalid or corrupted PDF file.");
  523. } else if (exception instanceof _pdf.MissingPDFException) {
  524. loadingErrorMessage = this.l10n.get("missing_file_error", null, "Missing PDF file.");
  525. } else if (exception instanceof _pdf.UnexpectedResponseException) {
  526. loadingErrorMessage = this.l10n.get("unexpected_response_error", null, "Unexpected server response.");
  527. } else {
  528. loadingErrorMessage = this.l10n.get("loading_error", null, "An error occurred while loading the PDF.");
  529. }
  530. return loadingErrorMessage.then(msg => {
  531. this.error(msg, {
  532. message
  533. });
  534. throw exception;
  535. });
  536. });
  537. },
  538. download() {
  539. function downloadByUrl() {
  540. downloadManager.downloadUrl(url, filename);
  541. }
  542. const url = this.baseUrl;
  543. const filename = this.contentDispositionFilename || (0, _ui_utils.getPDFFileNameFromURL)(this.url);
  544. const downloadManager = this.downloadManager;
  545. downloadManager.onerror = err => {
  546. this.error(`PDF failed to download: ${err}`);
  547. };
  548. if (!this.pdfDocument || !this.downloadComplete) {
  549. downloadByUrl();
  550. return;
  551. }
  552. this.pdfDocument.getData().then(function (data) {
  553. const blob = new Blob([data], {
  554. type: "application/pdf"
  555. });
  556. downloadManager.download(blob, url, filename);
  557. }).catch(downloadByUrl);
  558. },
  559. fallback(featureId) {
  560. if (this.fellback) {
  561. return;
  562. }
  563. this.fellback = true;
  564. this.externalServices.fallback({
  565. featureId,
  566. url: this.baseUrl
  567. }, function response(download) {
  568. if (!download) {
  569. return;
  570. }
  571. PDFViewerApplication.download();
  572. });
  573. },
  574. error(message, moreInfo) {
  575. const moreInfoText = [this.l10n.get("error_version_info", {
  576. version: _pdf.version || "?",
  577. build: _pdf.build || "?"
  578. }, "PDF.js v{{version}} (build: {{build}})")];
  579. if (moreInfo) {
  580. moreInfoText.push(this.l10n.get("error_message", {
  581. message: moreInfo.message
  582. }, "Message: {{message}}"));
  583. if (moreInfo.stack) {
  584. moreInfoText.push(this.l10n.get("error_stack", {
  585. stack: moreInfo.stack
  586. }, "Stack: {{stack}}"));
  587. } else {
  588. if (moreInfo.filename) {
  589. moreInfoText.push(this.l10n.get("error_file", {
  590. file: moreInfo.filename
  591. }, "File: {{file}}"));
  592. }
  593. if (moreInfo.lineNumber) {
  594. moreInfoText.push(this.l10n.get("error_line", {
  595. line: moreInfo.lineNumber
  596. }, "Line: {{line}}"));
  597. }
  598. }
  599. }
  600. const errorWrapperConfig = this.appConfig.errorWrapper;
  601. const errorWrapper = errorWrapperConfig.container;
  602. errorWrapper.removeAttribute("hidden");
  603. const errorMessage = errorWrapperConfig.errorMessage;
  604. errorMessage.textContent = message;
  605. const closeButton = errorWrapperConfig.closeButton;
  606. closeButton.onclick = function () {
  607. errorWrapper.setAttribute("hidden", "true");
  608. };
  609. const errorMoreInfo = errorWrapperConfig.errorMoreInfo;
  610. const moreInfoButton = errorWrapperConfig.moreInfoButton;
  611. const lessInfoButton = errorWrapperConfig.lessInfoButton;
  612. moreInfoButton.onclick = function () {
  613. errorMoreInfo.removeAttribute("hidden");
  614. moreInfoButton.setAttribute("hidden", "true");
  615. lessInfoButton.removeAttribute("hidden");
  616. errorMoreInfo.style.height = errorMoreInfo.scrollHeight + "px";
  617. };
  618. lessInfoButton.onclick = function () {
  619. errorMoreInfo.setAttribute("hidden", "true");
  620. moreInfoButton.removeAttribute("hidden");
  621. lessInfoButton.setAttribute("hidden", "true");
  622. };
  623. moreInfoButton.oncontextmenu = _ui_utils.noContextMenuHandler;
  624. lessInfoButton.oncontextmenu = _ui_utils.noContextMenuHandler;
  625. closeButton.oncontextmenu = _ui_utils.noContextMenuHandler;
  626. moreInfoButton.removeAttribute("hidden");
  627. lessInfoButton.setAttribute("hidden", "true");
  628. Promise.all(moreInfoText).then(parts => {
  629. errorMoreInfo.value = parts.join("\n");
  630. });
  631. },
  632. progress(level) {
  633. if (this.downloadComplete) {
  634. return;
  635. }
  636. const percent = Math.round(level * 100);
  637. if (percent > this.loadingBar.percent || isNaN(percent)) {
  638. this.loadingBar.percent = percent;
  639. const disableAutoFetch = this.pdfDocument ? this.pdfDocument.loadingParams.disableAutoFetch : _app_options.AppOptions.get("disableAutoFetch");
  640. if (disableAutoFetch && percent) {
  641. if (this.disableAutoFetchLoadingBarTimeout) {
  642. clearTimeout(this.disableAutoFetchLoadingBarTimeout);
  643. this.disableAutoFetchLoadingBarTimeout = null;
  644. }
  645. this.loadingBar.show();
  646. this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
  647. this.loadingBar.hide();
  648. this.disableAutoFetchLoadingBarTimeout = null;
  649. }, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
  650. }
  651. }
  652. },
  653. load(pdfDocument) {
  654. this.pdfDocument = pdfDocument;
  655. pdfDocument.getDownloadInfo().then(() => {
  656. this.downloadComplete = true;
  657. this.loadingBar.hide();
  658. firstPagePromise.then(() => {
  659. this.eventBus.dispatch("documentloaded", {
  660. source: this
  661. });
  662. });
  663. });
  664. const pageLayoutPromise = pdfDocument.getPageLayout().catch(function () {});
  665. const pageModePromise = pdfDocument.getPageMode().catch(function () {});
  666. const openActionPromise = pdfDocument.getOpenAction().catch(function () {});
  667. this.toolbar.setPagesCount(pdfDocument.numPages, false);
  668. this.secondaryToolbar.setPagesCount(pdfDocument.numPages);
  669. let baseDocumentUrl;
  670. baseDocumentUrl = null;
  671. this.pdfLinkService.setDocument(pdfDocument, baseDocumentUrl);
  672. this.pdfDocumentProperties.setDocument(pdfDocument, this.url);
  673. const pdfViewer = this.pdfViewer;
  674. pdfViewer.setDocument(pdfDocument);
  675. const {
  676. firstPagePromise,
  677. onePageRendered,
  678. pagesPromise
  679. } = pdfViewer;
  680. const pdfThumbnailViewer = this.pdfThumbnailViewer;
  681. pdfThumbnailViewer.setDocument(pdfDocument);
  682. const storedPromise = (this.store = new _view_history.ViewHistory(pdfDocument.fingerprint)).getMultiple({
  683. page: null,
  684. zoom: _ui_utils.DEFAULT_SCALE_VALUE,
  685. scrollLeft: "0",
  686. scrollTop: "0",
  687. rotation: null,
  688. sidebarView: _pdf_sidebar.SidebarView.UNKNOWN,
  689. scrollMode: _ui_utils.ScrollMode.UNKNOWN,
  690. spreadMode: _ui_utils.SpreadMode.UNKNOWN
  691. }).catch(() => {
  692. return Object.create(null);
  693. });
  694. firstPagePromise.then(pdfPage => {
  695. this.loadingBar.setWidth(this.appConfig.viewerContainer);
  696. Promise.all([_ui_utils.animationStarted, storedPromise, pageLayoutPromise, pageModePromise, openActionPromise]).then(async ([timeStamp, stored, pageLayout, pageMode, openAction]) => {
  697. const viewOnLoad = _app_options.AppOptions.get("viewOnLoad");
  698. this._initializePdfHistory({
  699. fingerprint: pdfDocument.fingerprint,
  700. viewOnLoad,
  701. initialDest: openAction && openAction.dest
  702. });
  703. const initialBookmark = this.initialBookmark;
  704. const zoom = _app_options.AppOptions.get("defaultZoomValue");
  705. let hash = zoom ? `zoom=${zoom}` : null;
  706. let rotation = null;
  707. let sidebarView = _app_options.AppOptions.get("sidebarViewOnLoad");
  708. let scrollMode = _app_options.AppOptions.get("scrollModeOnLoad");
  709. let spreadMode = _app_options.AppOptions.get("spreadModeOnLoad");
  710. if (stored.page && viewOnLoad !== ViewOnLoad.INITIAL) {
  711. hash = `page=${stored.page}&zoom=${zoom || stored.zoom},` + `${stored.scrollLeft},${stored.scrollTop}`;
  712. rotation = parseInt(stored.rotation, 10);
  713. if (sidebarView === _pdf_sidebar.SidebarView.UNKNOWN) {
  714. sidebarView = stored.sidebarView | 0;
  715. }
  716. if (scrollMode === _ui_utils.ScrollMode.UNKNOWN) {
  717. scrollMode = stored.scrollMode | 0;
  718. }
  719. if (spreadMode === _ui_utils.SpreadMode.UNKNOWN) {
  720. spreadMode = stored.spreadMode | 0;
  721. }
  722. }
  723. if (pageMode && sidebarView === _pdf_sidebar.SidebarView.UNKNOWN) {
  724. sidebarView = apiPageModeToSidebarView(pageMode);
  725. }
  726. if (pageLayout && spreadMode === _ui_utils.SpreadMode.UNKNOWN) {
  727. spreadMode = apiPageLayoutToSpreadMode(pageLayout);
  728. }
  729. this.setInitialView(hash, {
  730. rotation,
  731. sidebarView,
  732. scrollMode,
  733. spreadMode
  734. });
  735. this.eventBus.dispatch("documentinit", {
  736. source: this
  737. });
  738. if (!this.isViewerEmbedded) {
  739. pdfViewer.focus();
  740. }
  741. this._initializePermissions(pdfDocument);
  742. await Promise.race([pagesPromise, new Promise(resolve => {
  743. setTimeout(resolve, FORCE_PAGES_LOADED_TIMEOUT);
  744. })]);
  745. if (!initialBookmark && !hash) {
  746. return;
  747. }
  748. if (pdfViewer.hasEqualPageSizes) {
  749. return;
  750. }
  751. this.initialBookmark = initialBookmark;
  752. pdfViewer.currentScaleValue = pdfViewer.currentScaleValue;
  753. this.setInitialView(hash);
  754. }).catch(() => {
  755. this.setInitialView();
  756. }).then(function () {
  757. pdfViewer.update();
  758. });
  759. });
  760. pagesPromise.then(() => {
  761. this._initializeAutoPrint(pdfDocument, openActionPromise);
  762. });
  763. onePageRendered.then(() => {
  764. pdfDocument.getOutline().then(outline => {
  765. this.pdfOutlineViewer.render({
  766. outline
  767. });
  768. });
  769. pdfDocument.getAttachments().then(attachments => {
  770. this.pdfAttachmentViewer.render({
  771. attachments
  772. });
  773. });
  774. });
  775. this._initializePageLabels(pdfDocument);
  776. this._initializeMetadata(pdfDocument);
  777. },
  778. async _initializeAutoPrint(pdfDocument, openActionPromise) {
  779. const [openAction, javaScript] = await Promise.all([openActionPromise, pdfDocument.getJavaScript()]);
  780. if (pdfDocument !== this.pdfDocument) {
  781. return;
  782. }
  783. let triggerAutoPrint = false;
  784. if (openAction && openAction.action === "Print") {
  785. triggerAutoPrint = true;
  786. }
  787. if (javaScript) {
  788. javaScript.some(js => {
  789. if (!js) {
  790. return false;
  791. }
  792. console.warn("Warning: JavaScript is not supported");
  793. this.fallback(_pdf.UNSUPPORTED_FEATURES.javaScript);
  794. return true;
  795. });
  796. if (!triggerAutoPrint) {
  797. for (const js of javaScript) {
  798. if (js && _ui_utils.AutoPrintRegExp.test(js)) {
  799. triggerAutoPrint = true;
  800. break;
  801. }
  802. }
  803. }
  804. }
  805. if (!this.supportsPrinting) {
  806. return;
  807. }
  808. if (triggerAutoPrint) {
  809. setTimeout(function () {
  810. window.print();
  811. });
  812. }
  813. },
  814. async _initializeMetadata(pdfDocument) {
  815. const {
  816. info,
  817. metadata,
  818. contentDispositionFilename
  819. } = await pdfDocument.getMetadata();
  820. if (pdfDocument !== this.pdfDocument) {
  821. return;
  822. }
  823. this.documentInfo = info;
  824. this.metadata = metadata;
  825. this.contentDispositionFilename = contentDispositionFilename;
  826. console.log(`PDF ${pdfDocument.fingerprint} [${info.PDFFormatVersion} ` + `${(info.Producer || "-").trim()} / ${(info.Creator || "-").trim()}] ` + `(PDF.js: ${_pdf.version || "-"}` + `${this.pdfViewer.enableWebGL ? " [WebGL]" : ""})`);
  827. let pdfTitle;
  828. const infoTitle = info && info.Title;
  829. if (infoTitle) {
  830. pdfTitle = infoTitle;
  831. }
  832. const metadataTitle = metadata && metadata.get("dc:title");
  833. if (metadataTitle) {
  834. if (metadataTitle !== "Untitled" && !/[\uFFF0-\uFFFF]/g.test(metadataTitle)) {
  835. pdfTitle = metadataTitle;
  836. }
  837. }
  838. if (pdfTitle) {
  839. this.setTitle(`${pdfTitle} - ${contentDispositionFilename || document.title}`);
  840. } else if (contentDispositionFilename) {
  841. this.setTitle(contentDispositionFilename);
  842. }
  843. if (info.IsAcroFormPresent) {
  844. console.warn("Warning: AcroForm/XFA is not supported");
  845. this.fallback(_pdf.UNSUPPORTED_FEATURES.forms);
  846. }
  847. let versionId = "other";
  848. const KNOWN_VERSIONS = ["1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "2.0", "2.1", "2.2", "2.3"];
  849. if (KNOWN_VERSIONS.includes(info.PDFFormatVersion)) {
  850. versionId = `v${info.PDFFormatVersion.replace(".", "_")}`;
  851. }
  852. let generatorId = "other";
  853. const KNOWN_GENERATORS = ["acrobat distiller", "acrobat pdfwriter", "adobe livecycle", "adobe pdf library", "adobe photoshop", "ghostscript", "tcpdf", "cairo", "dvipdfm", "dvips", "pdftex", "pdfkit", "itext", "prince", "quarkxpress", "mac os x", "microsoft", "openoffice", "oracle", "luradocument", "pdf-xchange", "antenna house", "aspose.cells", "fpdf"];
  854. if (info.Producer) {
  855. const producer = info.Producer.toLowerCase();
  856. KNOWN_GENERATORS.some(function (generator) {
  857. if (!producer.includes(generator)) {
  858. return false;
  859. }
  860. generatorId = generator.replace(/[ .\-]/g, "_");
  861. return true;
  862. });
  863. }
  864. let formType = null;
  865. if (info.IsAcroFormPresent) {
  866. formType = info.IsXFAPresent ? "xfa" : "acroform";
  867. }
  868. this.externalServices.reportTelemetry({
  869. type: "documentInfo",
  870. version: versionId,
  871. generator: generatorId,
  872. formType
  873. });
  874. },
  875. async _initializePageLabels(pdfDocument) {
  876. const labels = await pdfDocument.getPageLabels();
  877. if (pdfDocument !== this.pdfDocument) {
  878. return;
  879. }
  880. if (!labels || _app_options.AppOptions.get("disablePageLabels")) {
  881. return;
  882. }
  883. const numLabels = labels.length;
  884. if (numLabels !== this.pagesCount) {
  885. console.error("The number of Page Labels does not match the number of pages in the document.");
  886. return;
  887. }
  888. let i = 0;
  889. while (i < numLabels && labels[i] === (i + 1).toString()) {
  890. i++;
  891. }
  892. if (i === numLabels) {
  893. return;
  894. }
  895. const {
  896. pdfViewer,
  897. pdfThumbnailViewer,
  898. toolbar
  899. } = this;
  900. pdfViewer.setPageLabels(labels);
  901. pdfThumbnailViewer.setPageLabels(labels);
  902. toolbar.setPagesCount(numLabels, true);
  903. toolbar.setPageNumber(pdfViewer.currentPageNumber, pdfViewer.currentPageLabel);
  904. },
  905. _initializePdfHistory({
  906. fingerprint,
  907. viewOnLoad,
  908. initialDest = null
  909. }) {
  910. if (this.isViewerEmbedded || _app_options.AppOptions.get("disableHistory")) {
  911. return;
  912. }
  913. this.pdfHistory.initialize({
  914. fingerprint,
  915. resetHistory: viewOnLoad === ViewOnLoad.INITIAL,
  916. updateUrl: _app_options.AppOptions.get("historyUpdateUrl")
  917. });
  918. if (this.pdfHistory.initialBookmark) {
  919. this.initialBookmark = this.pdfHistory.initialBookmark;
  920. this.initialRotation = this.pdfHistory.initialRotation;
  921. }
  922. if (initialDest && !this.initialBookmark && viewOnLoad === ViewOnLoad.UNKNOWN) {
  923. this.initialBookmark = JSON.stringify(initialDest);
  924. this.pdfHistory.push({
  925. explicitDest: initialDest,
  926. pageNumber: null
  927. });
  928. }
  929. },
  930. async _initializePermissions(pdfDocument) {
  931. const permissions = await pdfDocument.getPermissions();
  932. if (pdfDocument !== this.pdfDocument) {
  933. return;
  934. }
  935. if (!permissions || !_app_options.AppOptions.get("enablePermissions")) {
  936. return;
  937. }
  938. if (!permissions.includes(_pdf.PermissionFlag.COPY)) {
  939. this.appConfig.viewerContainer.classList.add(ENABLE_PERMISSIONS_CLASS);
  940. }
  941. },
  942. setInitialView(storedHash, {
  943. rotation,
  944. sidebarView,
  945. scrollMode,
  946. spreadMode
  947. } = {}) {
  948. const setRotation = angle => {
  949. if ((0, _ui_utils.isValidRotation)(angle)) {
  950. this.pdfViewer.pagesRotation = angle;
  951. }
  952. };
  953. const setViewerModes = (scroll, spread) => {
  954. if ((0, _ui_utils.isValidScrollMode)(scroll)) {
  955. this.pdfViewer.scrollMode = scroll;
  956. }
  957. if ((0, _ui_utils.isValidSpreadMode)(spread)) {
  958. this.pdfViewer.spreadMode = spread;
  959. }
  960. };
  961. this.isInitialViewSet = true;
  962. this.pdfSidebar.setInitialView(sidebarView);
  963. setViewerModes(scrollMode, spreadMode);
  964. if (this.initialBookmark) {
  965. setRotation(this.initialRotation);
  966. delete this.initialRotation;
  967. this.pdfLinkService.setHash(this.initialBookmark);
  968. this.initialBookmark = null;
  969. } else if (storedHash) {
  970. setRotation(rotation);
  971. this.pdfLinkService.setHash(storedHash);
  972. }
  973. this.toolbar.setPageNumber(this.pdfViewer.currentPageNumber, this.pdfViewer.currentPageLabel);
  974. this.secondaryToolbar.setPageNumber(this.pdfViewer.currentPageNumber);
  975. if (!this.pdfViewer.currentScaleValue) {
  976. this.pdfViewer.currentScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  977. }
  978. },
  979. cleanup() {
  980. if (!this.pdfDocument) {
  981. return;
  982. }
  983. this.pdfViewer.cleanup();
  984. this.pdfThumbnailViewer.cleanup();
  985. if (this.pdfViewer.renderer !== _ui_utils.RendererType.SVG) {
  986. this.pdfDocument.cleanup();
  987. }
  988. },
  989. forceRendering() {
  990. this.pdfRenderingQueue.printing = this.printing;
  991. this.pdfRenderingQueue.isThumbnailViewEnabled = this.pdfSidebar.isThumbnailViewVisible;
  992. this.pdfRenderingQueue.renderHighestPriority();
  993. },
  994. beforePrint() {
  995. if (this.printService) {
  996. return;
  997. }
  998. if (!this.supportsPrinting) {
  999. this.l10n.get("printing_not_supported", null, "Warning: Printing is not fully supported by this browser.").then(printMessage => {
  1000. this.error(printMessage);
  1001. });
  1002. return;
  1003. }
  1004. if (!this.pdfViewer.pageViewsReady) {
  1005. this.l10n.get("printing_not_ready", null, "Warning: The PDF is not fully loaded for printing.").then(notReadyMessage => {
  1006. window.alert(notReadyMessage);
  1007. });
  1008. return;
  1009. }
  1010. const pagesOverview = this.pdfViewer.getPagesOverview();
  1011. const printContainer = this.appConfig.printContainer;
  1012. const printService = PDFPrintServiceFactory.instance.createPrintService(this.pdfDocument, pagesOverview, printContainer, this.l10n);
  1013. this.printService = printService;
  1014. this.forceRendering();
  1015. printService.layout();
  1016. this.externalServices.reportTelemetry({
  1017. type: "print"
  1018. });
  1019. },
  1020. afterPrint() {
  1021. if (this.printService) {
  1022. this.printService.destroy();
  1023. this.printService = null;
  1024. }
  1025. this.forceRendering();
  1026. },
  1027. rotatePages(delta) {
  1028. if (!this.pdfDocument) {
  1029. return;
  1030. }
  1031. const newRotation = (this.pdfViewer.pagesRotation + 360 + delta) % 360;
  1032. this.pdfViewer.pagesRotation = newRotation;
  1033. },
  1034. requestPresentationMode() {
  1035. if (!this.pdfPresentationMode) {
  1036. return;
  1037. }
  1038. this.pdfPresentationMode.request();
  1039. },
  1040. bindEvents() {
  1041. const {
  1042. eventBus,
  1043. _boundEvents
  1044. } = this;
  1045. _boundEvents.beforePrint = this.beforePrint.bind(this);
  1046. _boundEvents.afterPrint = this.afterPrint.bind(this);
  1047. eventBus._on("resize", webViewerResize);
  1048. eventBus._on("hashchange", webViewerHashchange);
  1049. eventBus._on("beforeprint", _boundEvents.beforePrint);
  1050. eventBus._on("afterprint", _boundEvents.afterPrint);
  1051. eventBus._on("pagerendered", webViewerPageRendered);
  1052. eventBus._on("updateviewarea", webViewerUpdateViewarea);
  1053. eventBus._on("pagechanging", webViewerPageChanging);
  1054. eventBus._on("scalechanging", webViewerScaleChanging);
  1055. eventBus._on("rotationchanging", webViewerRotationChanging);
  1056. eventBus._on("sidebarviewchanged", webViewerSidebarViewChanged);
  1057. eventBus._on("pagemode", webViewerPageMode);
  1058. eventBus._on("namedaction", webViewerNamedAction);
  1059. eventBus._on("presentationmodechanged", webViewerPresentationModeChanged);
  1060. eventBus._on("presentationmode", webViewerPresentationMode);
  1061. eventBus._on("print", webViewerPrint);
  1062. eventBus._on("download", webViewerDownload);
  1063. eventBus._on("firstpage", webViewerFirstPage);
  1064. eventBus._on("lastpage", webViewerLastPage);
  1065. eventBus._on("nextpage", webViewerNextPage);
  1066. eventBus._on("previouspage", webViewerPreviousPage);
  1067. eventBus._on("zoomin", webViewerZoomIn);
  1068. eventBus._on("zoomout", webViewerZoomOut);
  1069. eventBus._on("zoomreset", webViewerZoomReset);
  1070. eventBus._on("pagenumberchanged", webViewerPageNumberChanged);
  1071. eventBus._on("scalechanged", webViewerScaleChanged);
  1072. eventBus._on("rotatecw", webViewerRotateCw);
  1073. eventBus._on("rotateccw", webViewerRotateCcw);
  1074. eventBus._on("switchscrollmode", webViewerSwitchScrollMode);
  1075. eventBus._on("scrollmodechanged", webViewerScrollModeChanged);
  1076. eventBus._on("switchspreadmode", webViewerSwitchSpreadMode);
  1077. eventBus._on("spreadmodechanged", webViewerSpreadModeChanged);
  1078. eventBus._on("documentproperties", webViewerDocumentProperties);
  1079. eventBus._on("find", webViewerFind);
  1080. eventBus._on("findfromurlhash", webViewerFindFromUrlHash);
  1081. eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount);
  1082. eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState);
  1083. eventBus._on("fileinputchange", webViewerFileInputChange);
  1084. eventBus._on("openfile", webViewerOpenFile);
  1085. },
  1086. bindWindowEvents() {
  1087. const {
  1088. eventBus,
  1089. _boundEvents
  1090. } = this;
  1091. _boundEvents.windowResize = () => {
  1092. eventBus.dispatch("resize", {
  1093. source: window
  1094. });
  1095. };
  1096. _boundEvents.windowHashChange = () => {
  1097. eventBus.dispatch("hashchange", {
  1098. source: window,
  1099. hash: document.location.hash.substring(1)
  1100. });
  1101. };
  1102. _boundEvents.windowBeforePrint = () => {
  1103. eventBus.dispatch("beforeprint", {
  1104. source: window
  1105. });
  1106. };
  1107. _boundEvents.windowAfterPrint = () => {
  1108. eventBus.dispatch("afterprint", {
  1109. source: window
  1110. });
  1111. };
  1112. window.addEventListener("visibilitychange", webViewerVisibilityChange);
  1113. window.addEventListener("wheel", webViewerWheel, {
  1114. passive: false
  1115. });
  1116. window.addEventListener("click", webViewerClick);
  1117. window.addEventListener("keydown", webViewerKeyDown);
  1118. window.addEventListener("resize", _boundEvents.windowResize);
  1119. window.addEventListener("hashchange", _boundEvents.windowHashChange);
  1120. window.addEventListener("beforeprint", _boundEvents.windowBeforePrint);
  1121. window.addEventListener("afterprint", _boundEvents.windowAfterPrint);
  1122. },
  1123. unbindEvents() {
  1124. const {
  1125. eventBus,
  1126. _boundEvents
  1127. } = this;
  1128. eventBus._off("resize", webViewerResize);
  1129. eventBus._off("hashchange", webViewerHashchange);
  1130. eventBus._off("beforeprint", _boundEvents.beforePrint);
  1131. eventBus._off("afterprint", _boundEvents.afterPrint);
  1132. eventBus._off("pagerendered", webViewerPageRendered);
  1133. eventBus._off("updateviewarea", webViewerUpdateViewarea);
  1134. eventBus._off("pagechanging", webViewerPageChanging);
  1135. eventBus._off("scalechanging", webViewerScaleChanging);
  1136. eventBus._off("rotationchanging", webViewerRotationChanging);
  1137. eventBus._off("sidebarviewchanged", webViewerSidebarViewChanged);
  1138. eventBus._off("pagemode", webViewerPageMode);
  1139. eventBus._off("namedaction", webViewerNamedAction);
  1140. eventBus._off("presentationmodechanged", webViewerPresentationModeChanged);
  1141. eventBus._off("presentationmode", webViewerPresentationMode);
  1142. eventBus._off("print", webViewerPrint);
  1143. eventBus._off("download", webViewerDownload);
  1144. eventBus._off("firstpage", webViewerFirstPage);
  1145. eventBus._off("lastpage", webViewerLastPage);
  1146. eventBus._off("nextpage", webViewerNextPage);
  1147. eventBus._off("previouspage", webViewerPreviousPage);
  1148. eventBus._off("zoomin", webViewerZoomIn);
  1149. eventBus._off("zoomout", webViewerZoomOut);
  1150. eventBus._off("zoomreset", webViewerZoomReset);
  1151. eventBus._off("pagenumberchanged", webViewerPageNumberChanged);
  1152. eventBus._off("scalechanged", webViewerScaleChanged);
  1153. eventBus._off("rotatecw", webViewerRotateCw);
  1154. eventBus._off("rotateccw", webViewerRotateCcw);
  1155. eventBus._off("switchscrollmode", webViewerSwitchScrollMode);
  1156. eventBus._off("scrollmodechanged", webViewerScrollModeChanged);
  1157. eventBus._off("switchspreadmode", webViewerSwitchSpreadMode);
  1158. eventBus._off("spreadmodechanged", webViewerSpreadModeChanged);
  1159. eventBus._off("documentproperties", webViewerDocumentProperties);
  1160. eventBus._off("find", webViewerFind);
  1161. eventBus._off("findfromurlhash", webViewerFindFromUrlHash);
  1162. eventBus._off("updatefindmatchescount", webViewerUpdateFindMatchesCount);
  1163. eventBus._off("updatefindcontrolstate", webViewerUpdateFindControlState);
  1164. eventBus._off("fileinputchange", webViewerFileInputChange);
  1165. eventBus._off("openfile", webViewerOpenFile);
  1166. _boundEvents.beforePrint = null;
  1167. _boundEvents.afterPrint = null;
  1168. },
  1169. unbindWindowEvents() {
  1170. const {
  1171. _boundEvents
  1172. } = this;
  1173. window.removeEventListener("visibilitychange", webViewerVisibilityChange);
  1174. window.removeEventListener("wheel", webViewerWheel, {
  1175. passive: false
  1176. });
  1177. window.removeEventListener("click", webViewerClick);
  1178. window.removeEventListener("keydown", webViewerKeyDown);
  1179. window.removeEventListener("resize", _boundEvents.windowResize);
  1180. window.removeEventListener("hashchange", _boundEvents.windowHashChange);
  1181. window.removeEventListener("beforeprint", _boundEvents.windowBeforePrint);
  1182. window.removeEventListener("afterprint", _boundEvents.windowAfterPrint);
  1183. _boundEvents.windowResize = null;
  1184. _boundEvents.windowHashChange = null;
  1185. _boundEvents.windowBeforePrint = null;
  1186. _boundEvents.windowAfterPrint = null;
  1187. }
  1188. };
  1189. exports.PDFViewerApplication = PDFViewerApplication;
  1190. let validateFileURL;
  1191. {
  1192. const HOSTED_VIEWER_ORIGINS = ["null", "http://mozilla.github.io", "https://mozilla.github.io"];
  1193. validateFileURL = function (file) {
  1194. if (file === undefined) {
  1195. return;
  1196. }
  1197. try {
  1198. const viewerOrigin = new URL(window.location.href).origin || "null";
  1199. if (HOSTED_VIEWER_ORIGINS.includes(viewerOrigin)) {
  1200. return;
  1201. }
  1202. const {
  1203. origin,
  1204. protocol
  1205. } = new URL(file, window.location.href);
  1206. if (origin !== viewerOrigin && protocol !== "blob:") {
  1207. throw new Error("file origin does not match viewer's");
  1208. }
  1209. } catch (ex) {
  1210. const message = ex && ex.message;
  1211. PDFViewerApplication.l10n.get("loading_error", null, "An error occurred while loading the PDF.").then(loadingErrorMessage => {
  1212. PDFViewerApplication.error(loadingErrorMessage, {
  1213. message
  1214. });
  1215. });
  1216. throw ex;
  1217. }
  1218. };
  1219. }
  1220. async function loadFakeWorker() {
  1221. if (!_pdf.GlobalWorkerOptions.workerSrc) {
  1222. _pdf.GlobalWorkerOptions.workerSrc = _app_options.AppOptions.get("workerSrc");
  1223. }
  1224. return (0, _pdf.loadScript)(_pdf.PDFWorker.getWorkerSrc());
  1225. }
  1226. function loadAndEnablePDFBug(enabledTabs) {
  1227. const appConfig = PDFViewerApplication.appConfig;
  1228. return (0, _pdf.loadScript)(appConfig.debuggerScriptPath).then(function () {
  1229. PDFBug.enable(enabledTabs);
  1230. PDFBug.init({
  1231. OPS: _pdf.OPS
  1232. }, appConfig.mainContainer);
  1233. });
  1234. }
  1235. function webViewerInitialized() {
  1236. const appConfig = PDFViewerApplication.appConfig;
  1237. let file;
  1238. const queryString = document.location.search.substring(1);
  1239. const params = (0, _ui_utils.parseQueryString)(queryString);
  1240. file = "file" in params ? params.file : _app_options.AppOptions.get("defaultUrl");
  1241. validateFileURL(file);
  1242. const fileInput = document.createElement("input");
  1243. fileInput.id = appConfig.openFileInputName;
  1244. fileInput.className = "fileInput";
  1245. fileInput.setAttribute("type", "file");
  1246. fileInput.oncontextmenu = _ui_utils.noContextMenuHandler;
  1247. document.body.appendChild(fileInput);
  1248. if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
  1249. appConfig.toolbar.openFile.setAttribute("hidden", "true");
  1250. appConfig.secondaryToolbar.openFileButton.setAttribute("hidden", "true");
  1251. } else {
  1252. fileInput.value = null;
  1253. }
  1254. fileInput.addEventListener("change", function (evt) {
  1255. const files = evt.target.files;
  1256. if (!files || files.length === 0) {
  1257. return;
  1258. }
  1259. PDFViewerApplication.eventBus.dispatch("fileinputchange", {
  1260. source: this,
  1261. fileInput: evt.target
  1262. });
  1263. });
  1264. appConfig.mainContainer.addEventListener("dragover", function (evt) {
  1265. evt.preventDefault();
  1266. evt.dataTransfer.dropEffect = "move";
  1267. });
  1268. appConfig.mainContainer.addEventListener("drop", function (evt) {
  1269. evt.preventDefault();
  1270. const files = evt.dataTransfer.files;
  1271. if (!files || files.length === 0) {
  1272. return;
  1273. }
  1274. PDFViewerApplication.eventBus.dispatch("fileinputchange", {
  1275. source: this,
  1276. fileInput: evt.dataTransfer
  1277. });
  1278. });
  1279. if (!PDFViewerApplication.supportsDocumentFonts) {
  1280. _app_options.AppOptions.set("disableFontFace", true);
  1281. PDFViewerApplication.l10n.get("web_fonts_disabled", null, "Web fonts are disabled: unable to use embedded PDF fonts.").then(msg => {
  1282. console.warn(msg);
  1283. });
  1284. }
  1285. if (!PDFViewerApplication.supportsPrinting) {
  1286. appConfig.toolbar.print.classList.add("hidden");
  1287. appConfig.secondaryToolbar.printButton.classList.add("hidden");
  1288. }
  1289. if (!PDFViewerApplication.supportsFullscreen) {
  1290. appConfig.toolbar.presentationModeButton.classList.add("hidden");
  1291. appConfig.secondaryToolbar.presentationModeButton.classList.add("hidden");
  1292. }
  1293. if (PDFViewerApplication.supportsIntegratedFind) {
  1294. appConfig.toolbar.viewFind.classList.add("hidden");
  1295. }
  1296. appConfig.mainContainer.addEventListener("transitionend", function (evt) {
  1297. if (evt.target === this) {
  1298. PDFViewerApplication.eventBus.dispatch("resize", {
  1299. source: this
  1300. });
  1301. }
  1302. }, true);
  1303. try {
  1304. webViewerOpenFileViaURL(file);
  1305. } catch (reason) {
  1306. PDFViewerApplication.l10n.get("loading_error", null, "An error occurred while loading the PDF.").then(msg => {
  1307. PDFViewerApplication.error(msg, reason);
  1308. });
  1309. }
  1310. }
  1311. let webViewerOpenFileViaURL;
  1312. {
  1313. webViewerOpenFileViaURL = function (file) {
  1314. if (file && file.lastIndexOf("file:", 0) === 0) {
  1315. PDFViewerApplication.setTitleUsingUrl(file);
  1316. const xhr = new XMLHttpRequest();
  1317. xhr.onload = function () {
  1318. PDFViewerApplication.open(new Uint8Array(xhr.response));
  1319. };
  1320. xhr.open("GET", file);
  1321. xhr.responseType = "arraybuffer";
  1322. xhr.send();
  1323. return;
  1324. }
  1325. if (file) {
  1326. PDFViewerApplication.open(file);
  1327. }
  1328. };
  1329. }
  1330. function webViewerResetPermissions() {
  1331. const {
  1332. appConfig
  1333. } = PDFViewerApplication;
  1334. if (!appConfig) {
  1335. return;
  1336. }
  1337. appConfig.viewerContainer.classList.remove(ENABLE_PERMISSIONS_CLASS);
  1338. }
  1339. function webViewerPageRendered(evt) {
  1340. const pageNumber = evt.pageNumber;
  1341. const pageIndex = pageNumber - 1;
  1342. const pageView = PDFViewerApplication.pdfViewer.getPageView(pageIndex);
  1343. if (pageNumber === PDFViewerApplication.page) {
  1344. PDFViewerApplication.toolbar.updateLoadingIndicatorState(false);
  1345. }
  1346. if (!pageView) {
  1347. return;
  1348. }
  1349. if (PDFViewerApplication.pdfSidebar.isThumbnailViewVisible) {
  1350. const thumbnailView = PDFViewerApplication.pdfThumbnailViewer.getThumbnail(pageIndex);
  1351. thumbnailView.setImage(pageView);
  1352. }
  1353. if (typeof Stats !== "undefined" && Stats.enabled && pageView.stats) {
  1354. Stats.add(pageNumber, pageView.stats);
  1355. }
  1356. if (pageView.error) {
  1357. PDFViewerApplication.l10n.get("rendering_error", null, "An error occurred while rendering the page.").then(msg => {
  1358. PDFViewerApplication.error(msg, pageView.error);
  1359. });
  1360. }
  1361. PDFViewerApplication.externalServices.reportTelemetry({
  1362. type: "pageInfo",
  1363. timestamp: evt.timestamp
  1364. });
  1365. PDFViewerApplication.pdfDocument.getStats().then(function (stats) {
  1366. PDFViewerApplication.externalServices.reportTelemetry({
  1367. type: "documentStats",
  1368. stats
  1369. });
  1370. });
  1371. }
  1372. function webViewerPageMode({
  1373. mode
  1374. }) {
  1375. let view;
  1376. switch (mode) {
  1377. case "thumbs":
  1378. view = _pdf_sidebar.SidebarView.THUMBS;
  1379. break;
  1380. case "bookmarks":
  1381. case "outline":
  1382. view = _pdf_sidebar.SidebarView.OUTLINE;
  1383. break;
  1384. case "attachments":
  1385. view = _pdf_sidebar.SidebarView.ATTACHMENTS;
  1386. break;
  1387. case "none":
  1388. view = _pdf_sidebar.SidebarView.NONE;
  1389. break;
  1390. default:
  1391. console.error('Invalid "pagemode" hash parameter: ' + mode);
  1392. return;
  1393. }
  1394. PDFViewerApplication.pdfSidebar.switchView(view, true);
  1395. }
  1396. function webViewerNamedAction(evt) {
  1397. const action = evt.action;
  1398. switch (action) {
  1399. case "GoToPage":
  1400. PDFViewerApplication.appConfig.toolbar.pageNumber.select();
  1401. break;
  1402. case "Find":
  1403. if (!PDFViewerApplication.supportsIntegratedFind) {
  1404. PDFViewerApplication.findBar.toggle();
  1405. }
  1406. break;
  1407. }
  1408. }
  1409. function webViewerPresentationModeChanged({
  1410. active,
  1411. switchInProgress
  1412. }) {
  1413. let state = _ui_utils.PresentationModeState.NORMAL;
  1414. if (switchInProgress) {
  1415. state = _ui_utils.PresentationModeState.CHANGING;
  1416. } else if (active) {
  1417. state = _ui_utils.PresentationModeState.FULLSCREEN;
  1418. }
  1419. PDFViewerApplication.pdfViewer.presentationModeState = state;
  1420. }
  1421. function webViewerSidebarViewChanged(evt) {
  1422. PDFViewerApplication.pdfRenderingQueue.isThumbnailViewEnabled = PDFViewerApplication.pdfSidebar.isThumbnailViewVisible;
  1423. const store = PDFViewerApplication.store;
  1424. if (store && PDFViewerApplication.isInitialViewSet) {
  1425. store.set("sidebarView", evt.view).catch(function () {});
  1426. }
  1427. }
  1428. function webViewerUpdateViewarea(evt) {
  1429. const location = evt.location,
  1430. store = PDFViewerApplication.store;
  1431. if (store && PDFViewerApplication.isInitialViewSet) {
  1432. store.setMultiple({
  1433. page: location.pageNumber,
  1434. zoom: location.scale,
  1435. scrollLeft: location.left,
  1436. scrollTop: location.top,
  1437. rotation: location.rotation
  1438. }).catch(function () {});
  1439. }
  1440. const href = PDFViewerApplication.pdfLinkService.getAnchorUrl(location.pdfOpenParams);
  1441. PDFViewerApplication.appConfig.toolbar.viewBookmark.href = href;
  1442. PDFViewerApplication.appConfig.secondaryToolbar.viewBookmarkButton.href = href;
  1443. const currentPage = PDFViewerApplication.pdfViewer.getPageView(PDFViewerApplication.page - 1);
  1444. const loading = currentPage.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED;
  1445. PDFViewerApplication.toolbar.updateLoadingIndicatorState(loading);
  1446. }
  1447. function webViewerScrollModeChanged(evt) {
  1448. const store = PDFViewerApplication.store;
  1449. if (store && PDFViewerApplication.isInitialViewSet) {
  1450. store.set("scrollMode", evt.mode).catch(function () {});
  1451. }
  1452. }
  1453. function webViewerSpreadModeChanged(evt) {
  1454. const store = PDFViewerApplication.store;
  1455. if (store && PDFViewerApplication.isInitialViewSet) {
  1456. store.set("spreadMode", evt.mode).catch(function () {});
  1457. }
  1458. }
  1459. function webViewerResize() {
  1460. const {
  1461. pdfDocument,
  1462. pdfViewer
  1463. } = PDFViewerApplication;
  1464. if (!pdfDocument) {
  1465. return;
  1466. }
  1467. const currentScaleValue = pdfViewer.currentScaleValue;
  1468. if (currentScaleValue === "auto" || currentScaleValue === "page-fit" || currentScaleValue === "page-width") {
  1469. pdfViewer.currentScaleValue = currentScaleValue;
  1470. }
  1471. pdfViewer.update();
  1472. }
  1473. function webViewerHashchange(evt) {
  1474. const hash = evt.hash;
  1475. if (!hash) {
  1476. return;
  1477. }
  1478. if (!PDFViewerApplication.isInitialViewSet) {
  1479. PDFViewerApplication.initialBookmark = hash;
  1480. } else if (!PDFViewerApplication.pdfHistory.popStateInProgress) {
  1481. PDFViewerApplication.pdfLinkService.setHash(hash);
  1482. }
  1483. }
  1484. let webViewerFileInputChange, webViewerOpenFile;
  1485. {
  1486. webViewerFileInputChange = function (evt) {
  1487. if (PDFViewerApplication.pdfViewer && PDFViewerApplication.pdfViewer.isInPresentationMode) {
  1488. return;
  1489. }
  1490. const file = evt.fileInput.files[0];
  1491. if (!_app_options.AppOptions.get("disableCreateObjectURL")) {
  1492. let url = URL.createObjectURL(file);
  1493. if (file.name) {
  1494. url = {
  1495. url,
  1496. originalUrl: file.name
  1497. };
  1498. }
  1499. PDFViewerApplication.open(url);
  1500. } else {
  1501. PDFViewerApplication.setTitleUsingUrl(file.name);
  1502. const fileReader = new FileReader();
  1503. fileReader.onload = function webViewerChangeFileReaderOnload(event) {
  1504. const buffer = event.target.result;
  1505. PDFViewerApplication.open(new Uint8Array(buffer));
  1506. };
  1507. fileReader.readAsArrayBuffer(file);
  1508. }
  1509. const appConfig = PDFViewerApplication.appConfig;
  1510. appConfig.toolbar.viewBookmark.setAttribute("hidden", "true");
  1511. appConfig.secondaryToolbar.viewBookmarkButton.setAttribute("hidden", "true");
  1512. appConfig.toolbar.download.setAttribute("hidden", "true");
  1513. appConfig.secondaryToolbar.downloadButton.setAttribute("hidden", "true");
  1514. };
  1515. webViewerOpenFile = function (evt) {
  1516. const openFileInputName = PDFViewerApplication.appConfig.openFileInputName;
  1517. document.getElementById(openFileInputName).click();
  1518. };
  1519. }
  1520. function webViewerPresentationMode() {
  1521. PDFViewerApplication.requestPresentationMode();
  1522. }
  1523. function webViewerPrint() {
  1524. window.print();
  1525. }
  1526. function webViewerDownload() {
  1527. PDFViewerApplication.download();
  1528. }
  1529. function webViewerFirstPage() {
  1530. if (PDFViewerApplication.pdfDocument) {
  1531. PDFViewerApplication.page = 1;
  1532. }
  1533. }
  1534. function webViewerLastPage() {
  1535. if (PDFViewerApplication.pdfDocument) {
  1536. PDFViewerApplication.page = PDFViewerApplication.pagesCount;
  1537. }
  1538. }
  1539. function webViewerNextPage() {
  1540. PDFViewerApplication.page++;
  1541. }
  1542. function webViewerPreviousPage() {
  1543. PDFViewerApplication.page--;
  1544. }
  1545. function webViewerZoomIn() {
  1546. PDFViewerApplication.zoomIn();
  1547. }
  1548. function webViewerZoomOut() {
  1549. PDFViewerApplication.zoomOut();
  1550. }
  1551. function webViewerZoomReset() {
  1552. PDFViewerApplication.zoomReset();
  1553. }
  1554. function webViewerPageNumberChanged(evt) {
  1555. const pdfViewer = PDFViewerApplication.pdfViewer;
  1556. if (evt.value !== "") {
  1557. pdfViewer.currentPageLabel = evt.value;
  1558. }
  1559. if (evt.value !== pdfViewer.currentPageNumber.toString() && evt.value !== pdfViewer.currentPageLabel) {
  1560. PDFViewerApplication.toolbar.setPageNumber(pdfViewer.currentPageNumber, pdfViewer.currentPageLabel);
  1561. }
  1562. }
  1563. function webViewerScaleChanged(evt) {
  1564. PDFViewerApplication.pdfViewer.currentScaleValue = evt.value;
  1565. }
  1566. function webViewerRotateCw() {
  1567. PDFViewerApplication.rotatePages(90);
  1568. }
  1569. function webViewerRotateCcw() {
  1570. PDFViewerApplication.rotatePages(-90);
  1571. }
  1572. function webViewerSwitchScrollMode(evt) {
  1573. PDFViewerApplication.pdfViewer.scrollMode = evt.mode;
  1574. }
  1575. function webViewerSwitchSpreadMode(evt) {
  1576. PDFViewerApplication.pdfViewer.spreadMode = evt.mode;
  1577. }
  1578. function webViewerDocumentProperties() {
  1579. PDFViewerApplication.pdfDocumentProperties.open();
  1580. }
  1581. function webViewerFind(evt) {
  1582. PDFViewerApplication.findController.executeCommand("find" + evt.type, {
  1583. query: evt.query,
  1584. phraseSearch: evt.phraseSearch,
  1585. caseSensitive: evt.caseSensitive,
  1586. entireWord: evt.entireWord,
  1587. highlightAll: evt.highlightAll,
  1588. findPrevious: evt.findPrevious
  1589. });
  1590. }
  1591. function webViewerFindFromUrlHash(evt) {
  1592. PDFViewerApplication.findController.executeCommand("find", {
  1593. query: evt.query,
  1594. phraseSearch: evt.phraseSearch,
  1595. caseSensitive: false,
  1596. entireWord: false,
  1597. highlightAll: true,
  1598. findPrevious: false
  1599. });
  1600. }
  1601. function webViewerUpdateFindMatchesCount({
  1602. matchesCount
  1603. }) {
  1604. if (PDFViewerApplication.supportsIntegratedFind) {
  1605. PDFViewerApplication.externalServices.updateFindMatchesCount(matchesCount);
  1606. } else {
  1607. PDFViewerApplication.findBar.updateResultsCount(matchesCount);
  1608. }
  1609. }
  1610. function webViewerUpdateFindControlState({
  1611. state,
  1612. previous,
  1613. matchesCount
  1614. }) {
  1615. if (PDFViewerApplication.supportsIntegratedFind) {
  1616. PDFViewerApplication.externalServices.updateFindControlState({
  1617. result: state,
  1618. findPrevious: previous,
  1619. matchesCount
  1620. });
  1621. } else {
  1622. PDFViewerApplication.findBar.updateUIState(state, previous, matchesCount);
  1623. }
  1624. }
  1625. function webViewerScaleChanging(evt) {
  1626. PDFViewerApplication.toolbar.setPageScale(evt.presetValue, evt.scale);
  1627. PDFViewerApplication.pdfViewer.update();
  1628. }
  1629. function webViewerRotationChanging(evt) {
  1630. PDFViewerApplication.pdfThumbnailViewer.pagesRotation = evt.pagesRotation;
  1631. PDFViewerApplication.forceRendering();
  1632. PDFViewerApplication.pdfViewer.currentPageNumber = evt.pageNumber;
  1633. }
  1634. function webViewerPageChanging(evt) {
  1635. const page = evt.pageNumber;
  1636. PDFViewerApplication.toolbar.setPageNumber(page, evt.pageLabel || null);
  1637. PDFViewerApplication.secondaryToolbar.setPageNumber(page);
  1638. if (PDFViewerApplication.pdfSidebar.isThumbnailViewVisible) {
  1639. PDFViewerApplication.pdfThumbnailViewer.scrollThumbnailIntoView(page);
  1640. }
  1641. if (typeof Stats !== "undefined" && Stats.enabled) {
  1642. const pageView = PDFViewerApplication.pdfViewer.getPageView(page - 1);
  1643. if (pageView && pageView.stats) {
  1644. Stats.add(page, pageView.stats);
  1645. }
  1646. }
  1647. }
  1648. function webViewerVisibilityChange(evt) {
  1649. if (document.visibilityState === "visible") {
  1650. setZoomDisabledTimeout();
  1651. }
  1652. }
  1653. let zoomDisabledTimeout = null;
  1654. function setZoomDisabledTimeout() {
  1655. if (zoomDisabledTimeout) {
  1656. clearTimeout(zoomDisabledTimeout);
  1657. }
  1658. zoomDisabledTimeout = setTimeout(function () {
  1659. zoomDisabledTimeout = null;
  1660. }, WHEEL_ZOOM_DISABLED_TIMEOUT);
  1661. }
  1662. function webViewerWheel(evt) {
  1663. const {
  1664. pdfViewer,
  1665. supportedMouseWheelZoomModifierKeys
  1666. } = PDFViewerApplication;
  1667. if (pdfViewer.isInPresentationMode) {
  1668. return;
  1669. }
  1670. if (evt.ctrlKey && supportedMouseWheelZoomModifierKeys.ctrlKey || evt.metaKey && supportedMouseWheelZoomModifierKeys.metaKey) {
  1671. evt.preventDefault();
  1672. if (zoomDisabledTimeout || document.visibilityState === "hidden") {
  1673. return;
  1674. }
  1675. const previousScale = pdfViewer.currentScale;
  1676. const delta = (0, _ui_utils.normalizeWheelEventDelta)(evt);
  1677. const MOUSE_WHEEL_DELTA_PER_PAGE_SCALE = 3.0;
  1678. const ticks = delta * MOUSE_WHEEL_DELTA_PER_PAGE_SCALE;
  1679. if (ticks < 0) {
  1680. PDFViewerApplication.zoomOut(-ticks);
  1681. } else {
  1682. PDFViewerApplication.zoomIn(ticks);
  1683. }
  1684. const currentScale = pdfViewer.currentScale;
  1685. if (previousScale !== currentScale) {
  1686. const scaleCorrectionFactor = currentScale / previousScale - 1;
  1687. const rect = pdfViewer.container.getBoundingClientRect();
  1688. const dx = evt.clientX - rect.left;
  1689. const dy = evt.clientY - rect.top;
  1690. pdfViewer.container.scrollLeft += dx * scaleCorrectionFactor;
  1691. pdfViewer.container.scrollTop += dy * scaleCorrectionFactor;
  1692. }
  1693. } else {
  1694. setZoomDisabledTimeout();
  1695. }
  1696. }
  1697. function webViewerClick(evt) {
  1698. if (!PDFViewerApplication.secondaryToolbar.isOpen) {
  1699. return;
  1700. }
  1701. const appConfig = PDFViewerApplication.appConfig;
  1702. if (PDFViewerApplication.pdfViewer.containsElement(evt.target) || appConfig.toolbar.container.contains(evt.target) && evt.target !== appConfig.secondaryToolbar.toggleButton) {
  1703. PDFViewerApplication.secondaryToolbar.close();
  1704. }
  1705. }
  1706. function webViewerKeyDown(evt) {
  1707. if (PDFViewerApplication.overlayManager.active) {
  1708. return;
  1709. }
  1710. let handled = false,
  1711. ensureViewerFocused = false;
  1712. const cmd = (evt.ctrlKey ? 1 : 0) | (evt.altKey ? 2 : 0) | (evt.shiftKey ? 4 : 0) | (evt.metaKey ? 8 : 0);
  1713. const pdfViewer = PDFViewerApplication.pdfViewer;
  1714. const isViewerInPresentationMode = pdfViewer && pdfViewer.isInPresentationMode;
  1715. if (cmd === 1 || cmd === 8 || cmd === 5 || cmd === 12) {
  1716. switch (evt.keyCode) {
  1717. case 70:
  1718. if (!PDFViewerApplication.supportsIntegratedFind) {
  1719. PDFViewerApplication.findBar.open();
  1720. handled = true;
  1721. }
  1722. break;
  1723. case 71:
  1724. if (!PDFViewerApplication.supportsIntegratedFind) {
  1725. const findState = PDFViewerApplication.findController.state;
  1726. if (findState) {
  1727. PDFViewerApplication.findController.executeCommand("findagain", {
  1728. query: findState.query,
  1729. phraseSearch: findState.phraseSearch,
  1730. caseSensitive: findState.caseSensitive,
  1731. entireWord: findState.entireWord,
  1732. highlightAll: findState.highlightAll,
  1733. findPrevious: cmd === 5 || cmd === 12
  1734. });
  1735. }
  1736. handled = true;
  1737. }
  1738. break;
  1739. case 61:
  1740. case 107:
  1741. case 187:
  1742. case 171:
  1743. if (!isViewerInPresentationMode) {
  1744. PDFViewerApplication.zoomIn();
  1745. }
  1746. handled = true;
  1747. break;
  1748. case 173:
  1749. case 109:
  1750. case 189:
  1751. if (!isViewerInPresentationMode) {
  1752. PDFViewerApplication.zoomOut();
  1753. }
  1754. handled = true;
  1755. break;
  1756. case 48:
  1757. case 96:
  1758. if (!isViewerInPresentationMode) {
  1759. setTimeout(function () {
  1760. PDFViewerApplication.zoomReset();
  1761. });
  1762. handled = false;
  1763. }
  1764. break;
  1765. case 38:
  1766. if (isViewerInPresentationMode || PDFViewerApplication.page > 1) {
  1767. PDFViewerApplication.page = 1;
  1768. handled = true;
  1769. ensureViewerFocused = true;
  1770. }
  1771. break;
  1772. case 40:
  1773. if (isViewerInPresentationMode || PDFViewerApplication.page < PDFViewerApplication.pagesCount) {
  1774. PDFViewerApplication.page = PDFViewerApplication.pagesCount;
  1775. handled = true;
  1776. ensureViewerFocused = true;
  1777. }
  1778. break;
  1779. }
  1780. }
  1781. const {
  1782. eventBus
  1783. } = PDFViewerApplication;
  1784. if (cmd === 1 || cmd === 8) {
  1785. switch (evt.keyCode) {
  1786. case 83:
  1787. eventBus.dispatch("download", {
  1788. source: window
  1789. });
  1790. handled = true;
  1791. break;
  1792. case 79:
  1793. {
  1794. eventBus.dispatch("openfile", {
  1795. source: window
  1796. });
  1797. handled = true;
  1798. }
  1799. break;
  1800. }
  1801. }
  1802. if (cmd === 3 || cmd === 10) {
  1803. switch (evt.keyCode) {
  1804. case 80:
  1805. PDFViewerApplication.requestPresentationMode();
  1806. handled = true;
  1807. break;
  1808. case 71:
  1809. PDFViewerApplication.appConfig.toolbar.pageNumber.select();
  1810. handled = true;
  1811. break;
  1812. }
  1813. }
  1814. if (handled) {
  1815. if (ensureViewerFocused && !isViewerInPresentationMode) {
  1816. pdfViewer.focus();
  1817. }
  1818. evt.preventDefault();
  1819. return;
  1820. }
  1821. const curElement = document.activeElement || document.querySelector(":focus");
  1822. const curElementTagName = curElement && curElement.tagName.toUpperCase();
  1823. if (curElementTagName === "INPUT" || curElementTagName === "TEXTAREA" || curElementTagName === "SELECT" || curElement && curElement.isContentEditable) {
  1824. if (evt.keyCode !== 27) {
  1825. return;
  1826. }
  1827. }
  1828. if (cmd === 0) {
  1829. let turnPage = 0,
  1830. turnOnlyIfPageFit = false;
  1831. switch (evt.keyCode) {
  1832. case 38:
  1833. case 33:
  1834. if (pdfViewer.isVerticalScrollbarEnabled) {
  1835. turnOnlyIfPageFit = true;
  1836. }
  1837. turnPage = -1;
  1838. break;
  1839. case 8:
  1840. if (!isViewerInPresentationMode) {
  1841. turnOnlyIfPageFit = true;
  1842. }
  1843. turnPage = -1;
  1844. break;
  1845. case 37:
  1846. if (pdfViewer.isHorizontalScrollbarEnabled) {
  1847. turnOnlyIfPageFit = true;
  1848. }
  1849. case 75:
  1850. case 80:
  1851. turnPage = -1;
  1852. break;
  1853. case 27:
  1854. if (PDFViewerApplication.secondaryToolbar.isOpen) {
  1855. PDFViewerApplication.secondaryToolbar.close();
  1856. handled = true;
  1857. }
  1858. if (!PDFViewerApplication.supportsIntegratedFind && PDFViewerApplication.findBar.opened) {
  1859. PDFViewerApplication.findBar.close();
  1860. handled = true;
  1861. }
  1862. break;
  1863. case 40:
  1864. case 34:
  1865. if (pdfViewer.isVerticalScrollbarEnabled) {
  1866. turnOnlyIfPageFit = true;
  1867. }
  1868. turnPage = 1;
  1869. break;
  1870. case 13:
  1871. case 32:
  1872. if (!isViewerInPresentationMode) {
  1873. turnOnlyIfPageFit = true;
  1874. }
  1875. turnPage = 1;
  1876. break;
  1877. case 39:
  1878. if (pdfViewer.isHorizontalScrollbarEnabled) {
  1879. turnOnlyIfPageFit = true;
  1880. }
  1881. case 74:
  1882. case 78:
  1883. turnPage = 1;
  1884. break;
  1885. case 36:
  1886. if (isViewerInPresentationMode || PDFViewerApplication.page > 1) {
  1887. PDFViewerApplication.page = 1;
  1888. handled = true;
  1889. ensureViewerFocused = true;
  1890. }
  1891. break;
  1892. case 35:
  1893. if (isViewerInPresentationMode || PDFViewerApplication.page < PDFViewerApplication.pagesCount) {
  1894. PDFViewerApplication.page = PDFViewerApplication.pagesCount;
  1895. handled = true;
  1896. ensureViewerFocused = true;
  1897. }
  1898. break;
  1899. case 83:
  1900. PDFViewerApplication.pdfCursorTools.switchTool(_pdf_cursor_tools.CursorTool.SELECT);
  1901. break;
  1902. case 72:
  1903. PDFViewerApplication.pdfCursorTools.switchTool(_pdf_cursor_tools.CursorTool.HAND);
  1904. break;
  1905. case 82:
  1906. PDFViewerApplication.rotatePages(90);
  1907. break;
  1908. case 115:
  1909. PDFViewerApplication.pdfSidebar.toggle();
  1910. break;
  1911. }
  1912. if (turnPage !== 0 && (!turnOnlyIfPageFit || pdfViewer.currentScaleValue === "page-fit")) {
  1913. if (turnPage > 0) {
  1914. if (PDFViewerApplication.page < PDFViewerApplication.pagesCount) {
  1915. PDFViewerApplication.page++;
  1916. }
  1917. } else {
  1918. if (PDFViewerApplication.page > 1) {
  1919. PDFViewerApplication.page--;
  1920. }
  1921. }
  1922. handled = true;
  1923. }
  1924. }
  1925. if (cmd === 4) {
  1926. switch (evt.keyCode) {
  1927. case 13:
  1928. case 32:
  1929. if (!isViewerInPresentationMode && pdfViewer.currentScaleValue !== "page-fit") {
  1930. break;
  1931. }
  1932. if (PDFViewerApplication.page > 1) {
  1933. PDFViewerApplication.page--;
  1934. }
  1935. handled = true;
  1936. break;
  1937. case 82:
  1938. PDFViewerApplication.rotatePages(-90);
  1939. break;
  1940. }
  1941. }
  1942. if (!handled && !isViewerInPresentationMode) {
  1943. if (evt.keyCode >= 33 && evt.keyCode <= 40 || evt.keyCode === 32 && curElementTagName !== "BUTTON") {
  1944. ensureViewerFocused = true;
  1945. }
  1946. }
  1947. if (ensureViewerFocused && !pdfViewer.containsElement(curElement)) {
  1948. pdfViewer.focus();
  1949. }
  1950. if (handled) {
  1951. evt.preventDefault();
  1952. }
  1953. }
  1954. function apiPageLayoutToSpreadMode(layout) {
  1955. switch (layout) {
  1956. case "SinglePage":
  1957. case "OneColumn":
  1958. return _ui_utils.SpreadMode.NONE;
  1959. case "TwoColumnLeft":
  1960. case "TwoPageLeft":
  1961. return _ui_utils.SpreadMode.ODD;
  1962. case "TwoColumnRight":
  1963. case "TwoPageRight":
  1964. return _ui_utils.SpreadMode.EVEN;
  1965. }
  1966. return _ui_utils.SpreadMode.NONE;
  1967. }
  1968. function apiPageModeToSidebarView(mode) {
  1969. switch (mode) {
  1970. case "UseNone":
  1971. return _pdf_sidebar.SidebarView.NONE;
  1972. case "UseThumbs":
  1973. return _pdf_sidebar.SidebarView.THUMBS;
  1974. case "UseOutlines":
  1975. return _pdf_sidebar.SidebarView.OUTLINE;
  1976. case "UseAttachments":
  1977. return _pdf_sidebar.SidebarView.ATTACHMENTS;
  1978. case "UseOC":
  1979. }
  1980. return _pdf_sidebar.SidebarView.NONE;
  1981. }
  1982. const PDFPrintServiceFactory = {
  1983. instance: {
  1984. supportsPrinting: false,
  1985. createPrintService() {
  1986. throw new Error("Not implemented: createPrintService");
  1987. }
  1988. }
  1989. };
  1990. exports.PDFPrintServiceFactory = PDFPrintServiceFactory;