How to generate and download barcode with jQuery free

By using JsBarcode JavaScript library we will generate barcode and this is a customizable barcode generator which is helpful to create your barcode. You can also download this barcode in PNG format and you can use that barcode image wherever you want.

Generate your own barcode

Step 1: Create a html file

<!DOCTYPE html>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   Include CSS and JS library
    <title>Barcode Generator</title>
	<!-- Barcode HTML Code -->	
	<!-- Barcode Generate and download Code -->

Step 2 : Include the library files in the head tag

	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="">
	<script src="" ></script>	
	<script src=""></script>
	<!-- JsBarcode library -->
	<script src="[email protected]/dist/JsBarcode.all.min.js"></script>
	<title>Barcode Generator</title>

In above code we have included Bootstrap library for designing your web page and here we also include jQuery to make code simple.

The most important JsBarcode JavaScript library to generate barcode for the web.

Step 3: Design your web page

<div class="container">	
  <h4>Barcode Generator</h4>
  <div class="container">	
	  <div class="row">
		<div class="col-lg-4">
			<label>Barcode text</label> 
		    <input type="text" class="form-control" id="input" placeholder="Enter the data">
		    <input type="button" class="btn btn-success"  onclick="generateBarcode()" value="Generate">
		    <input type="button" class="btn btn-success"  onclick="downloadBarcode()" value="Download">
	  <div class="col-lg-8" align="center">		
	  	<svg style="display:none" id="barcode"></svg> 

In above code we have created one textbox where we will take user input and based on that we will generate barcode. Here also we take two button one is Generate and second is Download.

When you click on Generate button we will can one JavaScript function generateBarcode() to generate barcode and that barcode will generate in SVG format. Once your barcode generated and we need to download that barcode in image format then we will call downloadBarcode() JavaScript function.

Step 4: Create a JavaScript function which will create the barcode based on the user input

    function generateBarcode() {
      var input = document.getElementById("input").value;
      var barcode = document.getElementById("barcode");
	  if (input=='')
		alert("<<!-- Provide  the data first --!>> ");
		return false;
      JsBarcode(barcode, input); = "inline";

In above code we will textbox value and that value pass into the JsBarcode() method and after that we will display that barcode.

Step 5: Download barcode

	function downloadBarcode(e){
	  const canvas = document.createElement("canvas");
	  const svg = document.getElementById("barcode");
	  const base64doc = btoa(unescape(encodeURIComponent(svg.outerHTML)));
	  const w = parseInt(svg.getAttribute('width'));
	  const h = parseInt(svg.getAttribute('height'));
	  const img_to_download = document.createElement('img');
	  img_to_download.src = 'data:image/svg+xml;base64,' + base64doc;
	  console.log(w, h);
	  img_to_download.onload = function () {
	    console.log('img loaded');
	    canvas.setAttribute('width', w);
	    canvas.setAttribute('height', h);
	    const context = canvas.getContext("2d");
	    //context.clearRect(0, 0, w, h);
	    const dataURL = canvas.toDataURL('image/png');
	    if (window.navigator.msSaveBlob) {
	      window.navigator.msSaveBlob(canvas.msToBlob(), "download.png");
	    } else {
	      const a = document.createElement('a');
	      const my_evt = new MouseEvent('click'); = 'download.png';
	      a.href = dataURL;

Here we created one function to download your barcode which is generated in SVG format. When you click on Download button at that time above JavaScript code will used.

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply