Silent Word DOC Printing with Duplex, Reverse & Pages Range Settings from Javascript
Product JSPrintManager Published 05/18/2020 Updated 08/01/2022 Author Neodynamic
Overview
In this walkthrough, you'll learn how to silently print DOC/x files from any web page (written on top of any Web Platform including ASP.NET MVC/CORE, PHP, Django, Ruby On Rails (RoR), Express.js, Angular, React, Vue) directly to the client printer without displaying any print dialog and by writing pure Javascript code. You'll be able to print DOC/x files to the Default client printer as well as to any other installed printer at the client machine with advanced settings like Duplex, Print in Reverse, and Pages Range!
- Available for Windows clients only
- Microsoft Word 97+ must be installed at the client machine
- DOC files can be any of these file formats: *.docx, *.docm, *.dotx, *.dotm, *.doc, *.dot, *.rtf, and *.odt
Follow up these steps
-
JSPrintManager Client App
Be sure you install in your dev machine JSPrintManager (JSPM) (Available for Windows, Linux, Raspberry Pi & Mac)
IMPORTANT: This small app must be installed on each client! -
HTML & Script References
- Download JSPrintManager.js
- Copy all of these *.js files to the same folder where the next
index.html
page sample will be located in. - By using your favorite Web Development IDE or Text Editor, create a new HTML file naming it
index.html
Copy/paste the following code inside that html file:
<!DOCTYPE html> <html> <head> <title>JSPrintManager</title> <meta charset="utf-8" /> </head> <body> <div style="text-align:center"> <h1>Advanced DOC Printing from Javascript</h1> <hr /> <fieldset> <legend><strong>DOC File to print</strong></legend> <label> Local DOC file: <input id="input-local-doc-file" type="file" /> </label> <br /><br /> <strong>OR...</strong> <br /><br /> <label> DOC File from URL <span> (e.g. <a href="https://neodynamic.com/temp/Sample-Employee-Handbook.doc" target="_blank"> https://neodynamic.com/temp/Sample-Employee-Handbook.doc </a>) </span> <input id="input-file-url" /> </label> </fieldset> <br /><br /> <fieldset> <legend><strong>Target Printer</strong></legend> <label>Printer:</label> <select id="printerName"> </select> <label>Pages Range: [e.g. 1,2,3,10-13]</label> <input type="text" id="printRange" /> <input type="checkbox" id="printInReverseOrder" name="printInReverseOrder" /> <label for="printInReverseOrder"> Print In Reverse Order </label> <input type="checkbox" id="manualDuplex" name="manualDuplex" /> <label for="manualDuplex"> Duplex Printing </label> </fieldset> <br /> <button onclick="doPrinting();"><h2>Print DOC Now...</h2></button> </div> <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 src="scripts/JSPrintManager.js"></script> <script> var clientPrinters = null; 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.getPrinters().then(function (printersList) { clientPrinters = printersList; var options = ''; for (var i = 0; i < clientPrinters.length; i++) { options += '<option>' + clientPrinters[i] + '</option>'; } $('#printerName').html(options); }); } }; //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) { console.warn('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; } } //Do printing... function doPrinting() { if (jspmWSStatus()) { //create ClientPrintJob var cpj = new JSPM.ClientPrintJob(); //Set Printer info var myPrinter = new JSPM.InstalledPrinter($('#printerName').val()); cpj.clientPrinter = myPrinter; //Set DOC file var local_file = $("#input-local-doc-file").prop('files'); var my_file = null; if (local_file && local_file.length > 0) { my_file = new JSPM.PrintFileDOC(local_file[0], JSPM.FileSourceType.BLOB, local_file[0].name, 1); } else if ($("#input-file-url").val().length > 0) { my_file = new JSPM.PrintFileDOC($("#input-file-url").val(), JSPM.FileSourceType.URL, "myFileToPrint.doc", 1); } else { alert('Must specify a local file or a URL!'); return; } //set file printing settings... my_file.printInReverseOrder = $('#printInReverseOrder').prop('checnnnnked'); my_file.printRange = $('#printRange').val(); my_file.manualDuplex = $('#manualDuplex').prop('checked'); //add file to ClientPrintJob cpj.files.push(my_file); //Send print job to printer! cpj.sendToClient(); } } </script> </body> </html>
- That's it! Run your website and test it.