2015-06-09 54 views
0

我一直在玩FPDF,並需要使用起始模板PDF創建PDF,以便使用FPDI進行設計。然後我需要使用我設法使用的非標準字體。然而......字母間距都是亂七八糟的(有些字母緊挨在一起,有些字母間隔很遠)。如何使用FPDF_CellFit與FPDI

我找到了FPDF_CellFit的附加解決方案,但該類擴展了FPDF,所以如何在使用FPDI時使其工作?這裏是我的代碼:

$pdf =& new FPDI(); 

$pdf->AddFont('AgencyFB-Reg','','agencyfbb.php'); 
$pdf->AddPage('mm', array(54,92)); 
$pdf->setSourceFile('test.pdf'); 
$tplIdx = $pdf->importPage(1); 
$pdf->useTemplate($tplIdx, 0, 0, 92); 
$pdf->SetFont('AgencyFB-Reg','',14); 
$pdf->SetXY(100, 27); 
$pdf->Write(0, "testing"); 
$pdf->Output(); 

但如上所述,當我看FPDF_CellFit類它開始是這樣的:

$pdf = new FPDF_CellFit(); 

class FPDF_CellFit extends FPDF { 
... ... ... 
} 

請幫助...我是如此接近!

+0

你看了[FAQ#14](http://fpdf.org/en/FAQ.php#q14)? –

+0

是的,但它仍然不清楚,因爲該示例結合了兩個類,否則這兩個類都擴展相同的類。在這種情況下,它是兩個類,每個類延伸一個不同的類。我可以想象這是一個類似的事情,但沒有榮耀。 –

回答

0

我已經制作了一個包來解決這個問題。您必須始終保持相同的PDF。所以當你給它增加一些效果時,它一直都是一樣的。

事實上,裝飾模式是一個很好的方法來做到這一點。

的裝飾類

<?php 
abstract class DecoratorPdf extends \FPDF { 
    protected $pdf; 

    public function __construct(\FPDF $pdf) { 
     $this->pdf = $pdf; 
    } 
} 

而且我已經做了脣裂類對象中這樣

<?php 

class FPDF_CellFit extends \DecoratorPdf { 
    //Cell with horizontal scaling if text is too wide 
    function CellFit($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $scale=false, $force=true) 
    { 
     //Get string width 
     $str_width=$this->pdf->GetStringWidth($txt); 
     //Calculate ratio to fit cell 
     if($w==0) 
      $w = $this->pdf->w-$this->pdf->rMargin-$this->x; 
     $ratio = ($w-$this->pdf->cMargin*2)/$str_width; 
     $fit = ($ratio < 1 || ($ratio > 1 && $force)); 
     .. 

所以現在,當你使用它,你創建延伸FPDF一個PDF類。因此,PDF將被創建,您可以調用擴展裝飾類的cellfit類,並在構造函數中添加您的PDF。所以你始終保持相同的PDF。

class Pdf extends \FPDI 
{ 
//Fonction qui représente une zone ciblée concernant le code postale de l'adresse du destinataire 

    function destinataireCodePostal($string) 
    { 
     // Helvetica 12 
     $this->SetFont('Helvetica',"",10); 

     //PdfDraw enrichie avec la pattern Decorator mon PDF 
     $cellfit = new \FPDF_CellFit($this); 

     //Position du code poste 
     $this->SetXY(102, 67.3); 

     //Permet d'espacer les lettres de manière égale 
     $cellfit->CellFitSpaceForce(15.5, 10, utf8_decode($string), 0, 0, 'l', 0); 

     //Retour à la police normal 
     $this->mainFont(); 
    } 

使用與作曲家,座位項目會自動加載

"require": { 
     "php": ">=5.3.3", 
     "setasign/fpdi-fpdf": "1.5.4" 
    }, 
    "autoload": { 
      "classmap": [ 
      "lib/draw/draw.php", 
      "lib/cellfit/cellfit.php", 
      "lib/DecoratorPdf.php" 
     ] 
    } 

全包的例子是在這裏,在lib文件夾我已經在這個對象進行抽獎和cellfit模式。

lib for cellfit in object