如何使用PHP条形码生成器将条形码打印到我想要​​的pdf格式页面上?

时间:2014-05-31 01:14:11

标签: php barcode

好吧首先要做的事情是:

我在这个网站上搜索了6个多小时,我不断得出相同的结果。我一直得到的主要答案是:How to Generate Barcode using PHP and Display it as an Image on the same page

但这不适合我。即使是被接受的页面上的答案也以&#34结束;在您添加完所有代码后,您将得到这样的结果:"这是如此模糊,我觉得我应该已经成为了解它的专家。我对这个问题感到沮丧,因为我似乎无法找到任何"白痴方向"这可以帮助我理解这个库中的一切是如何工作的,用于php的条形码生成器。

这就是我所拥有的:

我使用fpdf打印效果很好的pdf文件!

页面名称: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();
?>

这样可以精美地打印出我的PDF格式!现在我想在页面上添加一个条形码,代表订单号以便进行扫描。

现在,我的代码包含条形码...代码。

条形码页面的名称: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);
}
?>

现在在我的PDF文件就在这行之前:

$pdf->Output();

我添加了这个:

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

require('/BarCodeIt.php');

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

但它的作用是我的所有其他pdf元素都消失了,我只剩下一个大条码(正确的一个!那个部分有效),但这就是屏幕上的所有内容。就像条形码部分运行时一样,它会否定其他所有内容并打印条形码。我想在PDF上打印我想要的条形码,但我不够聪明,无法弄清楚我做错了什么。对此的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

$pdf->SetDisplayMode(real,'default');中,real不是标识符。我相信你已经忘记了$前缀。

您是否在最高级别报告警告?如果没有,请包括:

error_reporting(E_ALL);

在您的脚本中,看到它显示其他问题。

答案 1 :(得分:0)

我对fpdf并不熟悉,但是你所看到的只是通过查看它看起来是错误的:你通过使用$pdf对象上的方法(例如$pdf->...)向你的pdf添加元素。当你想要添加条形码时,你可以直接echo

不要echo你的形象。而是删除条形码脚本中的header()调用,保存图像并查找将图像添加到$pdf对象的正确方法。

以下是一个问题,其中包含有关添加图片的答案:Inserting an image with PHP and FPDF