How to Scan Documents & Images from Javascript
Product JSPrintManager Published 05/15/2020 Updated 11/05/2026 Author Neodynamic
Overview
JSPrintManager enables 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) to scan documents and images through any TWAIN/SANE compatible devices available at the client side by writing pure and simple Javascript code!
In this walkthrough, you'll learn how to scan documents and images from Javascript through any TWAIN/SANE client devices without displaying any system dialogs at all. You'll be able to specify the target scanner device, the resolution and output quality (dpi) and desired pixel mode (Grayscale or full Color) as well as the output image format (JPG/JPEG, PNG, TIFF or PDF), and use the document feeder (ADF) and duplex scanning when the device supports them. This solution works with any popular browser like Chrome, Edge, Firefox & Safari on Windows, Linux, Raspberry Pi and Mac systems!
Step-by-step
-
Client System Requirements
- Windows: None!
- macOS/OSX: None!
- Linux: Scanner Access Now Easy (SANE) API
-
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 that will scan 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.READ CAREFULLY - Windows 64-bit Clients
If you install the JSPrintManager client app in a Windows 64-bit edition through our universal installer or the one specific for Win64 and your scanner does not provide a 64-bit TWAIN driver or it supports the TWAIN 1.x specification only, then it's possible that no scanners will be detected! If this is your case, then do the following:
- Uninstall current JSPrintManager app for 64-bit
- Download and install JSPrintManager app for 32-bit instead. The JSPrintManager app for 32-bit will detect not only those missing TWAIN devices but also any other WIA devices that might be available in your system.
-
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>Scan Docs and Images from Javascript</h1> <hr /> <label>Scanner: <select id="scannerName"></select></label> <label>Resolution (DPI): <input type="number" id="resolution" value="200" /></label> <label>Pixel mode: <select id="pixelMode"> <option>Color</option> <option>Grayscale</option> <option>BlackAndWhite</option> </select> </label> <label>Output format: <select id="imageFormat"> <option>JPG</option> <option>PNG</option> <option>TIFF</option> <option>PDF</option> </select> </label> <br /> <label><input type="checkbox" id="enableFeeder" /> Use document feeder (ADF)</label> <label>Pages to scan from the feeder (0 = all): <input type="number" id="feederCount" value="0" min="0" /></label> <label><input type="checkbox" id="enableDuplex" /> Duplex</label> <label><input type="checkbox" id="enableDecodeBarcode" /> Decode barcodes <em>(Windows only)</em></label> <label><input type="checkbox" id="showUI" /> Show the scanner UI <em>(Windows only)</em></label> <hr /> <button type="button" onclick="doScanning();">Scan Now...</button> <div id="scanOutput"></div> </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(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (jspmWSStatus()) { // get the client scanners JSPM.JSPrintManager.getScanners().then(function (scannersList) { fillSelect('scannerName', scannersList); }).catch(function (err) { console.error(err); }); } }; // Do scanning... function doScanning() { if (jspmWSStatus()) { // Create a ClientScanJob var csj = new JSPM.ClientScanJob(); // Set the scanning settings csj.scannerName = val('scannerName'); csj.resolution = parseInt(val('resolution')); csj.pixelMode = JSPM.PixelMode[val('pixelMode')]; csj.imageFormat = JSPM.ScannerImageFormatOutput[val('imageFormat')]; csj.enableFeeder = checked('enableFeeder'); csj.feederCount = parseInt(val('feederCount')); csj.enableDuplex = checked('enableDuplex'); csj.enableDecodeBarcode = checked('enableDecodeBarcode'); csj.showUI = checked('showUI'); csj.showProgressUI = true; // Other settings: csj.jpgCompressionQuality, csj.tiffCompression, csj.pngCompression, csj.pdfTitle, csj.rotAngle, csj.dither document.getElementById('scanOutput').innerHTML = ''; // Get the scanned images (one call per scanned page) csj.onUpdate = function (data, last) { if (!(data instanceof Blob)) { // status info and detected barcodes (if enabled) if (data.barcodes) { data.barcodes.forEach(function (bc) { alert('Barcode Detected: ' + bc.type + '\nBarcode Data: ' + bc.data); }); } console.info(data); return; } var imgBlob = new Blob([data]); if (imgBlob.size == 0) return; var format = val('imageFormat'); if (format == 'TIFF' || format == 'PDF') { // download TIFF & PDF outputs saveBlob(imgBlob, format == 'TIFF' ? 'output_scan.tif' : 'output_scan.pdf'); } else { // display JPG & PNG outputs var img = document.createElement('img'); img.src = URL.createObjectURL(imgBlob); img.style.width = Math.round(96.0 / csj.resolution * 100.0) + '%'; document.getElementById('scanOutput').appendChild(img); } }; csj.onError = function (data, isCritical) { console.error(data); }; // Send the scan job to the client scanner csj.sendToClient().then(function (data) { console.info(data); }); } } // Download a Blob as a file function saveBlob(blob, fileName) { var a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = fileName; document.body.appendChild(a); a.click(); document.body.removeChild(a); } // 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. Click on Scan Now... to start the scanning process without any system dialog.
Useful? Share it!
| Tweet |
|
Related Articles
Advanced Silent PDF Printing with Tray, Paper Size, Rotation & Pages Range Settings from JavascriptHow to detect virtual & real-physical printers from Javascript
JSPrintManager - Browsers Local Network Access (LNA) / Apps and Devices Popup
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.
