【问题标题】:How to use FPDF_CellFit with FPDI如何将 FPDF_CellFit 与 FPDI 一起使用
【发布时间】:2016-04-20 15:58:33
【问题描述】:

我一直在玩 FPDF,需要使用起始模板 PDF 创建 PDF,因此设法使用 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 {
... ... ...
}

请帮助...我很接近!

【问题讨论】:

  • 你读过FAQ #14吗?
  • 是的,但仍然不清楚,因为该示例结合了两个类,否则它们都扩展同一个类。在这种情况下,它是 2 个类,每个类扩展一个不同的类。我可以想象这是类似的事情,但还没有荣耀。

标签: fpdf fpdi


【解决方案1】:

我已经制作了一个包来解决这个问题。您必须始终保持相同的 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 文件夹中,我在此对象模式中进行了绘制和单元格调整。

lib for cellfit in object

【讨论】:

    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多