How to detect virtual & real-physical printers from Javascript
Product JSPrintManager Published 01/15/2019 Updated 01/15/2019 Author Neodynamic
Overview
Sometimes, there are business polocies where certain documents should only be printed on physcal printers and web developers are given the task of just doing it. Determining whether a given client printer is a virtual or a real/physical device is not always straightforward but thanks to tools like JSPrintManager solution, you'd be able to get the needed printer information to infer it.
In this walkthrough, you'll learn how to use the printer info returned by JSPrintManager to trying determining whether a client printer is a virtual or a real/physical device. This solution works with any popular browser like Chrome, Firefox, IE/Edge & Safari on Windows, Linux, Mac & Raspberry Pi
The sample code will output a page like the following:
A sample list of installed printers and whether they are virtual or real-physical devices
Follow up these steps
- Be sure you install in your dev machine JSPrintManager (JSPM) (Available for Windows, Linux, Raspberry Pi & Mac)
This small app must be installed on each client that will print from your website! - By using your favorite Web Development IDE or Text Editor, create a new HTML file like index.html
Copy/paste the following snipped codes:HTML Code
<div style="text-align:center"> <h1>List of Installed Printers</h1> <hr /> <div id="lstPrinters"> </div> </div>
Script References
- Download JSPrintManager.js
- Copy all of these *.js files to the same folder where your html page is located and the add the following script references to your page.
<script src="JSPrintManager.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.5/bluebird.min.js"></script> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
Script Code
- Copy & paste the following script code into your page file:
<script> var _this = this; //WebSocket settings JSPM.JSPrintManager.auto_reconnect = true; JSPM.JSPrintManager.start(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (jspmWSStatus()) { //get client installed printers JSPM.JSPrintManager.getPrintersInfo().then(function (printersList) { let clientPrinters = printersList; let htmlContent = ''; for (let i = 0; i < clientPrinters.length; i++) { htmlContent += '<div>' + clientPrinters[i].name + ' ====>>>> ' + (_this.isVirtualPrinter(clientPrinters[i]) ? '<strong><em><span style="color:red">VIRTUAL PRINTER</span></em></strong>' : '<strong>REAL/PHYSICAL PRINTER</strong>') + '</div>'; } $('#lstPrinters').html(htmlContent); }); } }; //Check JSPM WebSocket status function jspmWSStatus() { if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Open) return true; else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Closed) { alert('JSPrintManager (JSPM) is not installed or not running! Download JSPM Client App from https://neodynamic.com/downloads/jspm'); return false; } else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Blocked) { alert('JSPM has blocked this website!'); return false; } } //Guessing whether a printer is Virtual or real/physical function isVirtualPrinter(clientPrinter) { let printerPort = clientPrinter.port.toLowerCase(); //For Windows if (printerPort != "nul" && clientPrinter.BIDIEnabled) return false; //For Unix if (printerPort.indexOf("usb") >= 0 && printerPort.indexOf("?serial=") >= 0) return false; return true; } </script>
- That's it! Run your website and test it.