2014-04-30 152 views
0

我使用下面的代碼結合tfpdf和FPDI

<?php 

$hd1 = $_POST["hd1"]; 
require_once('fpdi.php'); 
require_once('fpdf.php'); 
require('tfpdf.php'); 

$pdf =& new FPDI(); 

$pagecount = $pdf->setSourceFile('template.pdf'); 
$tplIdx = $pdf->importPage(1); 
$s = $pdf->getTemplatesize($tplIdx); 
$pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L', array($s['w'], $s['h'])); 
$pdf->useTemplate($tplIdx,0, 0, 0, 0, true); 

$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true); 
$pdf->AddFont('DejaVu', 'B', 'DejaVuSansCondensed-Bold.ttf', true); 


$pdf->SetFont('DejaVu', '', 14); 

$pdf->Cell(50,10,$hd1,0,1); 
// Select a standard font (uses windows-1252) 
$pdf->SetFont('Arial', '', 14); 
$pdf->Ln(10); 
$pdf->Write(5, 'The file uses font subsetting.'); 

$pdf->Output('doc.pdf', 'I'); 
?> 

我得到錯誤:

Fatal error: Class 'FPDF' not found in C:\xampp\htdocs\san\ak-form\pdf\fpdi_bridge.php on line 33 

當我用下面從http://www.setasign.com/products/fpdi/demos/tfpdf-demo/

<?php 

$hd1 = $_POST["hd1"]; 

// require tFPDF 
require_once('tfpdf.php'); 

// map FPDF to tFPDF so FPDF_TPL can extend it 
class FPDF extends tFPDF 
{ 
    /** 
    * "Remembers" the template id of the imported page 
    */ 
    protected $_tplIdx; 

    /** 
    * Draw an imported PDF logo on every page 
    */ 
    public function Header() 
    { 
     if (is_null($this->_tplIdx)) { 
      $this->setSourceFile("template.pdf"); 
      $this->_tplIdx = $this->importPage(1); 
     } 
     $size = $this->useTemplate($this->_tplIdx, 130, 5, 60); 

     $this->SetFont('DejaVu', 'B', 16); 
     $this->SetTextColor(0); 
     $this->SetXY($this->lMargin, 5); 

     $text = 'tFPDF (v' . tFPDF_VERSION . ') and FPDI (v' 
       . FPDI::VERSION . ')'; 
     $this->Cell(0, $size['h'], $text); 
     $this->Ln(); 
    } 
} 

// just require FPDI afterwards 
require_once('fpdi.php'); 

// initiate PDF 
$pdf = new FPDI(); 

// Add some Unicode font (uses UTF-8) 
$pdf->AddFont('DejaVu', '', 'DejaVuSansCondensed.ttf', true); 
$pdf->AddFont('DejaVu', 'B', 'DejaVuSansCondensed-Bold.ttf', true); 

// add a page 
$pdf->AddPage(); 

$pdf->SetFont('DejaVu', '', 14); 

// Select a standard font (uses windows-1252) 
$pdf->SetFont('Arial', '', 14); 
$pdf->Ln(10); 
$pdf->Write(5, 'The file uses font subsetting.'); 

$pdf->Output('doc.pdf', 'I'); 
?> 

代碼,並獲得以下錯誤:

Warning: fopen(/home/content/w/i/s/wiseinmotion/html/test3/pdf/font/unifont/DejaVuSansCondensed.ttf): failed to open stream: No such file or directory in C:\xampp\htdocs\san\ak-form\pdf\font\unifont\ttfonts.php on line 496 
Can't open file /home/content/w/i/s/wiseinmotion/html/test3/pdf/font/unifont/DejaVuSansCondensed.ttf 

什麼是正確的方法,以便我可以使用exiting pdf並可以使用utf-8字體?

回答

0

你問兩個不同的問題在這裏:

  1. 爲什麼不是第一個代碼示例中定義FPDF

  2. 爲什麼不能加載我的字體在第二個代碼樣品。

我先回答#2,因爲很簡單:字體文件沒有找到。檢查你的路徑,並確保代碼在正確的位置查找字體。

關於#1:我懷疑問題歸結爲您的require_once陳述。具體來說,當示例代碼不需要時,您需要在代碼中使用fpdf.php。我的猜測是那裏有衝突。

與其花費大量的時間四處尋找一個問題的圖書館,我將與示例代碼(這是從開發者的網站開始,所以我們非常確保它工作 - 也許不是,但我們假設它確實如此,直到證明其他情況)假設代碼有效(並且你需要做的就是修復你的字體的路徑),從那開始並修改它以做你想做的事。

+0

你是對的。雖然路徑正確,但我從以前的工作中下載了tfpdf。它會導致問題。現在我正在使用新鮮的tfpdf。它運作良好。 –