To generate bar codes from ERPNext JsBarcode can be very helpful. Sharing the code snippet so that it is useful for anyone that has a similar need.
The below example is based on the bar code symbology code128. The fields starting with “custom_” are the custom fields you may have to add on your Doctype based on your requirement.
frappe.ui.form.on ("Sales Invoice", "event_name", function (frm, cdt, cdn) {
var cur_doc = frm.doc;
if (cur_doc.custom_barcode_generated === 0) {
if(cur_doc.custom_bar_code){
$(frm.fields_dict['custom_barcode_image'].wrapper).html('<svg id="code128"></svg>');
$.getScript("/files/Path to your JSBarcode file", function( data, textStatus, jqxhr ) {
JsBarcode("#code128", cur_doc.custom_bar_code, {
background: "#FFFFFF"
});
var svg = $('#code128').parent().html();
frappe.model.set_value(cur_doc.doctype, cur_doc.name, "custom_barcode_svg", svg);
frappe.model.set_value(cur_doc.doctype, cur_doc.name, "custom_barcode_generated", 1);
cur_frm.save();
});
} else
{
frappe.msgprint(__("Please enter Barcode to to be printed"));
}
}
});
After this you can use the custom SVG field on your print format to generate the barcoded image
Note: The custom script is dependent on Jquery