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


How do I print a barcode using barcode generator for PHP onto a pdf formatted page where I want it?

好了,重要的是:

我在这个网站上搜索了6个多小时,我不断得到同样的结果。我一直得到的主要答案是:如何使用PHP生成条形码并将其显示为同一页面上的图像

但这对我不起作用。甚至那一页上的答案也以"在你添加了所有代码之后,你会得到这样的结果"结尾,这是如此模糊,我觉得我应该已经是一个理解它的专家了。我对这个问题感到沮丧,因为我似乎找不到任何"白痴方向",可以帮助我理解如何在这个库中为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上打印条形码,但我不够聪明,不知道我做错了什么。如有任何帮助,我将不胜感激。

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

您的警告报告是否达到最高级别?如果没有,包括:

error_reporting(E_ALL);

我不熟悉fpdf,但你所做的似乎是错误的,只是看看它:在任何地方,你通过使用$pdf->...$pdf对象上的方法添加元素到你的pdf,当你想添加条形码时,你直接echo

不要用echo把你的图片删掉。相反,在条形码脚本中去掉header()调用,保存图像并寻找将图像添加到$pdf对象的正确方法。

下面是一个关于添加图像的问题和答案:使用PHP和FPDF插入图像