How to send/request IPP (Internet Printing Protocol) messages from Javascript
Product JSPrintManager Published 03/14/2024 Updated 11/05/2026 Author Neodynamic
Overview
JSPrintManager supports IPP (Internet Printing Protocol) Communication from Javascript allowing you to Send/Request IPP JSON messages to any IPP URI device and get IPP JSON response from the client system. Based on pure Javascript code, you can enable IPP Comm on 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).
In this walkthrough, you'll learn how to Send/Request IPP JSON messages to any IPP URI device and get IPP JSON response from Javascript. This solution works with any popular browser like Chrome, Edge, Firefox & Safari on Windows, Linux, Raspberry Pi, Android and Mac systems!
The sample code in this article will perform the following IPP tasks on the specified IPP Device URI:
- Get Printer Attributes: Will return an IPP JSON object containing all the printer attributes like name, supported papers, and many more!
- Get Completed Jobs: Will return an IPP JSON object containing all the completed jobs including the job ID, job Name, job State, etc.
- Print a Plain Text file: Will print a plain text content and will return an IPP JSON object containing the job ID and related into.
- Print a PDF file: Will print a PDF file content and will return an IPP JSON object containing the job ID and related into. IMPORTANT! The target printer must support printing PDF docs natively otherwise an error response will be issued back by the printer.
Step-by-step
-
JSPrintManager Client App
Be sure you install in your dev machine JSPrintManager (JSPM) (Available for Windows, Linux, Raspberry Pi, Android & Mac)
IMPORTANT: This small app must be installed on each client!
Since Chrome 142, browsers ask the user to allow Local Network Access the first time your website connects to the JSPrintManager App. -
HTML & Script References
- Download JSPrintManager.js
- Copy all of these *.js files to the same folder where the next
index.htmlpage 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>IPP Comm from Javascript</h1> <hr /> <label>IPP device URI: <input id="deviceUri" size="50" placeholder="ipp://10.0.0.1:631/ipp/print" /></label> <label>User name (optional): <input id="userName" /></label> <label>Password (optional): <input id="password" type="password" /></label> <p><small>IMPORTANT: The target IPP device URI must be in the client's allow list!</small></p> <hr /> <strong>Predefined IPP samples:</strong> <button type="button" onclick="setIppMessage(1);">Get Printer Attributes</button> <button type="button" onclick="setIppMessage(2);">Get Completed Jobs</button> <button type="button" onclick="setIppMessage(3);">Print a Plain Text</button> <button type="button" onclick="setIppMessage(4);">Print a PDF file</button> <br /><br /> <strong>IPP message (JSON)</strong> <textarea id="txtIppMessage" rows="8" cols="100"></textarea> <br /> <button type="button" onclick="doSendIppMessage();">Send IPP Message...</button> <hr /> <strong>IPP response (JSON)</strong> <textarea id="txtIppResponse" rows="12" cols="100" readonly></textarea> </div> <!-- JSPrintManager.js (download it or install it with: npm install jsprintmanager) --> <script src="JSPrintManager.js"></script> <script> // Start the connection with the JSPrintManager Client App JSPM.JSPrintManager.auto_reconnect = true; JSPM.JSPrintManager.start(); var ippSample = 0; // Create the IPP message for the selected sample function setIppMessage(sample) { var uri = val('deviceUri'); if (!uri) { alert('Please specify the IPP device URI!'); return; } ippSample = sample; var operationAttributes = [ { tag: JSPM.Ipp.CHARSET, name: 'attributes-charset', value: ['utf-8'] }, { tag: JSPM.Ipp.NATURAL_LANG, name: 'attributes-natural-language', value: ['en-us'] }, { tag: JSPM.Ipp.URI, name: 'printer-uri', value: [uri] } ]; var msg = { version: { major: 1, minor: 1 }, requestId: 1, groups: [] }; if (sample == 1) { // Get Printer Attributes msg.operationId = JSPM.Ipp.GET_PRINTER_ATTRIBUTES; operationAttributes.push({ tag: JSPM.Ipp.KEYWORD, name: 'requested-attributes', value: ['all'] }); msg.groups.push({ tag: JSPM.Ipp.OPERATION_ATTRIBUTES_TAG, attributes: operationAttributes }); } else if (sample == 2) { // Get Completed Jobs msg.operationId = JSPM.Ipp.GET_JOBS; operationAttributes.push({ tag: JSPM.Ipp.KEYWORD, name: 'which-jobs', value: ['completed'] }); operationAttributes.push({ tag: JSPM.Ipp.KEYWORD, name: 'requested-attributes', value: ['job-id', 'job-state', 'job-state-reasons', 'job-name', 'job-media-sheets-completed'] }); msg.groups.push({ tag: JSPM.Ipp.OPERATION_ATTRIBUTES_TAG, attributes: operationAttributes }); } else { // Print a plain text (3) or a PDF file (4) msg.operationId = JSPM.Ipp.PRINT_JOB; operationAttributes.push({ tag: JSPM.Ipp.NAME_WITHOUT_LANG, name: 'job-name', value: ['HelloWorldTest'] }); operationAttributes.push({ tag: JSPM.Ipp.MIME_MEDIA_TYPE, name: 'document-format', value: [sample == 4 ? 'application/pdf' : 'application/octet-stream'] }); msg.groups.push({ tag: JSPM.Ipp.OPERATION_ATTRIBUTES_TAG, attributes: operationAttributes }); msg.groups.push({ tag: JSPM.Ipp.JOB_ATTRIBUTES_TAG, attributes: [ { tag: JSPM.Ipp.INTEGER, name: 'copies', value: [1] }, { tag: JSPM.Ipp.KEYWORD, name: 'media', value: ['iso_a4_210x297mm'] } ] }); } document.getElementById('txtIppMessage').value = JSON.stringify(msg, null, 2); document.getElementById('txtIppResponse').value = ''; } // Send the IPP message function doSendIppMessage() { if (!jspmWSStatus()) return; if (!val('txtIppMessage')) { alert('Please click on any of the predefined IPP samples!'); return; } if (ippSample == 4) { // Print a PDF file: get its binary content first // IMPORTANT: The target printer must support printing PDF docs natively fetch('https://neodynamic.com/temp/LoremIpsum.pdf') .then(function (response) { return response.arrayBuffer(); }) .then(function (buffer) { sendIppMessage(new Uint8Array(buffer)); }); } else if (ippSample == 3) { // Print a plain text sendIppMessage(new TextEncoder().encode("Hello World - Printed by JSPrintManager through IPP!")); } else { sendIppMessage(null); } } function sendIppMessage(fileContent) { var msg = JSON.parse(val('txtIppMessage')); JSPM.JSPrintManager.sendIppMessage(val('deviceUri'), msg, fileContent, val('userName'), val('password')).then(function (resp) { var error = ''; if (resp._oprationIdOrStatusCode && resp._oprationIdOrStatusCode > 0) { error = 'ERROR!!! ' + JSPM.Ipp[resp._oprationIdOrStatusCode] + '\n\n'; } document.getElementById('txtIppResponse').value = error + JSON.stringify(resp, null, 2); console.info(resp); }).catch(function (err) { document.getElementById('txtIppResponse').value = JSON.stringify(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; } // 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> </body> </html>
- That's it! Run your website and test it.
Useful? Share it!
| Tweet |
|
Related Articles
How to send/write and receive/read (BIDI) TCP/IP Data from JavascriptAdvanced Silent PDF Printing with Tray, Paper Size, Rotation & Pages Range Settings from Javascript
How to trace Print Job Status from Javascript
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.