简体   繁体   English

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

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

Alright so first things first: 好吧首先要做的事情是:

I've searched over this site for 6+ hours and I keep coming up with the same results. 我在这个网站上搜索了6个多小时,我不断得出相同的结果。 The main answer I keep getting is: How to Generate Barcode using PHP and Display it as an Image on the same page 我一直得到的主要答案是: 如何使用PHP生成条形码并在同一页面上将其显示为图像

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! 我正在使用fpdf打印一个效果很好的pdf文件!

Page Name: PrintMyPDF.php 页面名称: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! 这打印出我的pdf精美! 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 条形码页面的名称: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文件就在这行之前:

$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. 但它的作用是我的所有其他pdf元素都消失了,而我只剩下一个大条码(正确的一个!那个部分可以工作),但这就是屏幕上的所有内容。 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. 我想在PDF上打印我想要的条形码,但我不够聪明,弄清楚我做错了什么。 Any help on this would be greatly appreciated. 任何有关这方面的帮助将不胜感激。

In $pdf->SetDisplayMode(real,'default'); $pdf->SetDisplayMode(real,'default'); , real is not an identifier. real不是标识符。 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. 在您的脚本中,看到它显示其他问题。

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. 我对fpdf并不熟悉,但是你正在做的事情看起来是错误的:你通过使用$pdf对象上的方法(例如$pdf->...来添加元素到你的pdf中当你想添加条形码,你直接echo

Don't echo your image out. 不要echo你的形象。 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. 而是去除条形码脚本中的header()调用,保存图像并寻找将图像添加到$pdf对象的正确方法。

Here is a question with answers that deals with adding an image: Inserting an image with PHP and FPDF 这是一个问题,其中包含有关添加图像的答案: 使用PHP和FPDF插入图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM