This topic is locked

Barcode on Print-Friendly View or Report

4/22/2009 11:16:08 PM
PHPRunner General questions
B
bussb author

Hello, I would like to echo a barcode next to the human readable format of values in my PHP Runner database. When the user clicks on the print-friendly view or even a report, it would show barcode label which they would then print.
Any ideas?

B
barlow 4/24/2009

Hello, I would like to echo a barcode next to the human readable format of values in my PHP Runner database. When the user clicks on the print-friendly view or even a report, it would show barcode label which they would then print.

Any ideas?


Well I'm just theorizing, but I'd approach it like this:

  1. In your db table add fields for each of the number fields that need barcodes
  2. Configure these fields as images - set 'View As' to image in Visual editor.
  3. In Choose Fields, set these fields to only display in Print-friendly view.
  4. In Visual Editor, edit the Print-friendly page to have the barcode images where you want them.
  5. Obtain a PHP Barcode Generator file - Google for this, I just did and found several available.

    Find one, or adapt one, so that running the PHP file generates an image of the barcode you need. Give it GET paramters so you can pass in the number/code to codify.

    e.g. barcodeGenerator.php?code=1234 which would display ina browser as a barcode image
  6. In an appropriate event, where the number field is filled in (Add/Edit events), set the image fields to point to your generator file with parameters from the form

    e.g.
  7. You might want to validate or provide escapes around value['num1'] to protect against code injection, although PHPRUNNER does this.
    Going to all the trouble putting your barcode generator into image fields in your table means that you can use the Visual Editor on your output page without worrying about having to keep any custom layout or code up-to-date.
    An alternative which avoids the additional fields and event code is to put the image generator directly into the HTML using the Visual Editor.

    e.g.
    [font=Lucida Console]<IMG src="barcodeGenerator.php?code={num1_value}"/>
    where {num1_value} is, I believe, the code PHPRUNNER would use to display the num1 field. However, you'd have to check the printer-friendly page HTML to check the exact code used.
    Just some ideas on how to approach it. The key is, of course, getting the PHP barcode generator module, and one which renders an image output.

N
nitro23456 4/25/2009



Well I'm just theorizing, but I'd approach it like this:

  1. In your db table add fields for each of the number fields that need barcodes
  2. Configure these fields as images - set 'View As' to image in Visual editor.
  3. In Choose Fields, set these fields to only display in Print-friendly view.
  4. In Visual Editor, edit the Print-friendly page to have the barcode images where you want them.
  5. Obtain a PHP Barcode Generator file - Google for this, I just did and found several available.

    Find one, or adapt one, so that running the PHP file generates an image of the barcode you need. Give it GET paramters so you can pass in the number/code to codify.

    e.g. barcodeGenerator.php?code=1234 which would display ina browser as a barcode image
  6. In an appropriate event, where the number field is filled in (Add/Edit events), set the image fields to point to your generator file with parameters from the form

    e.g.
  7. You might want to validate or provide escapes around value['num1'] to protect against code injection, although PHPRUNNER does this.
    Going to all the trouble putting your barcode generator into image fields in your table means that you can use the Visual Editor on your output page without worrying about having to keep any custom layout or code up-to-date.
    An alternative which avoids the additional fields and event code is to put the image generator directly into the HTML using the Visual Editor.

    e.g.
    [font=Lucida Console]<IMG src="barcodeGenerator.php?code={num1_value}"/>
    where {num1_value} is, I believe, the code PHPRUNNER would use to display the num1 field. However, you'd have to check the printer-friendly page HTML to check the exact code used.
    Just some ideas on how to approach it. The key is, of course, getting the PHP barcode generator module, and one which renders an image output.


I have a fully working version of barcodes, please contact me if anyone needs help with this.

N
nitro23456 4/27/2009

For anyones information:
Copy this code into notepad and save it as barcodemaker.php and upload into your php directory.

<?php

/===========================================================================/

/ PHP Barcodemaker WRITTEN BY nitro23456 25/04/2009
PARAMETERS:

-----------

$barcode = [required] The barcode you want to generate
$type = (default=0) It's 0 for Code 3 of 9 (the only one supported)
$width = (default=160) Width of image in pixels. The image MUST be wide

enough to handle the length of the given value. The default

value will probably be able to display about 6 digits. If you

get an error message, make it wider!
$height = (default=80) Height of image in pixels
$format = (default=jpeg) Can be "jpeg", "png", or "gif"
$quality = (default=100) For JPEG only: ranges from 0-100
$text = (default=1) 0 to disable text below barcode, >=1 to enable
/

/=============================================================================/
//-----------------------------------------------------------------------------

// Startup code

//-----------------------------------------------------------------------------
if(isset($_GET["text"])) $text=$_GET["text"];

if(isset($_GET["format"])) $format=$_GET["format"];

if(isset($_GET["quality"])) $quality=$_GET["quality"];

if(isset($_GET["width"])) $width=$_GET["width"];

if(isset($_GET["height"])) $height=$_GET["height"];

if(isset($_GET["type"])) $type=$_GET["type"];

if(isset($_GET["barcode"])) $barcode=$_GET["barcode"];
if (!isset ($text)) $text = 1;

if (!isset ($type)) $type = 1;

if (empty ($quality)) $quality = 100;

if (empty ($width)) $width = 160;

if (empty ($height)) $height = 80;

if (!empty ($format)) $format = strtoupper ($format);

else $format="JPEG";
switch ($type)

{

default:

$type = 1;

case 1:

Barcode39 ($barcode, $width, $height, $quality, $format, $text);

break;

}
//-----------------------------------------------------------------------------

// Generate a Code 3 of 9 barcode

//-----------------------------------------------------------------------------

function Barcode39 ($barcode, $width, $height, $quality, $format, $text)

{

switch ($format)

{

default:

$format = "JPEG";

case "JPEG":

header ("Content-type: image/jpeg");

break;

case "PNG":

header ("Content-type: image/png");

break;

case "GIF":

header ("Content-type: image/gif");

break;

}
$im = ImageCreate ($width, $height)

or die ("Cannot Initialize new GD image stream");

$White = ImageColorAllocate ($im, 255, 255, 255);

$Black = ImageColorAllocate ($im, 0, 0, 0);

//ImageColorTransparent ($im, $White);

ImageInterLace ($im, 1);
$NarrowRatio = 20;

$WideRatio = 55;

$QuietRatio = 35;
$nChars = (strlen($barcode)+2) ((6 $NarrowRatio) + (3 $WideRatio) + ($QuietRatio));

$Pixels = $width / $nChars;

$NarrowBar = (int)(20
$Pixels);

$WideBar = (int)(55 $Pixels);

$QuietBar = (int)(35
$Pixels);
$ActualWidth = (($NarrowBar 6) + ($WideBar3) + $QuietBar) (strlen ($barcode)+2);
if (($NarrowBar == 0) || ($NarrowBar == $WideBar) || ($NarrowBar == $QuietBar) || ($WideBar == 0) || ($WideBar == $QuietBar) || ($QuietBar == 0))

{

ImageString ($im, 1, 0, 0, "Image is too small!", $Black);

OutputImage ($im, $format, $quality);

exit;

}
$CurrentBarX = (int)(($width - $ActualWidth) / 2);

$Color = $White;

$BarcodeFull = "
".strtoupper ($barcode)."";

settype ($BarcodeFull, "string");
$FontNum = 3;

$FontHeight = ImageFontHeight ($FontNum);

$FontWidth = ImageFontWidth ($FontNum);

if ($text != 0)

{

$CenterLoc = (int)(($width-1) / 2) - (int)(($FontWidth
strlen($BarcodeFull)) / 2);

ImageString ($im, $FontNum, $CenterLoc, $height-$FontHeight, "$BarcodeFull", $Black);

}

else

{

$FontHeight=-2;

}
for ($i=0; $i<strlen($BarcodeFull); $i++)

{

$StripeCode = Code39 ($BarcodeFull[$i]);
for ($n=0; $n < 9; $n++)

{

if ($Color == $White) $Color = $Black;

else $Color = $White;
switch ($StripeCode[$n])

{

case '0':

ImageFilledRectangle ($im, $CurrentBarX, 0, $CurrentBarX+$NarrowBar, $height-1-$FontHeight-2, $Color);

$CurrentBarX += $NarrowBar;

break;
case '1':

ImageFilledRectangle ($im, $CurrentBarX, 0, $CurrentBarX+$WideBar, $height-1-$FontHeight-2, $Color);

$CurrentBarX += $WideBar;

break;

}

}
$Color = $White;

ImageFilledRectangle ($im, $CurrentBarX, 0, $CurrentBarX+$QuietBar, $height-1-$FontHeight-2, $Color);

$CurrentBarX += $QuietBar;

}
OutputImage ($im, $format, $quality);

}
//-----------------------------------------------------------------------------

// Output an image to the browser

//-----------------------------------------------------------------------------

function OutputImage ($im, $format, $quality)

{

switch ($format)

{

case "JPEG":

ImageJPEG ($im, "", $quality);

break;

case "PNG":

ImagePNG ($im);

break;

case "GIF":

ImageGIF ($im);

break;

}

}
//-----------------------------------------------------------------------------

// Returns the Code 3 of 9 value for a given ASCII character

//-----------------------------------------------------------------------------

function Code39 ($Asc)

{

switch ($Asc)

{

case ' ':

return "011000100";

case '$':

return "010101000";

case '%':

return "000101010";

case '':

return "010010100"; //
Start/Stop

case '+':

return "010001010";

case '|':

return "010000101";

case '.':

return "110000100";

case '/':

return "010100010";

case '-':

return "010000101";

case '0':

return "000110100";

case '1':

return "100100001";

case '2':

return "001100001";

case '3':

return "101100000";

case '4':

return "000110001";

case '5':

return "100110000";

case '6':

return "001110000";

case '7':

return "000100101";

case '8':

return "100100100";

case '9':

return "001100100";

case 'A':

return "100001001";

case 'B':

return "001001001";

case 'C':

return "101001000";

case 'D':

return "000011001";

case 'E':

return "100011000";

case 'F':

return "001011000";

case 'G':

return "000001101";

case 'H':

return "100001100";

case 'I':

return "001001100";

case 'J':

return "000011100";

case 'K':

return "100000011";

case 'L':

return "001000011";

case 'M':

return "101000010";

case 'N':

return "000010011";

case 'O':

return "100010010";

case 'P':

return "001010010";

case 'Q':

return "000000111";

case 'R':

return "100000110";

case 'S':

return "001000110";

case 'T':

return "000010110";

case 'U':

return "110000001";

case 'V':

return "011000001";

case 'W':

return "111000000";

case 'X':

return "010010001";

case 'Y':

return "110010000";

case 'Z':

return "011010000";

default:

return "011000100";

}

}
?>


To place the barcode in your page (print friendly view etc) edit the HTML file using visual editor or other software. Place this code where you want the barcode image to appear:

<IMG src="barcodemaker.php?barcode={$XXXXX_value}&width=400&height=75">


replace the XXXXX with whatever your field is called that you want to make a barcode of..... adjust size to suit. EVERYTHING else MUST remain the same AND the field you are making a barcode of MUST also appear as text (i.e. as normal.... you can't make a barcode of missing information) on the page you are editing. It would also be advisable to have the barcode appear anywhere below the relevant field and not above.
This does work, I have it running - follow my instructions to the letter!!!
IN RETURN - can someone help me with my thread please: http://www.asprunner.com/forums/index.php?showtopic=11589