Reputation: 411
Trying to solve a problem linked to bar-codes in html. What i need to do is to create a template of label with bar-code in it and send to printer. I choosed to use html language to create that template with input data. I could get that html and it looks like this in picture.
Howerer, then i tried to print that html i got that label but without bar-code printed. So i tried to convert it to pdf and send to printer, but whenever i tried to convert to pdf, i get in converted pdf that label without bar-codes.
Tried to use some different libraries (
pdfkit,pdfcrowd,PyQt4.QtGui
), none saved pdf with barcode. Attaching script:
for index, row in output_ramco.iterrows():
print row['PARTNOMLT_OUT']
htmlas = '''
<html><head>
<meta name="author" content="VS">
<meta name="description" content="include bar code type code39 in html files with javascript">
<meta name="keywords" content="code39, javascript">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" src="code39.js"></script>
</head>
<body style="font-family:Verdana; font-size:11pt">
<style type="text/css" media = "print">
@page
{
size: auto; /* auto is the current printer page size */
margin: 3mm; /* this affects the margin in the printer settings */
}
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:8px;padding:5px 4px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:8px;font-weight:normal;padding:5px 4px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;}
.tg .tg-9hbo{font-weight:bold;vertical-align:top}
.tg .tg-yw4l{vertical-align:top}
</style>
<script language="JavaScript">
document.open();
Code39("12","175",20,1800,"'''+str(row['PARTNOMLT_OUT'])+'''");
Code39("12","197",20,1800,"'''+str(row['LOTSN'])+'''");
document.close();
</script>
<table class="tg" cellspacing="0" cellpadding="0">
<tr>
<td class="tg-9hbo">PN:</td>
<td class="tg-yw4l" colspan="2">'''+str(row['PARTNOMLT_OUT'])+'''</td>
<td class="tg-9hbo">QTY:</td>
<td class="tg-yw4l">'''+str(row['KIEKIS'])+'''</td>
</tr>
<tr>
<td class="tg-yw4l" colspan="5">'''+str(row['PARTDESCMLT_OUT'])+'''</td>
</tr>
<tr>
<td class="tg-9hbo">LOT/SN#:</td>
<td class="tg-yw4l" colspan="4">'''+str(row['LOTSN'])+'''</td>
</tr>
<tr>
<td class="tg-9hbo">Stock Status:</td>
<td class="tg-yw4l" colspan="4">'''+row['STATUSMLT_OUT']+'''</td>
</tr>
<tr>
<td class="tg-9hbo">Shelf Life Expiry Dt.:</td>
<td class="tg-yw4l" colspan="4">'''+row['EXP_DATE']+'''</td>
</tr>
<tr>
<td class="tg-9hbo">Certificate Type/No:</td>
<td class="tg-yw4l" colspan="4">'''+str(row['CERTAI'])+'''</td>
</tr>
<tr>
<td class="tg-9hbo">Source Document #</td>
<td class="tg-yw4l" colspan="4">'''+str(row['SOURCE_DOC'])+'''</td>
</tr>
<tr>
<td class="tg-9hbo">Storage Area:</td>
<td class="tg-yw4l" colspan="4">'''+str(row['STORAGE'])+'''</td>
</tr>
<tr>
<td class="tg-9hbo">Owner # :</td>
<td class="tg-yw4l" colspan="4">'''+row['OWNERIS']+'''</td>
</tr>
<tr>
<td class="tg-yw4l" colspan="5">11</td>
</tr>
<tr>
<td class="tg-yw4l" colspan="5">11</td>
</tr>
<tr>
<td class="tg-yw4l">EIA Issue 2</td>
<td class="tg-9hbo">Generated On:</td>
<td class="tg-yw4l" colspan="3">'''+str(row['GENERATED'])+'''</td>
</tr>
</table>
</body>
</html>
'''
try:
with open(dir_files+'html_test.html', 'w') as file:
file.write(htmlas.encode('UTF-8'))
except Exception, e:
log_error()
######1st
#from PyQt4.QtGui import QTextDocument, QPrinter, QApplication
#import sys
#app = QApplication(sys.argv)
#doc = QTextDocument()
#location = "html_test.html"
#html = open(location).read()
#doc.setHtml(html)
#printer = QPrinter()
#printer.setOutputFileName("foo.pdf")
#printer.setOutputFormat(QPrinter.PdfFormat)
#printer.setPageSize(QPrinter.A4);
#printer.setPageMargins (15,15,15,15,QPrinter.Millimeter);
#doc.print_(printer)
######2nd
#import pdfcrowd
#client = pdfcrowd.Client("orangutangas", "pwd")
#output_file = open('htmlas.pdf', 'wb')
#client.convertHtml(htmlas, output_file)
#output_file.close()
#########3rd
#path_wkthmltopdf = r'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe'
#config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
#pdfkit.from_string(htmlas, 'out.pdf',configuration=config)
So how can i translate html or make a jpg/pdf where printer can read this info and print properly with barcode thjat label?
Upvotes: 1
Views: 8493
Reputation: 535
ReportLab is very good in this case. I am not sure where I found out this code. But it really helped me.
from reportlab.graphics.barcode import code39, code128, code93
from reportlab.graphics.barcode import eanbc, qr, usps
from reportlab.graphics.shapes import Drawing
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF
def createBarCodes():
"""
Create barcode examples and embed in a PDF
"""
c = canvas.Canvas("barcodes.pdf", pagesize=letter)
barcode_value = "1234567890"
barcode39 = code39.Extended39(barcode_value)
barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1)
# code93 also has an Extended and MultiWidth version
barcode93 = code93.Standard93(barcode_value)
barcode128 = code128.Code128(barcode_value)
# the multiwidth barcode appears to be broken
#barcode128Multi = code128.MultiWidthBarcode(barcode_value)
barcode_usps = usps.POSTNET("50158-9999")
codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps]
x = 1 * mm
y = 285 * mm
x1 = 6.4 * mm
for code in codes:
code.drawOn(c, x, y)
y = y - 15 * mm
# draw the eanbc8 code
barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value)
bounds = barcode_eanbc8.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
d = Drawing(50, 10)
d.add(barcode_eanbc8)
renderPDF.draw(d, c, 15, 555)
# draw the eanbc13 code
barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value)
bounds = barcode_eanbc13.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
d = Drawing(50, 10)
d.add(barcode_eanbc13)
renderPDF.draw(d, c, 15, 465)
# draw a QR code
qr_code = qr.QrCodeWidget('www.mousevspython.com')
bounds = qr_code.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
d = Drawing(45, 45, transform=[45./width,0,0,45./height,0,0])
d.add(qr_code)
renderPDF.draw(d, c, 15, 405)
c.save()
if __name__ == "__main__":
createBarCodes()
Upvotes: 3
Reputation: 405
Check out this project, which uses jinja2
, z3c.rml
, and reportlab
to generate PDFs with barcodes on them. All of these libraries can accomplish what you're trying to do, and you can send labels to the printer with pycups
.
Upvotes: 0