繁体   English   中英

使用tcpdf时如何写html格式的条码

[英]how to write barcode in html format when using tcpdf

我正在使用 TCPDF 使用以下命令生成 PDF 文件

$pdf->writeHTML($htmlcontent, true, 0, true, 0);

TCPDF 还提供了一种使用以下命令创建条形码的方法

$pdf->Cell(0, 0, 'C39+', 0, 1);
$pdf->write1DBarcode('Code 39', 'C39+', '', '', 80, 15, 0.4, $style, 'N');
$pdf->Ln();

我希望能够编写条形码作为上述 HTML 代码的一部分。 有简单的方法吗?

我可以在上面的 writeHTML 代码中调用条形码图像,但不确定如何使用上面的条形码函数(或 TCPPDF 中的任何函数),这将允许我创建图像,然后将该图像生成 HTML。

您可以在 HTML 中编写 TCPDF 方法,如下所示

<?php
$params = $pdf->serializeTCPDFtagParameters(array('40144399300102444888207482244309', 'C128C', '', '', 0, 0, 0.2, array('position'=>'S', 'border'=>false, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>false, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>2), 'N'));    
$str='<table cellspacing="0" cellpadding="1" border="0">            
<tr> 
    <td align="left">barcode</td>
</tr>
<tr> 
    <td align="center" style="padding-left:5px;">';
    $str .= '<tcpdf method="write1DBarcode" params="'.$params.'" />';
    $str .='</td>
</tr>
</table>';

$pdf->writeHTML($str,true, false,false,false,'left');
$pdf->Output('example_049.pdf', 'I');
?>

详细参考请查看 TCPDF example_049.php

TCPDF 条码类已经包含以各种格式(SVG、PNG 和 HTML)导出条码的方法。

二维示例:

require_once(dirname(__FILE__).'/2dbarcodes.php');
$barcodeobj = new TCPDF2DBarcode('http://www.tcpdf.org', 'QRCODE,H');

// export as SVG image
//$barcodeobj->getBarcodeSVG(3, 3, 'black');

// export as PNG image
//$barcodeobj->getBarcodePNG(3, 3, array(0,128,0));

// export as HTML code
echo $barcodeobj->getBarcodeHTML(3, 3, 'black');

一维示例:

require_once(dirname(__FILE__).'/barcodes.php');
$barcodeobj = new TCPDFBarcode('123456', 'C128');

// export as SVG image
//$barcodeobj->getBarcodeSVG(2, 30, 'black');

// export as PNG image
//$barcodeobj->getBarcodePNG(2, 30, array(0,128,0));

// export as HTML code
echo $barcodeobj->getBarcodeHTML(2, 30, 'black');

查看http://www.tcpdf.org 上的文档和示例以获取更多信息。

您可以将您的条形码编号设为伪造的 HTML 标签,然后在您写出 HTML 时解析该标签,如本例所示。

这将在您的 HTML 中:

some HTML.... <POSTNET>12345-1234</POSTNET> ....some more HTML

这是解析假标签的代码。

        // look to see if there is a POSTNET tag
        if (strpos($letter_html, "<POSTNET>") !== false) {
            $postnet_pre = explode("<POSTNET>", $letter_html);
            $this->WriteHTML($postnet_pre[0], $this->line_height);

            // write the barcode
            $postnet_post = explode("</POSTNET>", $postnet_pre[1]);
            $zip_code = $postnet_post[0];
            $this->write1DBarcode($zip_code, 'POSTNET', '', '', 80, 15, 0.4, $style, 'N');

            // write rest of the letter
            $this->WriteHTML($postnet_post[1], $this->line_height);

        } else {

            // no POSTNET so just write the whole letter
            $this->WriteHTML($letter_html, $this->line_height);
        }

我无法在html文件中使用write html方法打印2dqrcode

我尝试了以下方法并且有效:

$params = $pdf->serializeTCPDFtagParameters(
    array('https://tcpdf.org/', 'QRCODE,H', '', '', 27, 27, '', 'N')
);

$html .= '<tcpdf method="write2DBarcode" params="'.$params.'" />';

生成条形码时,请确保将 12 位交货点括在“斜线”字符内。 大多数 POSTNET 字体将斜杠字符呈现为“控制”字符,用于预先/后修复条形码值。 没有这些控制字符,条形码在技术上是无效的。

可以下载 TrueType 格式的POSTNET 条码字体

暂无
暂无

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

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