Animal Farm Pig
04-15-2009, 01:18 AM
I need to generate a series of Code 39 barcodes. I'm using the GNU barcode library and front end. It works fine from the command line. I thought it would be nice to do a simple CGI web front end for it. Something funny is happening. For small values (ex. #'s from 20 to 30), it works fine. For larger values (ex. 6062000510 to 6062000520), the barcodes don't come out correctly.
You can check it out here:
http://atkinson.cable.nu/cgi-bin/barcodes.cgi
It looks to me like the input is exactly the same between command line and called from my Python CGI script, but the Python version is clearly wrong. :confused:
Here's the code:
#!/usr/bin/python
import cgi, subprocess
MyForm = cgi.FieldStorage()
if not (MyForm.has_key("Min") and MyForm.has_key("Max")):
print """Content-type: text/html\n
<html>
<head>
<title>Code39 barcode maker</title>
</head>
<body>
<form method = "post" action="http://192.168.10.1/cgi-bin/barcodes.cgi">
<p>Start Value: <input type='text' name='Min' />
<p>End Value: <input type='text' name='Max' />
<p><input type='submit'>
</form>
</body>
</html>
"""
else:
Min = int(MyForm.getvalue('Min'))
Max = int(MyForm.getvalue('Max')) + 1
#Make a list of strings in the form of ["n\n", "n + 1\n", "Max\n"...] as the barcode series
RangeString = map(lambda x: str(x) + '\n', range(Min, Max))
#Concatenate the list into one single string
InputString = reduce(lambda x, y: x + y, RangeString)
#Start the GNU barcode front end using Code 39 encoding, no checksum
PSConverter = subprocess.Popen(["-e code39", "-c"], executable = 'barcode', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
#Feed the barcodes to stdin of GNU barcode frontend and capture data from the stdout
RawPS = PSConverter.communicate(InputString)[0]
#Start ps2pdf with input coming through stdin and output going to stdout
PDFConverter = subprocess.Popen(['-', '-'], executable = 'ps2pdf', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
#Feed the postscript to ps2pdf and capture the data from stdout
PDF = PDFConverter.communicate(RawPS)[0]
print "Content-Type:application/pdf\n"
print "Content-Disposition:attachment;filename=\'Barcodes.pdf\'\n \n"
print PDF
#For making sure input is correct
#print "Content-Type:text/html\n"
#print InputString
#print "Success"
Anyone have any idea what I'm doing wrong here?
I need to go out and run some errands, but I'll check back later.
Edit: Just noticed that I was redirecting to my internal IP address and not the global one. Fixed now.
You can check it out here:
http://atkinson.cable.nu/cgi-bin/barcodes.cgi
It looks to me like the input is exactly the same between command line and called from my Python CGI script, but the Python version is clearly wrong. :confused:
Here's the code:
#!/usr/bin/python
import cgi, subprocess
MyForm = cgi.FieldStorage()
if not (MyForm.has_key("Min") and MyForm.has_key("Max")):
print """Content-type: text/html\n
<html>
<head>
<title>Code39 barcode maker</title>
</head>
<body>
<form method = "post" action="http://192.168.10.1/cgi-bin/barcodes.cgi">
<p>Start Value: <input type='text' name='Min' />
<p>End Value: <input type='text' name='Max' />
<p><input type='submit'>
</form>
</body>
</html>
"""
else:
Min = int(MyForm.getvalue('Min'))
Max = int(MyForm.getvalue('Max')) + 1
#Make a list of strings in the form of ["n\n", "n + 1\n", "Max\n"...] as the barcode series
RangeString = map(lambda x: str(x) + '\n', range(Min, Max))
#Concatenate the list into one single string
InputString = reduce(lambda x, y: x + y, RangeString)
#Start the GNU barcode front end using Code 39 encoding, no checksum
PSConverter = subprocess.Popen(["-e code39", "-c"], executable = 'barcode', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
#Feed the barcodes to stdin of GNU barcode frontend and capture data from the stdout
RawPS = PSConverter.communicate(InputString)[0]
#Start ps2pdf with input coming through stdin and output going to stdout
PDFConverter = subprocess.Popen(['-', '-'], executable = 'ps2pdf', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
#Feed the postscript to ps2pdf and capture the data from stdout
PDF = PDFConverter.communicate(RawPS)[0]
print "Content-Type:application/pdf\n"
print "Content-Disposition:attachment;filename=\'Barcodes.pdf\'\n \n"
print PDF
#For making sure input is correct
#print "Content-Type:text/html\n"
#print InputString
#print "Success"
Anyone have any idea what I'm doing wrong here?
I need to go out and run some errands, but I'll check back later.
Edit: Just noticed that I was redirecting to my internal IP address and not the global one. Fixed now.