Hi everyone.
I do raw Item lable printing with a Zebra printer. For a source of the barcode information I use the custom_barcode variable. Unfortunately, the custom_barcode is not text, it’s a SVG image. If I switch the custom_barcode to Small Text, labels are printed OK , but there is no barcode preview in the Item. The question is, what variable can I use to get proper information of a barcode usable for ZPL of a Zebra printer?
Thank you in advance.
You might find this useful
Thank you for your reply.
I use this topic and it’s helpful, but there is no barcode information from Item form. I tried to use doc.barcodes[0].barcode, but it doesn’t work as well.
You could add a custom field to the Item DocType and pull the info from their if you can’t get it from the Barcode child table easily. Feed that to ZPL with the correct code and it should generate the barcode for you.
What code are you using to get the barcode from the barcode child table?
I use doc.custom_barcode but switched it from type Barcode to Small text.
What script are you using to retrieve the barcode value?
Is doc.custom_barcode on the item form and trying to get the barcode from the Item’s barcode child table?
Also what type of barcode are you trying to generate and how are you printing your existing labels?
frappe.ui.form.on(‘Item’, {
refresh: function(frm) {
frm.add_custom_button((“Print Label”), function() {
let d = new frappe.ui.Dialog({
title: ‘Print Labels on Zebra1’,
fields: [
{
label: ‘Number of Labels?’,
fieldname: ‘label_n’,
fieldtype: ‘Int’,
default: 1
}
],
primary_action_label: ‘Submit’,
primary_action(values) {
var item = frm.doc.item_code;
var barcode = frm.doc.custom_barcode;
if (!barcode) {
frappe.msgprint((“Custom Barcode field is empty. Please set a valid barcode value.”));
return;
}
var zpl = ^XA^CFB,20^FO10,15^FH_^FD${item}^FS^BY2,2^FO15,110^A0N,20,20^BCN,70^FD${barcode}^FS^PQ${values.label_n}^XZ
;
var ip_addr = “192.168.0.31”;
var url = http://${ip_addr}/pstprnt
;
var method = “POST”;
var async = true;
var request = new XMLHttpRequest();
request.open(method, url, async);
request.setRequestHeader("Content-Length", zpl.length);
request.send(zpl);
d.hide();
}
});
d.show();
});
}
});
Label size is 50x25mm.
Does ${ip_addr}, ${item} and ${barcode} actually return the values you expect using console.log etc?
Also I found that I needed to use double quotation marks to encapsulate the ZPL code.
Try this. I have removed the IF test for a blank barcode, just to make it easier to troubleshoot.
frappe.ui.form.on('Item', {
refresh: function(frm) {
frm.add_custom_button(('Print Label'), function() {
let d = new frappe.ui.Dialog({
title: 'Print Labels on Zebra1',
fields: [
{
label: 'Number of Labels?',
fieldname: 'label_n',
fieldtype: 'Int',
default: 1
}
],
primary_action_label: 'Submit',
primary_action(values) {
var item = frm.doc.item_code;
var barcode = frm.doc.custom_barcode;
var zpl = "^XA^CFB,20^FO10,15^FH_^FD"+item+"^FS^BY2,2^FO15,110^A0N,20,20^BCN,70^FD"+barcode+"^FS^PQ$"+label_n+"^XZ";
var ip_addr = "192.168.0.31";
var url = "http://"+ip_addr+"/pstprnt";
var method = "POST";
var async = true;
var request = new XMLHttpRequest();
request.open(method, url, async);
request.setRequestHeader("Content-Length", zpl.length);
request.send(zpl);
d.hide();
}
});
d.show();
});
}
});
I use Code128.
I can’t show you the full return right now, but at the end of a long message which starts from <svg height =…, there is the barcode data B-1-6-001.
Just setting up a new ERPNext system today, will try and get something similar setup later to test out the code again
Don’t bother yourself by Frappe printing. The Zebra designer gives you the txt file. Put in your variable fields in that file and import it as a doctype. This is clearly a string which you need to format using the data from your code. Use IP and Port number to send the data you have formatted to that printer.
Don’t use barcode fields on Erpnext at all if you’re going to let ZPL handle the conversion. Saves time, effort, space and offers more options.
This code is working on my setup. The printed label is a little bit smaller but that could be due to my printer being 300 dpi and maybe your printer is a 200 dpi model.
One thing to be wary of is to ensure the quotation marks are correct. When I copied your code over many of the quotation marks needed to be corrected.
frappe.ui.form.on('Item', {
refresh: function(frm) {
frm.add_custom_button(('Print Label'), function() {
let d = new frappe.ui.Dialog({
title: 'Print Labels on Zebra1',
fields: [
{
label: 'Number of Labels?',
fieldname: 'label_n',
fieldtype: 'Int'
}
],
primary_action_label: 'Submit',
primary_action(values) {
var item = frm.doc.item_code;
var barcode = frm.doc.custom_barcode;
if (!barcode) {
frappe.msgprint(('Custom Barcode field is empty. Please set a valid barcode value.'));
return;
}
var zpl = "^XA^CFB,20^FO10,15^FH_^FD"+item+"^FS^BY2,2^FO15,110^A0N,20,20^BCN,70^FD"+barcode+"}^FS^PQ"+values.label_n+"^XZ";
var ip_addr = "192.168.30.31";
var url = "http://"+ip_addr+"/pstprnt";
var method = "POST";
var async = true;
var request = new XMLHttpRequest();
request.open(method, url, async);
request.setRequestHeader("Content-Length", zpl.length);
request.send(zpl);
d.hide();
}
});
d.show();
}
);
}});
This is what doc.custom_barcode returns:
<svg height="88px" width="100%" x="0px" y="0px" viewBox="0 0 422 88" xmlns="http://www.w3.org/2000/svg" version="1.1" style="transform: translate(0,0)" data-barcode-value="B-1-6-002"><rect x="0" y="0" width="422" height="88" style="fill:#ffffff;"></rect><g transform="translate(10, 10)" style="fill:#000000;"><rect x="0" y="0" width="6" height="50"></rect><rect x="9" y="0" width="3" height="50"></rect><rect x="18" y="0" width="3" height="50"></rect><rect x="33" y="0" width="3" height="50"></rect><rect x="45" y="0" width="3" height="50"></rect><rect x="51" y="0" width="6" height="50"></rect><rect x="66" y="0" width="3" height="50"></rect><rect x="75" y="0" width="6" height="50"></rect><rect x="84" y="0" width="9" height="50"></rect><rect x="99" y="0" width="3" height="50"></rect><rect x="108" y="0" width="9" height="50"></rect><rect x="123" y="0" width="6" height="50"></rect><rect x="132" y="0" width="3" height="50"></rect><rect x="141" y="0" width="6" height="50"></rect><rect x="150" y="0" width="9" height="50"></rect><rect x="165" y="0" width="6" height="50"></rect><rect x="177" y="0" width="9" height="50"></rect><rect x="189" y="0" width="3" height="50"></rect><rect x="198" y="0" width="3" height="50"></rect><rect x="207" y="0" width="6" height="50"></rect><rect x="216" y="0" width="9" height="50"></rect><rect x="231" y="0" width="3" height="50"></rect><rect x="240" y="0" width="9" height="50"></rect><rect x="252" y="0" width="6" height="50"></rect><rect x="264" y="0" width="3" height="50"></rect><rect x="273" y="0" width="9" height="50"></rect><rect x="285" y="0" width="6" height="50"></rect><rect x="297" y="0" width="6" height="50"></rect><rect x="309" y="0" width="9" height="50"></rect><rect x="324" y="0" width="3" height="50"></rect><rect x="330" y="0" width="3" height="50"></rect><rect x="336" y="0" width="3" height="50"></rect><rect x="348" y="0" width="6" height="50"></rect><rect x="363" y="0" width="6" height="50"></rect><rect x="378" y="0" width="9" height="50"></rect><rect x="390" y="0" width="3" height="50"></rect><rect x="396" y="0" width="6" height="50"></rect><text style="font: 16px monospace" text-anchor="middle" x="201" y="68">B-1-6-002</text></g></svg>
One difference I can think of is that for the custom_barcode field I selected it to be a Data type field, rather than small text as you had previously done.
I entered B-1-6-001 in the custom_barcode field, as in your example, and it generated the barcode correctly. ZPL should take that value and create the label barcode for you without trying to convert it to an SVG first. Not sure why you are generating an SVG file.
I originally had a couple of console.log
parts in the script to check that the correct values for item_code and custom_barcode were being returned. Might be worth adding those in to see if you are getting the correct information from ERPnext to send to the printer.
Can you tell me more about your setup? What printer are you using, how it’s connected to your local network etc.