3

Alright so first things first:

I've searched over this site for 6+ hours and I keep coming up with the same results. The main answer I keep getting is: How to Generate Barcode using PHP and Display it as an Image on the same page

But this is not working for me. Even the answer on that page that was accepted ends with "After you have added all the codes, you will get this way:" which is so vague I feel like I'm supposed to already be an expert to understand it. I'm getting frustrated with this problem because I cannot seem to find any "moron directions" that can help me understand how everything works in this library for barcode generator for php.

Here is what I have:

I'm using fpdf to print a pdf file which works great!

Page Name: PrintMyPDF.php

<?php
//error_reporting(E_ALL);  
//ini_set('display_errors', 1);
$thisorderID = $_GET['Xort'];

require ('UFunctions.php');

if (trim($thisorderID) == ""){
$value = '0';
}

if (!is_digit($thisorderID) || $thisorderID < 0)
{
header('Location:ErrorInt.php');
exit;
}

//Database connection established
require_once('DBASEConnector.php');

$sql2 = "SELECT users.name, users.storenum, users.storename, Orders.OrderID, Orders.name
FROM users, Orders
WHERE Orders.name = users.name
AND Orders.OrderID = '$thisorderID'";
$result = $db->query($sql2);

$row = $result->fetch_assoc();
$ThisStoreNum = $row['storenum'];
$ThisStoreName = $row['storename'];

require('fpdf.php');

$pdf = new FPDF();
//$fpdf->SetMargins(0, 0, 0);
//$fpdf->SetAutoPageBreak(true, 0);
$pdf->SetAuthor('Walter Ballsbig');
$pdf->SetTitle('Order Form');
$pdf->SetFont('Helvetica','B',16);
$pdf->SetTextColor(0,0,0);

$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');

$pdf->SetXY(50,20);
$pdf->SetDrawColor(0,0,0);
$pdf->Cell(100,10,'Order Form',1,1,'C',0);
$pdf->SetFontSize(10);
$pdf->SetX(50);
$pdf->Cell(100,10, 'Order: '.$thisorderID.'  |  Store: '.$ThisStoreNum.'-'.$ThisStoreName,1,1,'C',0);


$pdf->SetXY(10,50);
$pdf->SetFontSize(12);
$pdf->Cell(6,6,'X',1,0,'C',0);
$pdf->Cell(14,6,'QTY',1,0,'C',0);
$pdf->Cell(130,6, 'ITEM',1,0,'C',0);
$pdf->Cell(30,6, 'UNIT',1,1,'C',0); 

$query = "SELECT Inventory.ProductI, Inventory.ProductName, Inventory.CurrentQty, Inventory.Pull, Inventory.Unit, OrderItems.ProductI, OrderItems.QtyO, OrderItems.OrderI 
FROM Inventory, OrderItems
WHERE OrderItems.OrderI = '$thisorderID'
AND OrderItems.ProductI = Inventory.ProductI
ORDER BY Inventory.Pull, Inventory.ProductName";
$result = $db->query($query);

$num_results = $result->num_rows;

for ($i=0; $i <$num_results; $i++) 
{
$row = $result->fetch_assoc();

$pdf->SetFontSize(12);

IF ($row['CurrentQty'] <=0)
{
$pdf->SetFontSize(10);
$pdf->Cell(6,6,'BO',1,0,'C',0);
$pdf->SetFontSize(12);
}else{
$pdf->Cell(6,6,' ',1,0,'C',0);
}
$pdf->Cell(14,6, $row['QtyO'],1,0,'C',0);
$pdf->Cell(130,6, $row['ProductName'],1,0,'L',0);
$pdf->Cell(30,6, $row['Unit'],1,1,'C',0);   
}


$pdf->Output();

$db->close();
?>

This prints up my pdf beautifully! Now I wanted to add a barcode on the page that will represent the order number for scanning purposes.

Now here is what I have for my code that contains the barcode... code.

Name of barcode page: BarCodeIt.php

<?php

function BarCodeIt($MyID) {
// Including all required classes
require_once('./class/BCGFontFile.php');
require_once('./class/BCGColor.php');
require_once('./class/BCGDrawing.php');

// Including the barcode technology
require_once('./class/BCGcode39.barcode.php');

// Loading Font
$font = new BCGFontFile('./font/Arial.ttf', 18);

// Don't forget to sanitize user inputs
$text = isset($_GET['text']) ? $_GET['text'] : $MyID;

// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);

$drawException = null;
try {
$code = new BCGcode39();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$code->parse($text); // Text
} catch(Exception $exception) {
$drawException = $exception;
}

/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}

//Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
header('Content-Disposition: inline; filename="barcode.png"');

// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
}
?>

Now in my PDF file just before this line:

$pdf->Output();

I have added this:

$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');

require('/BarCodeIt.php');

$MyBarCode = BarCodeIt($thisorderID);
echo $MyBarCode;

But what it does is all of my other pdf elements disappear and I'm left with only a big barcode (the right one! that part works) but that's all that is on the screen. It's like when the barcode section runs it negates everything else and just prints the barcode. I want to print just the barcode where I want it on the PDF but I'm not clever enough to figure out what I'm doing wrong. Any help on this would be greatly appreciated.

Community
  • 1
  • 1
Bruce BanEm
  • 57
  • 1
  • 7

2 Answers2

0

In $pdf->SetDisplayMode(real,'default');, real is not an identifier. I believe you've forgotten the $ prefix.

Have you warnings reporting at maximum level? If not, include:

error_reporting(E_ALL);

in your script and see that it shows additional issues.

Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
0

I'm not familiar with fpdf, but what you are doing seems wrong just by looking at it: Everywhere you add elements to your pdf by using methods on your $pdf object like $pdf->... and when you want to add the barcode, you echo it out directly.

Don't echo your image out. Instead get rid of the header() calls in your barcode script, save your image and look for the right method to add an image to the $pdf object.

Here is a question with answers that deals with adding an image: Inserting an image with PHP and FPDF

Community
  • 1
  • 1
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • From what I understand, because this part is blank: `/* Here is the list of the arguments 1 - Filename (empty : display on screen) 2 - Background color */ $drawing = new BCGDrawing('', $color_white);` it's supposed to print the image out to the screen. I will try adding $pdf-> to different parts of the code. Also if I save the image it would save it as the same file name. While I only need it to last long enough for it to be in this PDF for printing, what if it is generated at the same time and is overwritten before it's printed in this pdf... or does that sound possible? – Bruce BanEm May 31 '14 at 02:49
  • @BruceBanEm Generate a unique idea and put it in the server's `/tmp/` directory. Or something similar. – jeroen May 31 '14 at 17:29