【问题标题】:how to write barcode in html format when using tcpdf使用tcpdf时如何以html格式编写条形码
【发布时间】:2011-02-17 12:49:13
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: php pdf pdf-generation barcode tcpdf


    【解决方案1】:

    您可以在 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

    【讨论】:

      【解决方案2】:

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

      2D 示例:

      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 的文档和示例以获取更多信息。

      【讨论】:

        【解决方案3】:

        您可以将您的条形码编号设置为一个虚假的 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);
                }
        

        【讨论】:

          【解决方案4】:

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

          可以下载TrueType格式的POSTNET barcode font

          【讨论】:

            【解决方案5】:

            我尝试了以下方法并且成功了:

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

            【讨论】:

            • 不适合我,你能分享完整的代码吗
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-04
            • 1970-01-01
            相关资源
            最近更新 更多