【问题标题】:Rotate text in FPDF+FPDI在 FPDF+FPDI 中旋转文本
【发布时间】:2016-11-23 16:53:15
【问题描述】:

我遇到了关于 FPDF+FPDI 中文本旋转的问题... 我找到了一个类,但是它不起作用。

我需要将第一个文本旋转 180°。

到目前为止我的工作代码:

$pdf = new FPDI('P','mm',array(225.37,261.719));
$pdf->SetMargins(0,0,0);
$pdf->SetAutoPageBreak(true,0);
$pdf->AddFont('Chrioc','','CHRIOC__.php');
$mid_x=112.685;

$pdf->AddPage();
$pdf->setSourceFile("source/empty.pdf");
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx,0,0,225.37,261.719);
$pdf->SetFont('Chrioc','',55);
$pdf->SetTextColor(255,255,255);
$pdf->Text($mid_x-($pdf->GetStringWidth("aaa")/2),100,"aaa");
$pdf->Text($mid_x-($pdf->GetStringWidth("bbb")/2),168,"bbb");

$pdf->Output(); ?>

非常感谢!

【问题讨论】:

    标签: text rotation fpdf fpdi


    【解决方案1】:

    我设法使用 url page 中使用的类来旋转我的 pdf 中的文本

    基本上,我创建了一个类,其函数以 FPDI 作为父级,并导入到我的代码中,而不是

    // Your code    
    $pdf = new FPDI('P','mm',array(225.37,261.719));
    // Import changed
    $pdf = new PDF_Rotate('P','mm',array(225.37,261.719));
    

    那我就可以用了

    $pdf->SetXY(2, -5);
    $pdf->RotatedText(5,250,"Example of rotated text",90);
    

    完整代码如下: PDF_Rotate.php(从 FPDI 导入而不是 FPDF 作为示例)

    class PDF_Rotate extends \FPDI
    {
        var $angle=0;
    
        function Rotate($angle,$x=-1,$y=-1)
        {
            if($x==-1)
                $x=$this->x;
            if($y==-1)
                $y=$this->y;
            if($this->angle!=0)
                $this->_out('Q');
            $this->angle=$angle;
            if($angle!=0)
            {
                $angle*=M_PI/180;
                $c=cos($angle);
                $s=sin($angle);
                $cx=$x*$this->k;
                $cy=($this->h-$y)*$this->k;
                $this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
            }
        }
    
        function _endpage()
        {
            if($this->angle!=0)
            {
                $this->angle=0;
                $this->_out('Q');
            }
            parent::_endpage();
        }
    
        function RotatedText($x,$y,$txt,$angle)
        {
            //Text rotated around its origin
            $this->Rotate($angle,$x,$y);
            $this->Text($x,$y,$txt);
            $this->Rotate(0);
        }
    
        function RotatedImage($file,$x,$y,$w,$h,$angle)
        {
            //Image rotated around its upper-left corner
            $this->Rotate($angle,$x,$y);
            $this->Image($file,$x,$y,$w,$h);
            $this->Rotate(0);
        }
    }
    

    在另一个文件中,导入类。

    $pdf = new  PDF_Rotate();
    $pdf->SetXY(2, -5);
    $pdf->RotatedText(5,250,"Example of rotated text",90);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-07
      • 2012-08-24
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2011-12-24
      相关资源
      最近更新 更多