【问题标题】:How do I print a barcode using barcode generator for PHP onto a pdf formatted page where I want it?如何使用 PHP 的条形码生成器将条形码打印到我想要​​的 pdf 格式页面上?
【发布时间】:2014-07-20 20:34:18
【问题描述】:

好吧,首先要做的事情:

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

但这对我不起作用。即使该页面上被接受的答案也以“添加所有代码后,您将得到这种方式:”结尾,这太模糊了,我觉得我应该已经是理解它的专家了。我对这个问题感到沮丧,因为我似乎找不到任何“白痴方向”来帮助我理解这个库中的一切是如何工作的,用于 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 上我想要的地方打印条形码,但我不够聪明,无法弄清楚我做错了什么。对此的任何帮助将不胜感激。

【问题讨论】:

    标签: php barcode


    【解决方案1】:

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

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

    error_reporting(E_ALL);
    

    在您的脚本中查看它是否显示了其他问题。

    【讨论】:

      【解决方案2】:

      我对 fpdf 不熟悉,但您所做的只是通过查看它似乎是错误的:在任何地方,您都可以通过使用 $pdf 对象上的方法(如 $pdf-&gt;...)向 pdf 添加元素以及何时添加条码,你echo直接拿出来。

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

      这里有一个关于添加图像的问题的答案:Inserting an image with PHP and FPDF

      【讨论】:

      • 据我了解,因为这部分是空白的:/* Here is the list of the arguments 1 - Filename (empty : display on screen) 2 - Background color */ $drawing = new BCGDrawing('', $color_white); 它应该将图像打印到屏幕上。我将尝试将 $pdf-> 添加到代码的不同部分。此外,如果我保存图像,它会将其保存为相同的文件名。虽然我只需要它持续足够长的时间才能在此 PDF 中进行打印,但如果它同时生成并在此 pdf 中打印之前被覆盖……或者这听起来可能吗?
      • @BruceBanEm 生成一个独特的想法,并将其放在服务器的/tmp/ 目录中。或类似的东西。
      猜你喜欢
      • 2021-03-03
      • 2021-10-27
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多