【问题标题】:Write barcode in HTML Template tcpdf在 HTML 模板 tcpdf 中写入条形码
【发布时间】:2016-02-26 09:11:51
【问题描述】:

我需要替换模板 PDF 中的条形码,

代码:

$tmp_product = str_replace("{::prod_price}", $product['price'], $tmp_product);
$eancodes = $product['ean']; 
$eancode =  new TCPDFBarcode($eancodes, 'EAN13'); 
$tmp_product = str_replace("{::prod_ean}", $eancode, $tmp_product);

模板 HTML:

 define('PDF_TEMPLATE_PROD', '  
<tr class="pdf_prod" id="{::prod_name}" nobr="true">         
  <td class="pdf_prod_desc">               
    <ul class="pdf_prod_ul">
      <li><strong>{::txt_prod_price}</strong> {::prod_price}</li>            
      <li class="pdf_prod_bcode">{::prod_ean}</li>
     </ul>
  </td> 
</tr>

');

【问题讨论】:

  • 看起来您只需要在替换代码之前将 $tmp_product 设置为 PDF_TEMPLATE_PROD 的值即可。当您在第一个 str_replace 之前添加它时会发生什么:$tmp_product = constant('PDF_TEMPLATE_PROD');
  • 没什么区别,还是有问题
  • 到底是什么问题?你看到了什么输出?
  • 未知:TCPDFBarcode 类的对象无法转换为字符串

标签: php tcpdf


【解决方案1】:

根据TCPDFBarcode documentation 文档,TCPDFBarcode 类为您提供了一种可用于显示条形码的方法:

  • getBarcodeHTML - 返回条形码的 HTML 表示。您可以将此 HTML 直接插入到您的 PDF_TEMPLATE_PROD 模板中

使用getBarcodeHTML 方法,您可以执行以下操作:

// define the HTML template you'll use to markup the product data
define('PDF_TEMPLATE_PROD', '  
    <tr class="pdf_prod" id="{::prod_name}" nobr="true">         
        <td class="pdf_prod_desc">               
            <ul class="pdf_prod_ul">
                <li><strong>{::txt_prod_price}</strong> {::prod_price}</li>            
                <li class="pdf_prod_bcode">{::prod_ean}</li>
            </ul>
        </td> 
    </tr>

    ');

// pass the template into a variable where you'll fill in the data
$tmp_product = constant('PDF_TEMPLATE_PROD');
// insert the price
$tmp_product = str_replace("{::prod_price}", $product['price'], $tmp_product);

// extract the data you want to encode and create your TCPDFBarcode object:
$eancodes = $product['ean'];
$eancode =  new TCPDFBarcode($eancodes, 'EAN13'); 

// insert the HTML represenation of the barcode and print the result
$tmp_product = str_replace("{::prod_ean}", $eancode->getBarcodeHTML(), $tmp_product);
echo $tmp_product;

【讨论】:

  • 感谢您的帮助,一切正常,最好的问候。
猜你喜欢
  • 1970-01-01
  • 2011-02-17
  • 2020-12-24
  • 2012-09-04
  • 2014-11-16
  • 2013-04-07
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
相关资源
最近更新 更多