gpt4 book ai didi

python - 生成带条形码的数据标签(html或jpg或pdf)并打印它(现在不生成条形码)

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:43 27 4
gpt4 key购买 nike

尝试解决与 html 中的条形码相关的问题。我需要做的是创建一个带有条形码的标签模板并将其发送到打印机。我选择使用 html 语言来创建带有输入数据的模板。我可以得到那个 html,它看起来像图片中这样。 enter image description here然而,然后我尝试打印该 html,我得到了该标签,但没有打印条形码。所以我尝试将其转换为 pdf 并发送到打印机,但每当我尝试转换为 pdf 时,我都会得到转换后的 pdf,该标签没有条形码。尝试使用一些不同的库(pdfkit、pdfcrowd、PyQt4.QtGui),但没有保存带有条形码的pdf。附上脚本:

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)

那么我如何翻译 html 或制作 jpg/pdf 文件,以便打印机可以读取此信息并使用条形码标签正确打印?

最佳答案

ReportLab 在这种情况下非常好用。我不确定我在哪里找到这个代码。但这确实对我有帮助。

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()

关于python - 生成带条形码的数据标签(html或jpg或pdf)并打印它(现在不生成条形码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38100239/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com