UNPKG

pdfjs-vue-print

Version:

Example Vue 3 project using pdf.js to build a simple custom PDF.js viewer and print service.

190 lines (154 loc) 5.51 kB
/* Copyright 2012 Mozilla Foundation * Copyright 2022 Drew Letcher * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ "use strict"; import { PDFViewerApp } from "./viewer_app.js" import { PDFPrintServiceFactory } from "./print_service_factory.js"; import * as PrintService from "./print_service.js" export default class PDFPrinterApp extends PDFViewerApp { constructor(options) { super(options); this.printContainer = options.printContainer || null; this.supportsPrinting = true; this.printResolution = 150; this.printService = null; } // Called once when the document is loaded. async createViewer() { super.createViewer(); PrintService.install(); let app = this; this.eventBus.on("pagesloaded", (ev) => { console.log("pagesloaded " + ev.pagesCount) // bind the various event handlers *after* the viewer has been // initialized, to prevent errors if an event arrives too soon. app.bindEvents(); app.bindWindowEvents(); }); } async close() { this.unbindWindowEvents(); this.unbindEvents(); PrintService.remove(); return super.close(); } /* forceRendering() { this.pdfRenderingQueue.printing = !!this.printService; this.pdfRenderingQueue.isThumbnailViewEnabled = this.pdfSidebar.visibleView === SidebarView.THUMBS; this.pdfRenderingQueue.renderHighestPriority(); } */ beforePrint() { console.log("beforePrint") if (this.printService) { console.ware("printService already created") // There is no way to suppress beforePrint/afterPrint events, // but PDFPrintService may generate double events -- this will ignore // the second event that will be coming from native window.print(). return; } if (!this.supportsPrinting) { this.l10n.get("printing_not_supported").then(msg => { this._otherError(msg); }); return; } // The beforePrint is a sync method and we need to know layout before // returning from this method. Ensure that we can get sizes of the pages. if (!this.pdfViewer.pageViewsReady) { this.l10n.get("printing_not_ready").then(msg => { // eslint-disable-next-line no-alert window.alert(msg); }); return; } const pagesOverview = this.pdfViewer.getPagesOverview(); const printContainer = this.printContainer; const printResolution = this.printResolution; const optionalContentConfigPromise = this.pdfViewer.optionalContentConfigPromise; console.log("create printService") const printService = PDFPrintServiceFactory.instance.createPrintService( this.pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, this.l10n ); this.printService = printService; //this.forceRendering(); //this.setTitle("printing"); printService.layout(); } afterPrint() { console.log("afterPrint"); if (this.printService) { console.log("destroy printService") this.printService.destroy(); this.printService = null; } //this.forceRendering(); //this.setTitleUsingMetadata(this.pdfDocument); } print() { if (!this.supportsPrinting) { return; } window.print(); } bindEvents() { const { eventBus, _boundEvents } = this; _boundEvents.beforePrint = this.beforePrint.bind(this); _boundEvents.afterPrint = this.afterPrint.bind(this); eventBus._on("beforeprint", _boundEvents.beforePrint); eventBus._on("afterprint", _boundEvents.afterPrint); //eventBus._on("print", print); } bindWindowEvents() { const { eventBus, _boundEvents } = this; _boundEvents.windowBeforePrint = () => { eventBus.dispatch("beforeprint", { source: window }); }; _boundEvents.windowAfterPrint = () => { eventBus.dispatch("afterprint", { source: window }); }; window.addEventListener("beforeprint", _boundEvents.windowBeforePrint); window.addEventListener("afterprint", _boundEvents.windowAfterPrint); } unbindEvents() { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { throw new Error("Not implemented: unbindEvents"); } const { eventBus, _boundEvents } = this; eventBus._off("beforeprint", _boundEvents.beforePrint); eventBus._off("afterprint", _boundEvents.afterPrint); //eventBus._off("print", print); _boundEvents.beforePrint = null; _boundEvents.afterPrint = null; } unbindWindowEvents() { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { throw new Error("Not implemented: unbindWindowEvents"); } const { _boundEvents } = this; window.removeEventListener("beforeprint", _boundEvents.windowBeforePrint); window.removeEventListener("afterprint", _boundEvents.windowAfterPrint); _boundEvents.windowBeforePrint = null; _boundEvents.windowAfterPrint = null; } }; export { PDFPrinterApp };