npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pdfjs-vue-print

v0.0.10

Published

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

Downloads

4

Readme

PDFjs-vue-print

Example Vue.js 3 project using PDF.js to build a simple, custom PDF.js viewer and print service. Though, the Javascript code in pdf_lib could be used in any Javascript framework.

This is a simple, bare bones project to load, view and print a PDF document. Based on example code from Mozilla's pdf.js project on GitHub.

Recommended IDE Setup

Installing

git clone https://gitlab.com/drewlab/pdfjs-vue-print.git
cd pdfjs-vue-print
npm install

Try It

Run the Vite development web server which will display a home page with buttons to view and/or print a PDF document. Open the Javascript console to see console.log() messages added to show the program flow. The examples automatically open the Print Preview Dialog. Cancel it to avoid printing the 14 page PDF document.

Launch "dev serve" or use command line "npm run dev".
Launch "Firefox developer" or in a browser go to http://localhost:3000/.

Project Folders Overview

Here are the relevant project files. Assuming the user has some knowledge of building apps with Vue.js 3.

  • package.js - dependency for the pdfjs-dist package.
  • src/components/
  • src/components/HomePage.vue - main page with component switcher to choose view/print components.
  • src/components/ViewNPrint.vue - load custom PDF.js viewer/printer app.
  • src/components/ViewOnly.vue - just the custom PDF.js viewer part of the app.
  • src/components/PrintOnly.vue - custom PDF.js printer app without using PDF.js PDFViewer.
  • src/pdf_lib/
  • src/pdf_lib/viewer_app.js - { PDFViewerApp } simple viewer using pdfjs-dist to load and display a PDF document.
  • src/pdf_lib/printer_app.js - { PDFPrinterApp } extends PDFViewerApp to handle window printing events.
  • src/pdf_lib/print_only_app.js - { PrintOnlyApp } extends PDFViewerApp to handle window printing events.
  • src/pdf_lib/print_service.js - { PDFPrintService } handles window.print() request and renders page images for printing on a temporary canvas.
  • src/pdf_lib/print_service_factory.js - used by PDFPrinterApp and PrintOnlyApp to create a PDFPrintService instance.
  • src/pdf_lib/viewer.css - styles for viewer and printed pages.

Project Notes

In this project I was mainly interested in an simple way to automatically print PDF documents. I looked at various code files in the PDF.js project then combined and stripped out anything that wasn't needed for viewing or printing. The main PDF.js files I looked at where the examples/components/simpleviewer, examples/mobile-viewer and the web/ folder. The web/ folder of the PDF.js project contains many files that make up the behemoth that is the PDF viewer for Firefox and plugins for other browsers. Note, of the PDF.js web/ files only a webpack'd pdf_viewer.js is included in the pdfjs-dist package.

Things that were stripped out include toolbars, sidebars, LinkManager, ScriptManager, Annotations, OverlayManager, most dialogs, etc. If you need those you'll have to do some work to add them back in. The main file to start looking at would be web/app.js (PDFViewerApplication) and the web/viewer.* files.

My PDFViewerApp and PDFPrinterApp could be combined into one class module. I kept them separate while I was learning and dissecting the PDF.js code. You can use the PDFViewerApp without PDFPrinterApp. If you want printing without viewing then use the PrintOnlyApp.

Disabling the Print Preview Dialog

The Print Preview Dialog is part the host application (browser) that displays the web pages, e.g. Firefox, Chrome, Electron. You can't disable it in web page code, because that is a security risk. Disabling the dialog is a function of the host application. In Firefox and Chrome there are configuration settings. In Electron, code written for the Electron main process can disable the dialog using the silent print option.

Using PDF.js with Electron

After trying to integrate [email protected] package into an Electron app I found that electron-forge wanted to rebuild canvas (node-canvas) with node-gyp. The rebuild of [email protected] kept failing. Falling back to a previous release of pdfjs-dist fixed the problem. Release [email protected] does not have a dependency on node-canvas.

Successfully tested Electron and pdfjs-dist with:

"dependencies": {
    "pdfjs-dist": "^2.16.105"
  },
  "devDependencies": {
    "electron": "^21.3.0"
  }

Hope you find this example useful for your PDF printing project. -Drew