【问题标题】:Adding vertical text with fpdf/fpdi in php在 php 中使用 fpdf/fpdi 添加垂直文本
【发布时间】:2012-10-25 12:55:11
【问题描述】:

我有一个从数据库 blob 构建多页 pdf 的脚本。这个 pdf 可以工作并输出良好的电流,但我需要在每页的左侧添加一行垂直的文本。我已经设法让它适用于一些 pdf,但对于一些我得到一个损坏的文件错误。有没有人有不同的添加垂直文本的方法我可以尝试使用 fpdf/fpdi。

这是我目前所拥有的:

function buildBSIPDF($filename){
global $supplier;

$pdf = new FPDI();
$i = 1;
$pagecount = $pdf->setSourceFile($filename);

//create text to append
$sideline = "Some text here";

while($i <= $pagecount){

    //$pdf->setSourceFile($filename); 
    // import page 1 
    $tplIdx = $pdf->importPage($i); 
    //use the imported page and place it at point 0,0; calculate width and height
    //automaticallay and ajust the page size to the size of the imported page 

    //$s = $pdf->getTemplatesize($tplidx); 
    $pdf->AddPage();
    $pdf->useTemplate($tplIdx); 

    // now write some text above the imported page 
    $pdf->SetFont('Arial', '', '12'); 
    $pdf->SetTextColor(0,0,0);
    //set position in pdf document
    $pdf->SetXY(20, 20);
    //first parameter defines the line height
    $pdf->RotatedText(5,250,$sideline,90);
    $i++;
}
$pdf->Output($filename, 'F');

}

【问题讨论】:

    标签: php pdf


    【解决方案1】:

    试试这个:

    class PDF extends FPDF { 
    
    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));
         } 
      } 
    } 
    
    $pdf=new PDF(); 
    $pdf->AddPage(); 
    $pdf->SetFont('Arial','',10); 
    $this->Rotate(10); 
    $pdf->Write(20,'this text is crooked');
    $this->Rotate(0); 
    

    【讨论】:

    • 感谢您的回答,但旋转文本仍然出现相同的错误。它适用于无角度的文本,即使使用您的函数但角度为 0 也可以。尽管我在 acrobat 中打开 pdf 并收到错误消息,但只要打开任何角度:“此页面上存在错误。Acrobat 可能无法正确读取该页面”
    • 是的,其他 pdf 阅读器很好,我已经缩小了此处概述的与 pdf 中不平衡的 Q 和 q 运算符有关的问题。然而,我的 OOP 知识是有限的,所以我不知道在实现这些人所说的方面我是否在循环方面做错了 - board.phpbuilder.com/…
    • @MattWade 据了解,你需要把 $this->Rotate(0); 或用你的话 $pdf->Rotate( 0);.这应该设置平衡“q”和“Q”。不知道这些是什么。一定是与pdf格式规范相关的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多