"CGI" stands for "Common Gateway Interface". CGI is a popular method by which a web server can obtain data from (or send data to) databases, documents, and other programs, and present that data to viewers via the web. More simply, CGI is programming for the web. A CGI can be written in any programming language, but Perl is the most popular.
- PERL 5 or later for Windows.
- A Microsoft Windows based Server running either Microsoft Personal Web Server (PWS) or Microsoft Internet Information Server (IIS).
- TAL Bar Code ActiveX Control (Plus version).
- Microsoft Internet Explorer 4.01 or higher (recommended)
- Microsoft Visual Basic 6.0 (To create and compile your own VB program)
While investigating how to create server side bar codes, we found that it is possible to use Perl CGI code to do the job however it was also determined that using Active Server Pages is far easier, quicker and requires less work to implement. Since we had developed a method of using a Perl CGI Script (with some help from a compiled Visual Basic program) to achieve the same results as our ASP page example, we have included here.
How it Works
The user completes an HTML form with information about the bar code they wish to create, such as the symbology, the text to be encoded, the text for the comment, etc. and submits the information to the server.
This information is decoded by the Perl script and passed to an executable program, written in Visual Basic. The VB program generates a bar code to the visitors' specifications and saves it as an image file with a unique filename. The unique filename is based on a number, generated by the Perl script using a file called "Counter.txt". Counter.txt contains nothing more than a number, which the Perl script reads into a variable called "$COUNT". Count is then incremented and the text file updated. The script then passes the number to the VB program along with the form data.
The VB program also generates some HTML that can be used to display the image the correct size in the visitors browser, and saves it to a text file. The Perl Script opens this text file and copies the HTML from it as it generates the page to display in the Browser.
Using this Example
- Download, unzip and install Perl 5
- Configure your Web Server to run Perl Scripts
- Download the files discussed in the example below. This is a self extracting zip file containing the HTML form, Perl script, Visual Basic project files, the compiled program for those of you who do not have Visual Basic, but would still like to test it, and the "counter" file.
- Extract the files. Copy "Getbarcode.exe","Barcode.pl" and "counter.txt" into your "Scripts" folder (usually "C:\Webshare\Scripts").
- Copy "perlForm.htm" into the root directory of your web site (usually C:\Webshare\Wwwroot).
- Open your Web browser and type in "http://your.Server.name/perlForm.htm" into the address line, change the properties that interest you on the form, and submit it. You should be rewarded with a barcode. If you are not, check out the troubleshooting section at the end of this document.
- If you have Visual Basic 6 you can open the VB project and make changes to the "Getbarcode" program and recompile it. (If you have an older version of Visual Basic you may not be able to open the project but can still import the form and the module, or copy and paste the code from this page into your own project).
Sample HTML Form Code
Click here for the sample code for building an HTML Form.
Sample Perl Script
# Tell the server that this is a Perl Script, and where to find the Perl interpreter.
Sample Visual Basic Code
Module 1 Code
This is the code that generates the barcodes. The parameters specified by the user are passed by the Perl Script to this routine as one long string of data separated by the tilde (~) character. The routine below parses out the parameters into an array of data, then sets the properties of the activeX control with the data supplied by the user. The properties are matched to the data based on the order in which they were received. The barcode is then saved as an image file using the SaveBarCode Method. The image is saved in a format appropriate for the user's Browser (PNG is preferable because its compression technique generates smaller images than GIF files, meaning that the page will load faster and less space is required on the server). The HTML code required to display the image is written to a text file with the same name as the image, which the perl script opens up and uses to display the final image on the page.
Note: In the code below there is no data validation to verify that submitted data is appropriate for the symbology selected, nor does it check to see if all required information was supplied. If you plan to use this technique such additional checking will be required.
If no command line arguments are passed to the program the following form appears. It can be used by the administrator to set the Default settings on the Server. If command line arguments are passed to the form, then the form will not be displayed.
This is the code behind the Visual Basic Main Form:
Private Sub chkBearer_Click()
'set Bearer bars TALBarCd1.BearerBars = chkBearer.Value End Sub Private Sub chkQuiet_Click() 'set quiet zones TALBarCd1.QuietZones = chkQuiet.Value End Sub Private Sub cmdBrowse_Click() 'This routine displays a dialog box from which the user can select a folder PathForm.Show 1 End Sub Private Sub Combo1_Click() 'Set Symbology TALBarCd1.Symbology = Combo1.ListIndex End Sub Private Sub Combo2_Click() 'Set rotation TALBarCd1.Rotation = Combo2.ListIndex End Sub Private Sub Combo3_Click() 'set comment alignment TALBarCd1.CommentAlignment = Combo3.ListIndex End Sub Private Sub Combo4_Click() 'set Fore color Select Case Combo4.ListIndex Case 0: C& = 0 Case 1: C& = &HFFFFFF Case 2: C& = &HFF Case 3: C& = &HFF00& Case 4: C& = &HFF0000 End Select TALBarCd1.ForeColor = C& End Sub Private Sub Combo5_Click() 'Set Back color Select Case Combo5.ListIndex Case 0: C& = 0 Case 1: C& = &HFFFFFF Case 2: C& = &HFF Case 3: C& = &HFF00& Case 4: C& = &HFF0000 End Select TALBarCd1.BackColor = C& End Sub Private Sub Dir1_Change() 'After changing the image path update the textbox on the form txtImagePath.Text = Dir1.Path End Sub Private Sub Form_Load() 'When the form loads... 'fill Symbology dropdown with list of symbologies Combo1.AddItem "Code 39" Combo1.AddItem "Code 39 Full ASCII" Combo1.AddItem "Code 39 HIBC" Combo1.AddItem "Codabar" Combo1.AddItem "Code 93" Combo1.AddItem "Code 128" Combo1.AddItem "EAN/UCC 128" Combo1.AddItem "Interleaved 2 of 5" Combo1.AddItem "Postnet" Combo1.AddItem "UPC-A" Combo1.AddItem "UPC-E" Combo1.AddItem "EAN-8" Combo1.AddItem "EAN-13" Combo1.AddItem "Bookland" Combo1.AddItem "MSI/Plessey" Combo1.AddItem "PDF417" Combo1.AddItem "Aztec Code" Combo1.AddItem "Data Matrix" Combo1.AddItem "Maxicode" 'Get the last symbology used from the registry. Combo1.ListIndex = GetSetting("BCGen", "Settings", "Symbology", "0") 'fill Rotation dropdown Combo2.AddItem "0 Degrees" Combo2.AddItem "90 Degrees" Combo2.AddItem "180 Degrees" Combo2.AddItem "270 Degrees" 'Get the last Rotation setting from the registry. Combo2.ListIndex = GetSetting("BCGen", "Settings", "Rotation", "0") 'fill Comment alignment dropdown Combo3.AddItem "Left" Combo3.AddItem "Center" Combo3.AddItem "Right" 'Get the last Comment Alignment setting from the registry. Combo3.ListIndex = GetSetting("BCGen", "Settings", "commentAlignment", "0") 'fill Fore Color dropdown Combo4.AddItem "Black" Combo4.AddItem "White" Combo4.AddItem "Red" Combo4.AddItem "Green" Combo4.AddItem "Blue" 'Get the last Forecolor setting from the registry. Combo4.ListIndex = GetSetting("BCGen", "Settings", "ForeColor", "0") 'fill Back Color dropdown Combo5.AddItem "Black" Combo5.AddItem "White" Combo5.AddItem "Red" Combo5.AddItem "Green" Combo5.AddItem "Blue" 'Get the last Backcolor setting from the registry. Combo5.ListIndex = GetSetting("BCGen", "Settings", "BackColor", "1") 'MainForm.TALBarCd1.BackColor = Combo5.ListIndex 'Get the last Image resolution used from the registry or use a default of 300 txtImageResolution = GetSetting("BCGen", "Settings", "RasterResolution", "300") 'set image resolution property MainForm.TALBarCd1.RasterImageResolution = txtImageResolution.Text 'Get the last Narrow Bar Width setting from registry or use a default of 13 txtNarrowBW = GetSetting("BCGen", "Settings", "NarrowBarWidth", "13") 'set narrow bar width property MainForm.TALBarCd1.NarrowBarWidth = txtNarrowBW.Text 'Get the last BarHeight setting from registry or use a default of 1000 txtBarHeight = GetSetting("BCGen", "Settings", "BarHeight", "1000") MainForm.TALBarCd1.BarHeight = txtBarHeight.Text 'Get the last Bar Width Reduction setting from registry or use a default of 0 txtBarWidthRed = GetSetting("BCGen", "Settings", "BarWidthReduction", "0") MainForm.TALBarCd1.BarWidthReduction = txtBarWidthRed.Text 'Get the last Matrix Module size setting from registry or use a default of 20 txtMatrixModSize = GetSetting("BCGen", "Settings", "MatrixModuleSize", "20") MainForm.TALBarCd1.MatrixModuleSize = txtMatrixModSize.Text 'Get the last Quiet Zones setting from registry or use a default of 1 (meaning "On") chkQuiet = GetSetting("BCGen", "Settings", "QuietZones", "1") MainForm.TALBarCd1.QuietZones = chkQuiet.Value 'Get the last BearerBars setting from registry or use a default of 0 (meaning "Off") chkBearer = GetSetting("BCGen", "Settings", "BearerBars", "0") MainForm.TALBarCd1.BearerBars = chkBearer.Value End Sub Private Sub Form_Unload(Cancel As Integer) 'save new settings to the registry SaveSetting "BCGen", "Settings", "Symbology", Combo1.ListIndex SaveSetting "BCGen", "Settings", "Rotation", Combo2.ListIndex SaveSetting "BCGen", "Settings", "CommentAlignment", Combo3.ListIndex SaveSetting "BCGen", "Settings", "ForeColor", Combo4.ListIndex SaveSetting "BCGen", "Settings", "BackColor", Combo5.ListIndex SaveSetting "BCGen", "Settings", "imagepath", txtImagePath SaveSetting "BCGen", "Settings", "RasterResolution", txtImageResolution SaveSetting "BCGen", "Settings", "NarrowBarWidth", txtNarrowBW SaveSetting "BCGen", "Settings", "BarHeight", txtBarHeight SaveSetting "BCGen", "Settings", "BarWidthReduction", txtBarWidthRed SaveSetting "BCGen", "Settings", "MatrixModuleSize", txtMatrixModSize SaveSetting "BCGen", "Settings", "QuietZones", chkQuiet SaveSetting "BCGen", "Settings", "BearerBars", chkBearer End Sub Private Sub mExit_Click() 'exit from menu Unload Me End Sub Private Sub Text1_Change() 'When user types new text into Message text box, update barcode TALBarCd1.Message = Text1.Text End Sub Private Sub Text2_Change() 'When user types new text into Comment text box, update barcode TALBarCd1.Comment = Text2.Text End Sub Private Sub txtImagePath_Change() 'if the imagepath is typed in without a trailing backslash, then put one in If Right(Me.txtImagePath, 1) <> "\" Then Me.txtImagePath = Me.txtImagePath + "\" End If End Sub Private Sub txtBarHeight_Change() 'if the bar height is changed to a value between 11 and 6999 then set the bar height, otherwise beep. If Val(txtBarHeight.Text) > 10 And Val(txtBarHeight.Text) < 7000 Then TALBarCd1.BarHeight = Val(txtBarHeight.Text) Else Beep End If End Sub Private Sub txtBarWidthRed_Change() 'if the bar width reduction is changed to a value between 99 and -99 then set the bar width reduction, otherwise beep. If (Val(txtBarWidthRed.Text) < 100) And (Val(txtBarWidthRed.Text) > -100) Then TALBarCd1.BarWidthReduction = Val(txtBarWidthRed.Text) Else Beep End If End Sub Private Sub txtMatrixModSize_Change() 'if the Matrix Module size is changed to a value between 1 and 64 then set theMatrix Module size, otherwise beep. If Val(txtMatrixModSize.Text) > 0 And Val(txtMatrixModSize.Text) < 65 Then TALBarCd1.MatrixModuleSize = Val(txtMatrixModSize.Text) Else Beep End If End Sub Private Sub txtNarrowBW_Change() 'if the narrowbarwidth is changed to a value between 1 and 99 then set the narrowbarwidth, otherwise beep. If Val(txtNarrowBW.Text) > 0 And Val(txtNarrowBW.Text) < 100 Then TALBarCd1.NarrowBarWidth = Val(txtNarrowBW.Text) Else Beep End If End Sub
How do I find out what the homepage or root directory of my web server is?
If you do not know your Server name or the root directory of your web server, then double click on the Web Server icon in your system tray to find out:
If you are running an older version of PWS or FrontPage Web Server, then your icon and PWS manager may look like this:
When I submit the Form there is a broken link where the barcode image should be
The most likely causes of this problem are:
- Check that the line of code that saves the barcode is saving it to the same folder that the <IMG> tag expects to find it in. For example, in our code above we are saving the image into the root directory of the website. If we were to set the "src" (source) attribute to our image tag incorrectly (such as having it look in the scripts folder) we would get a broken link since the image is not going to be in that folder.
- Verify that the image is being created in the specified folder. If the image is not being created then it may be a permissions issue. Check that the public has write access to the folder in which you wish to save the barcodes.
- You tried to create a barcode with invalid parameters.
When I submit the Form I receive "Cannot find Server"
Verify that you have started the Web Server by double clicking on the icon in the system tray if a button marked "Start" is available then click on it to start the Web Server If the button says "Stop" then try stopping and restarting the Web Server.
If this does not resolve the problem make sure that you opened the form via http, rather than as a file on your hard drive. In other words, the address line in your Browser should read: "http://your.Server.Name/perlForm.htm" not "C:\WEBSHARE\WWWROOT\perlForm.htm".
When I submit the Form I receive "HTTP 404 Not Found"
This error means that the script (or the form) is in the wrong folder. For example, you may have copied both the form and the script into your "Scripts" folder (C:\WEBSHARE\Scripts). Since your Web Server's name is different to ours we used a relative link when telling the form where the barcode.asp script is located. When you submit the form, it tells the Web Server that the script is one level deeper than the form on the server, inside a folder called "Scripts". If the form is already inside that folder, then the Server is looking for C:\WEBSHARE\Scripts\Scripts. Since this folder probably does not exist it cannot find the script it is looking for. To resolve the problem, either move the form to the C:\WEBSHARE\WWWRoot folder, or change the <Form> tag in the html form to point to where the Script actually is. In this example, changing the line <form METHOD="POST" NAME="BarCodeForm" action="scripts/barcode.pl"> to read <form METHOD="POST" NAME="BarCodeForm" action="barcode.pl"> would resolve the problem.
When I submit the Form I receive "HTTP Error 403 403.1 Forbidden"
This error indicates that the folder into which you copied the barcode.pl and "Getbarcode.exe" file does not have execute permissions. Either move the files to a folder that does have execute permissions such as the "Scripts" Folder, or grant execute permissions to that folder.
To grant execute permissions, open the Personal Web Server Manager, click on "Advanced", double click on the folder you wish to use, then check the "Execute" and "Scripts" checkboxes and click "OK". (On older versions of Personal Web Server click on "Administration", and then change the properties of the folder).
When I submit the Form I receive "HTTP Error 405 - Method Not allowed"
This error means that your server is not configured to run Perl scripts. Configure your server to run Perl Scripts. If restarting the server doesn't work then reboot the server machine.
When I submit the Form I receive "Error 500 - Internal Server Error"
This means that there is a syntax error in your Perl script. The best way to find it is to open an MSDOS window, navigate to the directory where you installed Perl and type: Perl -c script.pl (where "script.pl" is the name of your script. Note: If you script is in a different folder to the Perl interpreter - which for the sake of security it certainly should be! - then you must specify the full path to the script: e.g. Perl -c C:\webshare\wwwroot\CGI-bin\script.pl). The "-c" argument tells Perl to compile the script without actually executing it.
If there is a compilation error Perl will inform you which line the error is on and often why the error occurred. Common syntax errors that cause an Error 500 include:
- "Missing semicolon in previous line."
- "Can't find terminator before EOF." This message is usually the result of a missing right bracket or quotation mark.
- "Might be a runaway multi-line string starting on line n." This is usually a missing quotation mark.
- "Missing right bracket"
By using Perl to compile your script you can turn a very baffling Error 500 into a simple "whoops I seem to have forgotten to close my quotations on line 10".
It is also extremely important to remember that Perl is case sensitive. If you create a variable called $test and then later refer to it as $Test you will get unexpected results since $Test is considered by Perl to be a new and empty variable. For example:
$X = 6; $Y = $x + 2; print "$X + 2 = $Y";
Clearly, when the code was written I expected to print 6 + 2 = 8 onto my web page, but what I actually get is 6 + 2 = 2. While in this case we would not get a Server error, we would not get the result we expected and would be left looking rather silly.
When I submit the Form I see the Perl script as a text document
This means that your server is not configured to run Perl scripts. Configure your server to run Perl Scripts. If restarting the server doesn't work then reboot the server machine.
When I try to run the Getbarcode.exe program it generates an "automation error" or something about a missing DLL file?
To resolve the problem, the latest Visual Basic Runtime files must be installed onto the client machine before the program can run successfully.
Where Do I Get these files?
The VBRUN60.EXE file is available for download here.
For additional information, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID : Q192461
TITLE : FILE: VBRUN60.EXE Installs Visual Basic 6.0 Run-Time Files
If I change the symbology I sometimes get a broken link or a totally different barcode stretched out of proportion, why is this?
In our Example, we did not include any code to validate the data. Some Barcode symbologies have very strict rules about what can be encoded, UPC-A for instance, can only accept a 12 digit numeric message. If you type in "Test" and select UPC-A then the activeX control produces an error and sets the size properties back to the dafaults for code 39 encoded with 1234567890, but it does not save a barcode to disk. You will recall that we allowed for 10 unique barcodes to be generated before it rolls over and starts overwriting. If you haven't yet created 10 barcodes then you will probably get a broken link, otherwise you will get a distorted image of the last number n barcode.