[Barcode] Web Frontend
dani@enpl.es
dani@enpl.es
Wed, 7 Feb 2001 11:40:24 -0500 (EST)
On Wed, 7 Feb 2001, Alessandro Rubini wrote:
>
> > You can do both ways. If you use a browser, the user will always see
> > the same URL (params=values in the url are not shown to the user). But
> > you can still do a GET URL using param=value;param2=value2 way.
>
> That's fine. That's what I've been stating. So we need a sample html
> form, don't we?
Oops, now I see the problem :)
I've tried:
bash$ lynx http://montgri/cgi-bin/cb-eng.pl?encoding=CODE128;code="344";printer=
haley;horitz=3;vert=5;page=210x217;copies=1;upper_margin=0;lower_margin=0
And the CGI says: Error: nothing to encode.
Meaning $code is null.
I guess don't know how to write CGIs that aren't form-based.
I send you the English version, more documented and (a bit) more
secure. When I discover how to write form-less based cgi's, I'll send the
modifications.
<---------------------- CUT --------------------------->
#!/usr/bin/perl -w
#
# barcode.pl is a Web frontend to the Alessandro Rubini barcode's utility.
# You can modify and distribute it under the terms of the GNU General
# public license.
# You should change
# 1- $executable, @printers and line 104 according to your system.
# 2- Copy barcode.pl where your cgi-bin/ dir. resides.
use strict;
use CGI;
my $query; # The from class
my @encodings; # Different encodings Barcode utility can handle
my @printers; # Array of printers where the user can print
my $encoding; # Desired encoding
my $code; # What to encode
my $printer; # Desired printer
my $horitz; # Number of labels in the horitzontal
my $page; #
my $copies; # Number of copies
my $vert; # Number of lables in the vert.
my $upper_margin;
my $lower_margin;
my $sh;
my $executable="/usr/local/bin/barcode"; # Change this line according
# to the path where you have barcode
@encodings=("EAN-13","EAN-8","UPC","ISBN","CODE39","CODE128","i25","CODABAR","MSI","PLESSEY");
# Change @printers according to the printers you have
# or you want your users to print at.
@printers=("hpmanip","haley","hpexped","apple","lexmark","laserjet5");
$query=new CGI;
if($query->param()) # If we come from the submit button
{
# We take parameters
$encoding=$query->param("encoding");
$code=$query->param("code");
$printer=$query->param("printer");
$horitz=$query->param("horitz");
$vert=$query->param("vert");
$page=$query->param("page");
$copies=$query->param("copies");
$upper_margin=$query->param("upper_margin");
$lower_margin=$query->param("lower_margin");
# HTML header
print $query->header();
print $query->start_html("Barcode generator");
print '<meta HTTP-EQUIV="EXPIRES" CONTENT="0">';
print '<BODY BGCOLOR="#FFFFFF">';
# Check for valid input.
if ($code eq "")
{
print "<BR><B> Error: Nothing to encode</B>";exit;
}
if (!($horitz=~/(\d+)/) or(!($vert=~/(\d+)/)))
{
print "<BR><B> Error: Incorrect label distribution</B>";exit;
}
if (!($page=~/(\d+)x(\d+)/))
{
print "<BR><B> Error: Incorrect page type</B>"; exit;
}
if (!($copies=~/(\d+)/))
{
print "<BR><B> Error: Incorrect number of copies</B>"; exit;
}
if((!($upper_margin=~/\d+/)) or (!($lower_margin=~/\d+/)))
{
print "<BR><B> Error: Incorrect margins</B>";exit();
}
# We build the shell command to execute
$sh="$executable -u mm -e " . lc($encoding);
$sh.= " -t " . $horitz . "x" . $vert . "+0+0-0-" . $lower_margin;
$sh.= " -g " . $page . "+2+" . $upper_margin; # 2 mm for left margin
# $code may contain any character, so
# we quote it.
$sh .= (" -b '" . $code . "'") x ($horitz * $vert);
# printf("<B>%s</B>",$sh);
open(PIPE,"$sh 2>&1 1>/tmp/ean |");
while(<PIPE>) # We are reading stderr
{
# print barcode utility error message
printf("<BR>%s",$_);
# and exit
exit();
}
close(PIPE);
# Change next line according to your printing system.
system("/usr/bin/lp -d $printer -n $copies /tmp/ean >/dev/null && rm /tmp/ean");
print "<B>Done</B>";
print $query->end_html;
}
else #There are no parameters, let's build the html form.
{
print $query->header;
print $query->start_html("Barcode generator");
print '<meta HTTP-EQUIV="EXPIRES" CONTENT="0">';
print '<BODY BGCOLOR="#FFFFFF">';
print $query->start_form;
# $query->delete_all();
print '<TABLE BORDER="0" WIDTH="100%">';
print '<TR VALIGN="TOP">';
print '<TD colspan="2" rowspan="2" width="195">';
print '<UL><font size=+3><BR>Barcode generator ';
print '</font></TD>';
print '</TR></TABLE>';
print $query->hr;
print 'Encoding: ' . $query->popup_menu("encoding",\@encodings) . " Code: " . $query->textfield("code","8410435",20);
print '<BR>Printer: ' . $query->popup_menu("printer",\@printers);
print $query->hr;
print '<B>Stickers</B><BR>Horitz: ' . $query->textfield("horitz","3",5) . "Vert: " . $query->textfield("vert","5",5);
print '<BR><BR><B>Copies</B><BR>Print ' . $query->textfield("copies","1",5) . ' Copies';
print '<BR><BR><B>Page</B><BR>Dimensions(mm): ' . $query->textfield("page","210x297",10);
print '<BR>Upper margin(mm): ' . $query->textfield("upper_margin","20",10);
print '<BR>Lower margin(mm): ' . $query->textfield("lower_margin","20",10);
print "<BR><BR>" . $query->submit(-name=>'Print!');
print $query->end_form;
print $query->end_html;
}