app.js 67 KB

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