How to generate barcode (node for barcode generator API in JavaScript and PDF.co Web API

Learn how to generate barcode (node to have barcode generator API in JavaScript

These source code samples are listed and grouped by their programming language and functions they use. Barcode generator API in JavaScript can be implemented with PDF.co Web API. PDF.co Web API is the Rest API that provides set of data extraction functions, tools for documents manipulation, splitting and merging of pdf files. Includes built-in OCR, images recognition, can generate and read barcodes from images, scans and pdf.

JavaScript code snippet like this for PDF.co Web API works best when you need to quickly implement barcode generator API in your JavaScript application. For implimentation of this functionality, please copy and paste code below into your app using code editor. Then compile and run your app. You can use these JavaScript sample examples in one or many applications.

PDF.co Web API – free trial version is on available our website. Also, there are other code samples to help you with your JavaScript application included into trial version.

Try PDF.co Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

GenerateBarcode.js
      
var https = require("https"); var path = require("path"); var fs = require("fs"); // The authentication key (API Key). // Get your own by registering at https://app.pdf.co/documentation/api const API_KEY = "***********************************"; // Result image file name const DestinationFile = "./barcode.png"; // Barcode type. See valid barcode types in the documentation https://app.pdf.co/documentation/api/1.0/barcode/generate.html const BarcodeType = "Code128"; // Barcode value const BarcodeValue = "qweasd123456"; // Prepare request to `Barcode Generator` API endpoint var queryPath = `/v1/barcode/generate?name=${path.basename(DestinationFile)}&type=${BarcodeType}&value=${BarcodeValue}&async=True`; var reqOptions = { host: "api.pdf.co", path: encodeURI(queryPath), headers: { "x-api-key": API_KEY } }; // Send request https.get(reqOptions, (response) => { response.on("data", (d) => { // Parse JSON response var data = JSON.parse(d); if (data.error == false) { console.log(`Job #${data.jobId} has been created!`); checkIfJobIsCompleted(data.jobId, data.url); } else { // Service reported error console.log(data.message); } }); }).on("error", (e) => { // Request error console.error(e); }); function checkIfJobIsCompleted(jobId, resultFileUrl) { let queryPath = `/v1/job/check?jobid=${jobId}`; let reqOptions = { host: "api.pdf.co", path: encodeURI(queryPath), method: "GET", headers: { "x-api-key": API_KEY } }; https.get(reqOptions, (response) => { response.on("data", (d) => { response.setEncoding("utf8"); // Parse JSON response let data = JSON.parse(d); console.log(`Checking Job #${jobId}, Status: ${data.status}, Time: ${new Date().toLocaleString()}`); if (data.status == "working") { // Check again after 3 seconds setTimeout(function(){ checkIfJobIsCompleted(jobId, resultFileUrl);}, 3000); } else if (data.status == "success") { // Download image file var file = fs.createWriteStream(DestinationFile); https.get(resultFileUrl, (response2) => { response2.pipe(file) .on("close", () => { console.log(`Generated image file saved as "${DestinationFile}" file.`); }); }); } else { console.log(`Operation ended with status: "${data.status}".`); } }) }); }

Try PDF.co Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

VIDEO

ON-PREMISE VERSION INFORMATION

Get 60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Get PDF.co Web API Free Training

WEB API

Get Your Free API Key

Explore Web API Documentation

Related Samples: