How to print raw commands from Web APIs, WebServices, Remote and Local files with Javascript
Product JSPrintManager Published 02/21/2019 Updated 02/21/2019 Author Neodynamic
Overview
In this article, we'll cover how to send and print any Raw Printer Commands from different sources like a Web API or Web Service, a remote file, or a local file under your website. Reading and sending the raw printer commands is done in Javascript code and our JSPrintManager solution that was specially designed for this kind of printing needs.
About Raw Printer Commands
First of all, let's clarify that "Raw Commands" refers to a stream of bytes that are expressed by following the syntax and rules of the Command Language (a.k.a. Programming Language) supported by the target printer. Some Printer Command Languages are composed of human-readable ASCII characters (like Zebra ZPL, EPL, etc) while others combine them with non-printable characters like is the case of EPSON ESC/POS, Postscript and HP-PCL languages.
On Windows, a PRN file also contains raw commands instructions for a printer which are usually created by a printer driver, therefor PRN files are printer model specific and will not work with other types of printers! It is not a simple text file you can open with a text editor software and print it as is, because it contains commands instructions in the form of bytes that must be sent to the printer without any modifications so the printer firmware can process them correctly.
Given said that, a plain text like "HELLO WORLD"
is NOT considered to be "raw commands" and you cannot expect to get the target printer to handle it and produce such output! Each printer brand supports one or more raw commands languages which are specified by the manufacturer. So, to expect some printing output, the correct "Raw Printer Commands" (which will depend in which of them the target printer supports) must be sent.
Now that you know what Raw Printer Commands refer to, in the following topics you'll learn how to read them from different sources and send them to the target printer by using Javascript and our JSPrintManager solution.
Creating a sample website
- 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>Print Raw Commands from Javascript</h1> <hr /> <label class="checkbox"> <input type="checkbox" id="useDefaultPrinter" /> <strong>Print to Default printer</strong> </label> <p>or...</p> <div id="installedPrinters"> <label for="installedPrinterName">Select an installed Printer:</label> <select name="installedPrinterName" id="installedPrinterName"></select> </div> <br /><br /> <button type="button" onclick="print();">Print Now...</button> </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 scripts and please read the following considerations depending on the sources from where the Raw Commands will be obtained.
-
Printing Raw Printer Commands generated by a Web API or WebService
- If the raw printer commands are generated by a Web API or WebService (like the one offered by well knwon postal service companies) and these APIs allow you to get them by using an XMLHttpRequest (XHR) object, then copy/paste the following code and be sure you update the
'URL-TO-GET-RAW-PRINTER-COMMANDS'
value accord e.g.https://thirdparty.com/webapi
- IMPORTANT: The source from where the Raw Commands are returned must be configured to allow Cross-Origin Resource Sharing (CORS)
- If the raw printer commands are generated by a Web API or WebService (like the one offered by well knwon postal service companies) and these APIs allow you to get them by using an XMLHttpRequest (XHR) object, then copy/paste the following code and be sure you update the
-
Printing Raw Printer Commands from Remote files
- If the raw printer commands are stored in a file which could be hosted in an external website, then use an XMLHttpRequest (XHR) object to read it. Copy/paste the following code and be sure you update the
'URL-TO-GET-RAW-PRINTER-COMMANDS'
value to the correct URL i.e. to the external file e.g.https://somesite.com/file
- IMPORTANT: For Remote files, the server/website from where the Raw Commands are returned must be configured to allow Cross-Origin Resource Sharing (CORS)
- If the raw printer commands are stored in a file which could be hosted in an external website, then use an XMLHttpRequest (XHR) object to read it. Copy/paste the following code and be sure you update the
-
Printing Raw Printer Commands from Local website files
- If the raw printer commands are stored in a file available in a local subfolder of your website, then use an XMLHttpRequest (XHR) object to read it. Copy/paste the following code and be sure you update the
'URL-TO-GET-RAW-PRINTER-COMMANDS'
value to the correct URL i.e. to the local relative file e.g./files/myfile.txt
- IMPORTANT: For local files, it's recommended that the file be renamed to *.txt extension; otherwise if the file extension is not supported by your webserver, then it must be configured to support it.
- If the raw printer commands are stored in a file available in a local subfolder of your website, then use an XMLHttpRequest (XHR) object to read it. Copy/paste the following code and be sure you update the
<script> //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 (myPrinters) { var options = ''; for (var i = 0; i < myPrinters.length; i++) { options += '<option>' + myPrinters[i] + '</option>'; } $('#installedPrinterName').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) { 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; } } //Do printing... function print(o) { if (jspmWSStatus()) { var xhr = new XMLHttpRequest(); xhr.open('GET', 'URL-TO-GET-RAW-PRINTER-COMMANDS', true); xhr.responseType = 'arraybuffer'; xhr.onload = function (e) { if (this.status == 200) { //Create a ClientPrintJob var cpj = new JSPM.ClientPrintJob(); //Set Printer type (Refer to the help, there many of them!) if ($('#useDefaultPrinter').prop('checked')) { cpj.clientPrinter = new JSPM.DefaultPrinter(); } else { cpj.clientPrinter = new JSPM.InstalledPrinter($('#installedPrinterName').val()); } //Set printer commands... cpj.binaryPrinterCommands = new Uint8Array(xhr.response); //Send print job to printer! cpj.sendToClient(); } } xhr.send(); } } </script>
- That's it! Run your website and test it. Click on Print Now... to print the Raw Printer Commands without print dialog. You can print it to the Default client printer or you can get a list of the installed printers available at the client machine.
NOTE
You can also print directly to any LPT Parallel Port, RS232 Serial Port or IP/Ethernet Printer although these scenarios have not been considered in this article for simplicity. For further details on those scenarios, please contact our tech support.