php条形码 - 在单页中显示多个条形码

时间:2015-03-16 05:30:33

标签: php while-loop barcode

我创建了一个打印条形码的php页面。只是在我打印到A4之前查看它。仍在测试阶段。代码如下。

<?php
include('include/conn.php');
include('include/Barcode39.php'); 
$sql="select * from barcode where b_status = 'NOT-PRINTED'";
$result=mysqli_query($conn,$sql);
echo mysqli_num_rows($result);

$i=0;

while($row=mysqli_fetch_assoc($result)){
$acc_no = $row["b_acc_no_code"];
$bc = new Barcode39($row["b_acc_no_code"]);
echo $bc->draw();
$bc->draw($acc_no.$i.".jpg");
echo '<br /><br />';
$i++;
}
?>

没有while循环,可以打印,但只能打印一个条形码。如何使其生成,例如在数据库中有5个值,它将在同一页面中打印5个条形码。提前致谢

2 个答案:

答案 0 :(得分:0)

尝试使用其他条形码来源。因为每页只生成一个条形码。无法在每页创建多个条形码。

答案 1 :(得分:0)

我知道这是一篇较旧的帖子,但在搜索中出现,所以可能值得回复。

我已成功使用Barcode39显示多个条形码。诀窍是从类中获取base64数据,然后在单独的HTML标记中显示条形码。

最快的方法是在draw()方法中添加$ base64参数:

   public function draw($filename = null, $base64 = false) {

然后,在draw()方法的末尾附近,修改以缓冲imagegif()调用并返回base64中的输出:

// check if writing image
if ($filename) {
    imagegif($img, $filename);
} 
// NEW: Return base 64 for the barcode image
else if ($base64) {
    ob_start();
    imagegif($img);
    $image_data = ob_get_clean();

    imagedestroy($img);
    return base64_encode($image_data);
} 
// display image
else {
    header("Content-type: image/gif");
    imagegif($img);
}

最后,要显示调用过程的倍数,请在循环中构建图像HTML并显示:

// assuming everything else has been set up, end with this...
$base64 = $barcode->draw('', true);     // Note the second param is set for base64
$html = '';
for ($i = 0; $i < $numBarcodes; $i++) {
    $html .= '<img src="data:image/gif;base64,'.$base64.'">';
}
die('<html><body>' . $html . '</body></html>');

我希望这有助于其他任何人面对这一挑战。