How to print raw PRN commands/files from Javascript
Product JSPrintManager Published 09/22/2020 Updated 11/05/2026 Author Neodynamic
Overview
A PRN file contains raw commands instructions for a printer which are usually created by a printer driver. 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.
In this walkthrough, you'll learn how to print raw PRN commands from Javascript directly to the client printer without displaying a print dialog at all. You'll be able to print PRN commands to the Default client printer as well as to any other installed printer at the client machine. This solution works with any popular browser like Chrome, Firefox, Edge & Safari on Windows, Linux, Raspberry Pi or Mac systems!
Step-by-step
- 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!
Since Chrome 142, browsers ask the user to allow Local Network Access the first time your website connects to the JSPrintManager App. - By using your favorite Web Development IDE or Text Editor, create a new HTML file like index.html
Copy/paste the following code snippets:HTML Code
<div style="text-align:center"> <h1>Print PRN commands/files from Javascript</h1> <hr /> <label for="printerType">Client printer:</label> <select id="printerType" onchange="showPrinterSettings();"> <option value="0">Default printer</option> <option value="1">Let the user select it (OS print dialog)</option> <option value="2" selected>Installed printer</option> <option value="3">IP/Ethernet printer</option> <option value="4">Parallel port (LPT) printer</option> <option value="5">Serial port (RS-232) printer</option> <option value="6">Bluetooth printer</option> </select> <div id="printer-settings-2" class="printer-settings"> <label>Installed printer: <select id="installedPrinterName"></select></label> </div> <div id="printer-settings-3" class="printer-settings" style="display:none"> <label>IP address: <input id="netIp" value="10.0.0.1" /></label> <label>Port: <input id="netPort" value="9100" /></label> <label>or DNS name: <input id="netDns" /></label> </div> <div id="printer-settings-4" class="printer-settings" style="display:none"> <label>Parallel port: <input id="lptPort" value="LPT1" /></label> </div> <div id="printer-settings-5" class="printer-settings" style="display:none"> <label>Serial port: <select id="serialPort"></select></label> <label>Baud rate: <input id="serialBaudRate" value="9600" /></label> <label>Data bits: <select id="serialDataBits"> <option value="Eight">8</option> <option value="Seven">7</option> <option value="Six">6</option> <option value="Five">5</option> </select> </label> <label>Parity: <select id="serialParity"> <option>None</option> <option>Odd</option> <option>Even</option> <option>Mark</option> <option>Space</option> </select> </label> <label>Stop bits: <select id="serialStopBits"> <option value="One">1</option> <option value="OnePointFive">1.5</option> <option value="Two">2</option> </select> </label> <label>Flow control: <select id="serialFlowControl"> <option value="None">None</option> <option value="XOnXOff">XOn/XOff</option> <option value="RequestToSend">RTS (Request to send)</option> <option value="RequestToSendXOnXOff">RTS + XOn/XOff</option> </select> </label> </div> <div id="printer-settings-6" class="printer-settings" style="display:none"> <label>Bluetooth address: <input id="btAddress" placeholder="00:00:00:00:00:00" /></label> <label>Channel: <input id="btChannel" value="1" /></label> </div> <br /><br /> <button type="button" onclick="doPrinting();">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 then add the following script references to your page.
<!-- JSPrintManager.js (download it or install it with: npm install jsprintmanager) --> <script src="JSPrintManager.js"></script>Script Code
<script> // Start the connection with the JSPrintManager Client App JSPM.JSPrintManager.auto_reconnect = true; JSPM.JSPrintManager.start(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (jspmWSStatus()) { // get the client installed printers JSPM.JSPrintManager.getPrinters().then(function (printersList) { fillSelect('installedPrinterName', printersList); }); // get the client serial ports (if any) JSPM.JSPrintManager.getSerialPorts().then(function (portsList) { fillSelect('serialPort', portsList); }); } }; // Do printing... function doPrinting() { if (jspmWSStatus()) { // ###### IMPORTANT ################################################ // CHANGE the URL/path of the PRN file in the following line of code // ################################################################# fetch('/the-printer-commands.prn') .then(function (response) { if (!response.ok) throw new Error('HTTP error ' + response.status); return response.arrayBuffer(); }) .then(function (buffer) { // Create a ClientPrintJob var cpj = new JSPM.ClientPrintJob(); // Set the target client printer cpj.clientPrinter = getClientPrinter(); // Set the raw printer commands as bytes (sent to the printer without any changes) cpj.binaryPrinterCommands = new Uint8Array(buffer); // Trace the print job status (optional) traceJob(cpj); // Send the print job to the client printer cpj.sendToClient(); }) .catch(function (err) { console.error(err); }); } } // Check the connection status with the JSPrintManager Client App function jspmWSStatus() { var status = JSPM.JSPrintManager.websocket_status; if (status == JSPM.WSStatus.Open) { return true; } else if (status == JSPM.WSStatus.WaitingForUserResponse) { console.info('Waiting for the user to allow this website (JSPrintManager dialog or browser Local Network Access prompt)...'); } else if (status == JSPM.WSStatus.Blocked) { alert('JSPrintManager has blocked this website!'); } else if (status == JSPM.WSStatus.NotInstalled || status == JSPM.WSStatus.Closed) { alert('JSPrintManager Client App is not installed or not running! Download it from https://neodynamic.com/downloads/jspm'); } else if (status == JSPM.WSStatus.CertificateError) { alert('A TLS/SSL certificate error prevented the connection with JSPrintManager Client App.'); } else if (status == JSPM.WSStatus.ConnectionError) { alert('A network error (e.g. a firewall) prevented the connection with JSPrintManager Client App.'); } return false; } // Show the settings of the selected printer type function showPrinterSettings() { var panels = document.querySelectorAll('.printer-settings'); for (var i = 0; i < panels.length; i++) { panels[i].style.display = (panels[i].id == 'printer-settings-' + val('printerType')) ? '' : 'none'; } } // Create the client printer for the selected printer type function getClientPrinter() { switch (val('printerType')) { case '0': return new JSPM.DefaultPrinter(); case '1': return new JSPM.UserSelectedPrinter(); case '2': return new JSPM.InstalledPrinter(val('installedPrinterName')); case '3': return new JSPM.NetworkPrinter(parseInt(val('netPort')), val('netIp'), val('netDns')); case '4': return new JSPM.ParallelPortPrinter(val('lptPort')); case '5': return new JSPM.SerialPortPrinter(val('serialPort'), parseInt(val('serialBaudRate')), JSPM.Serial.Parity[val('serialParity')], JSPM.Serial.StopBits[val('serialStopBits')], JSPM.Serial.DataBits[val('serialDataBits')], JSPM.Serial.Handshake[val('serialFlowControl')]); case '6': return new JSPM.BluetoothPrinter(val('btAddress'), parseInt(val('btChannel'))); } } // Log the print job status updates (optional) function traceJob(cpj) { cpj.onUpdated = function (data) { console.info(data); }; cpj.onFinished = function (data) { console.info(data); }; cpj.onError = function (data, isCritical) { console.error(data); }; } // Helpers function val(id) { return document.getElementById(id).value; } function checked(id) { return document.getElementById(id).checked; } // Fill a <select> element with a list of items function fillSelect(id, items, getText) { var select = document.getElementById(id); select.innerHTML = ''; for (var i = 0; i < items.length; i++) { var text = getText ? getText(items[i]) : items[i]; select.add(new Option(text, text)); } } </script> - That's it! Run your website and test it. Click on Print Now... to print the PRN Commands without print dialog. You can print it to the Default client printer, to any installed printer available at the client machine, or directly to an IP/Ethernet, LPT, RS-232 or Bluetooth printer.
NOTE Besides the Default printer and any installed printer, this sample also lets you print through the OS print dialog and directly to an IP/Ethernet printer (e.g. port 9100), a Parallel Port (LPT), a Serial Port (RS-232) or a Bluetooth printer.
Useful? Share it!
| Tweet |
|
Related Articles
How to print raw commands from Web APIs, WebServices, Remote and Local files with JavascriptHow to configure RAW printing mode in Windows
How to configure RAW printing mode in Linux and Raspberry Pi
How to configure RAW printing mode in macOS and OSX
Need help? Contact Support
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.