PDA

View Full Version : Writing a barcode thingy -- strange results


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.

Clover
04-15-2009, 03:39 AM
I've coded a barcode cracker in python and PHP. When I dig up the source code, I'll post it. Maybe it'll help you, who knows :confused:.

Axiom
04-15-2009, 04:34 AM
Casting it with INT() might be your problem - 6062000510 is well over the maximum value that you can store as an integer. Maybe try using a Unsigned Double instead...

http://en.wikipedia.org/wiki/Integer_(computer_science)

Animal Farm Pig
04-16-2009, 03:36 PM
Figured it out. For some reason the encoding argument wasn't getting passed to /usr/bin/barcode correctly. I changed the way that it's passed and everything works now.

Here is the completed 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="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, layout 4x6 barcodes per page
PSConverter = subprocess.Popen("barcode -e code39 -t 4x6", shell = True, 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


You can try it out at http://atkinson.cable.nu/cgi-bin/barcode.cgi