2016-02-24 86 views
1

請我需要更換條碼模板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> 

'); 
+0

它看起來像所有你需要的是$ tmp_product設置爲'PDF_TEMPLATE_PROD的價值'在替換代碼之前。當你在第一個str_replace之前添加這個時會發生什麼:'$ tmp_product = constant('PDF_TEMPLATE_PROD');'? – Stiliyan

+0

它沒有區別,仍然是一個問題 – Patcino

+0

究竟是什麼問題?你看到了什麼輸出? – Stiliyan

回答

0

根據TCPDFBarcode documentation文檔,TCPDFBarcode類爲您提供了一種可用於d顯示條形碼:

  • 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; 
+0

感謝您的幫助,這工作正常,最好的問候。 – Patcino