2010-05-10 213 views
1

當我使用TCPDF用來生成PDF文件,下面的命令寫入HTML格式的條形碼如何使用TCPDF

$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代碼中調用一個條形碼圖像,但不知道如何使用上面的條形碼函數(或任何在TCPDF中),這將允許我創建圖像,然後將該圖像轉換爲HTML代。

回答

2

你可以把你的條形碼號碼是一個假的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); 
     } 
+0

文件不以%PDF錯誤開頭。你能幫我嗎? – abel 2010-08-27 11:42:07

10

你可以寫在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

+0

+1這一個工作 – jned29 2014-10-29 03:06:13

7

TCPDF條碼類已經包含方法導出各種格式的條形碼(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'); 

1D例如:

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進一步的信息。

0

生成條形碼時,請確保將12位交貨點放在「斜槓」字符內。大多數POSTNET字體會將斜槓字符顯示爲預先修復條形碼值的「控制」字符。如果沒有這些控制字符,條形碼在技術上不是有效的。

TrueType格式的POSTNET barcode font可以下載。