在 HTML 模板 tcpdf 中写入条码

Write barcode in HTML Template tcpdf

我需要替换模板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>

');

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

  • getBarcodeHTML - returns 条形码的 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;