Search code examples
javascriptjquerybarcodejspdfhtml-to-pdf

Exporting numerical value as barcode in JSPDF


I have the following code:

var doc = new jsPDF('p','pt','a4');
var specialElementHandlers = {
  '#bypassme': function(element, renderer) {
    return true;
  }
};

doc.fromHTML(
  $('#generalDiv').html(), 10, 10,
  {
    'width': 250,'elementHandlers': specialElementHandlers
  }
);

doc.output('dataurl');
}

and My html is this one

<div id="generalDiv"> 
123456
</div>

Do you know if there is a method in jspdf plugin to render the nmbers as a barcode? Or do you know another way to do it?


Solution

  • I did it, the answer was to use jquery-barcode to generate the barcode, then use the html2canvas.js to convert the container div with the bar code to a canvas, and then convert it to an image and export it as PDF.

     html2canvas($("#testcase"), {
            onrendered: function(canvas) {
    
                var imgData = canvas.toDataURL(
                    'image/png');              
                var doc = new jsPDF('p', 'mm');
                doc.addImage(imgData, 'PNG', 0, 0);
                doc.save('REQUEST.pdf');
            }
        });