Available Now: Explore our latest release with enhanced accessibility and powerful IDP features
By Roger Dunham | 2024 Nov 29
6 min
Tags
node.js
apryse sdk
Summary: Barcodes are a cornerstone of modern business operations, enabling seamless inventory management, tracking, and automation. With the help of Node.js and an Apryse SDK, you can implement robust solutions to detect and decode barcodes, even from low-quality images. This capability is particularly valuable in aviation, where barcodes on baggage tags, cargo labels, or boarding passes may be smudged, crumpled, or partially damaged during transit.
Barcodes, whether 1D (the traditional sort) or 2D (for example the ‘QR’ code), are widely used in many industries. An obvious example is in retail where they allow scanning of groceries at the supermarket, or in healthcare where, among other uses, they identify and track specimens when they go for testing. In this article, though, we will focus on aviation, where barcodes are routinely used on boarding cards, as well as for labelling luggage and freight.
In particular, we are going to look at how we can detect and decode barcodes on boarding passes. We will do that using the Apryse SDK v11.0 and its specialized Barcode module.
What better PDF to use for testing barcodes than the IATA Bar Coded Boarding Pass implementation guide? It’s a 56-page document that contains a number of barcode samples.
Figure 1 - An example of a barcode from page 44 of the sample document.
The document allows us to see some of the great results that you can get with the Barcode Module, but also illustrates how the quality of the image can affect the output.
The Apryse SDK is available for many languages and frameworks, including C++, Java, C#, Go, Ruby, and PHP. In this example though, we will use the framework with Node.js
I’m using Node 18 on a Windows 11 machine, but the Apryse SDK also works with macOS and Linux.
Create a new folder for your project and navigate into it, then within a terminal window create a new app using:
node init
This will scaffold the project, and you will need to make some choices during the process. For now, though, you can just use the default options.
Create a file called “index.js” (or whatever name you specified as the main entry point when creating the project).
Next you need to install the Apryse SDK, using
npm i @pdftron/pdfnet-node
That will download the Apryse SDK, which provides support for a host of document processing functionality including PDF creation, redaction and annotation. However, when working with barcodes, it is necessary to use a dedicated add-on module. Other modules exist including those for working with CAD, OCR, Data Extraction, and for converting from PDF to Word.
Download the barcode module (the link will be different if you are using macOS or Linux) and extract it.
In my case I am extracting the zipped module into a folder called lib within my project.
Finally, copy the PDF that we will process into a folder called files. (If you prefer then you could refer to the sample file via a URL).
Your project should now look something like this:
Figure 2 - Typical layout of a node.js sample project
Now drop this code into the file index.js:
const { PDFNet } = require('@pdftron/pdfnet-node');
const main = async () => {
await PDFNet.addResourceSearchPath('lib/');
if (!await PDFNet.BarcodeModule.isModuleAvailable()) {
console.log('\nUnable to run Barcode extraction: Apryse SDK Barcode module not available.');
console.log('---------------------------------------------------------------');
}
const doc = await PDFNet.PDFDoc.createFromUFilePath('files/2021_03_02-bcbp-implementation-guide-version-7-.pdf')
await PDFNet.BarcodeModule.extractBarcodes(doc, 'output.json');
};
// add your own license key as the second parameter, e.g. in place of
PDFNet.runWithCleanup(main, '[Your license key]')
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
})
.then(function () {
PDFNet.shutdown();
});
It’s important to make sure that PDFNet.addResourceSearchPath points to the folder where you downloaded the barcode module.
The Apryse SDK needs a license key, but it's free to get a trial one. Copy the key into the function PDFNet.runWithCleanup which was included in the previous code.
That’s it, just save the file then run the code:
Node index.js
It may take a few seconds for the processing to complete – it is doing a lot of work - searching every page for a range of different bar code types. When it finishes, a new file will be created called, in this case, “output.json”.
What you do with that file depends on your use-case. You could use it as input for further processing or store it in a database for example. For now, though, we will just open it in Notepad++ and format the output using the JSON Viewer plugin.
Figure 3 - The start of the generated JSON file - there are no barcodes detected on the first 3 pages.
Apparently, there was nothing in the first 3 pages! If we look at the sample PDF, we will see that it is correct. There are no barcodes present in the first 3 pages. The JSON includes information about the page, even if no barcodes are found, letting us know that it searched for them but found nothing.
Figure 4 - The first four pages of the sample document - there are no barcodes present.
Further down the JSON file you will see that on page 10 a barcode was found.
Figure 5 - A barcode was detected on page 10
The data shows us the type of barcode, the text it contained and where it was on the page relative to the top left corner of the page.
And that’s exactly what we find on that page in the PDF.
Figure 6 - The PDF417 barcode on the page – seen in Apryse WebViewer. The values shown for 'rect' in the JSON file match the location of the barcode.
A little further down the JSON file is information for another barcode, in this case the Aztec type.
Figure 7 - An example of an Aztec code
Figure 8 - the data detected for the Aztec code
That’s pretty cool!
The Apryse Barcode module supports more than 30 types, including all of the commonly used ones.
You can test all of those using barcodes.pdf that is included in samples within the Barcode Module archive.
Figure 9 - The barcode module zip file includes a sample document containing many different barcode types.
The Apryse SDK does a great job at detecting and decoding barcodes, but this gets more difficult if the barcode is very small, poor quality, or skewed.
As an example, look at this sample taken from page 39 of the “Barcode Boarding Pass Gude” document that we have been using.
Figure 10 - An example PDF147 barcode.
If you zoom in to document and look at the detail then you can see that the barcode (and the text) is extremely fuzzy, and the barcode which should only have black and white cells contains many grayscale ones. This was caused by the way in which the document was scanned as a “lossy jpg”.
Figure 11 - Zooming into the image reveals that the barcode is fuzzy with not just black and white cells, but also a range of gray ones.
As a result, the barcode can’t be decoded using the default options - simply because the quality is so poor.
The best solution, by far, for this problem is to get better quality images.
Improving scan quality may be outside of your control, in which case you may wish to try specifying an Extraction Profile. These tweak the processing to work with low-quality and very small scans, and also for barcodes included in Natural Pictures of real-world objects.
const options = new PDFNet.BarcodeModule.BarcodeOptions();
options.setBarcodeProfile( PDFNet.BarcodeModule.BarcodeOptions.BarcodeProfile.e_low_quality_source_profile);
await PDFNet.BarcodeModule.extractBarcodes(doc, 'barcodes.json', options);
While this may allow the barcode to be extracted, using an Extraction Profile requires more processing and may extend the time taken to process the document.
If using an Extract Profile doesn't help, then contact us via our Discord channel - other profiles that exist which are not yet publicly available.
In this article, we have looked primarily at barcodes in aviation, but their use is ubiquitous across many sectors.
The Apryse SDK was already a superb tool for working with many kinds of documents. The release of the barcode SDK adds further best-in-class functionality allowing you to accurately, and quickly, extract data stored in barcodes, whatever industry you are in.
Get yourself a trial license and try things out. There is extensive documentation on the entire SDK to help you, but feel free to contact us on Discord.
Tags
node.js
apryse sdk
Roger Dunham
Share this post
PRODUCTS
Enterprise
Small Business
Popular Content