Update: this is now a functioning example with issue fixed
Auto generating barcode:
import random
def validate_barcode(self):
if self.barcode:
if self.barcode == "auto":
existing = frappe.db.sql_list("select barcode from tabItem")
self.barcode = random.choice([item for item in map(str, range(1000,10000)) if item not in existing])
..
Customize Form Item:
New Print Format HTML:
<table class="custom">
<tr>
<td class="papa">
<p class="text-center" style="font-weight:bold; margin-bottom:4px;">
Siddhagiri's Satvyk<sup>TM</sup>
</p>
<p style="font-size:8pt;">{{ doc.item_name }}</p>
<svg class="barcode"></svg>
<p style="font-weight:bold;">MRP Rs. {{ doc.standard_rate }}</p>
<p id="pkd-date" style="font-size:7pt;">Pkd. Date: {{ doc.packed_date }}</p>
<p id="best-before" style="font-size:7pt;">Best before: {{ doc.best_before }}</p>
</td>
</tr>
</table>
<script src="https://cdn.jsdelivr.net/jsbarcode/3.5.1/JsBarcode.all.min.js"></script>
<script type="text/javascript">
if ( {{ doc.barcode }} > 1000000000000 ) {
JsBarcode(".barcode", "{{ doc.barcode }}", {
format: "EAN13",
height: 35,
fontSize: 12,
textAlign: "left",
lineColor: "#36414c",
width: 1,
margin: 0
});
}
if ( {{ doc.barcode }} <= 1000000000000 && {{ doc.barcode }} > 100000000000 ) {
JsBarcode(".barcode", "{{ doc.barcode }}", {
format: "UPC",
height: 35,
fontSize: 12,
textAlign: "left",
lineColor: "#36414c",
width: 1,
margin: 0
});
}
if ( {{ doc.barcode }} <= 100000000000 ) {
JsBarcode(".barcode", "{{ doc.barcode }}", {
height: 35,
fontSize: 12,
textAlign: "left",
lineColor: "#36414c",
width: 2,
displayValue: false,
margin: 0
});
}
if ('{{ doc.packed_date }}' == 'None'){
$('#pkd-date').hide();
}
if ('{{ doc.best_before }}' == 'None'){
$('#best-before').hide();
}
$('.papa').clone().insertAfter('.papa');
</script>
New Print Format CSS:
.print-format table, .print-format tr,
.print-format td, .print-format div, .print-format p {
vertical-align: middle;
}
@media screen {
.print-format {
width: 104mm;
min-height: 40mm;
height: 40mm;
padding: 0;
}
}
.custom {
width:100%;
border:none;
}
.custom td {
width : 50%;
height : 40mm;
padding : 2mm !important;
}
p sup{
opacity: 0.7;
}
.print-format p {
margin: 0px;
}
EDIT: End Result
Preview:

