【问题标题】:Trying to write to a PDF file using FPDF and FPDI尝试使用 FPDF 和 FPDI 写入 PDF 文件
【发布时间】:2013-03-14 18:37:10
【问题描述】:

我收到了一个 PDF 文件,其中包含需要用数据库中的值填写的点。我将 FPDF 库与 FPDI 一起使用,定位文本似乎需要反复试验……我必须使用 X 和 Y 坐标才能将文本放置在正确的位置。似乎是一种繁琐且低效的方法。我在这里错过了什么吗?

 require_once('fpdf/fpdf.php');
 require_once('fpdi/fpdi.php');

 // initiate FPDI  
 $pdf = new FPDI();  
 // add a page
 $pdf->AddPage();  
 // set the sourcefile  
 $pdf->setSourceFile('1.pdf');  
 // import page 1  
 $tplIdx = $pdf->importPage(1);  
 // use the imported page and place it at point 10,10 with a width of 200 mm   (This is    the image of the included pdf)
 $pdf->useTemplate($tplIdx, 10, 10, 200);  
 // now write some text above the imported page
 $pdf->SetTextColor(0,0,0);

 $pdf->SetFont('Arial','B',20);  
 $pdf->SetXY(85, 50);  
 $pdf->Write(0, "Johnny Walker");
 $pdf->SetFont('Arial','B',11);
 $pdf->SetXY(92, 69); 
 $pdf->Write(0, "3"); 
 $pdf->SetFont('Arial','B',10);
 $pdf->SetXY(114, 76); 
 $pdf->Write(0, 'Ventilation Management of the Amyotropic Lateral Scleropsis'); 
 $pdf->SetFont('Arial','B',10);
 $pdf->SetXY(114, 90); 
 $pdf->Write(0, date('m/d/Y'));  
 $pdf->SetFont('Arial','B',7);
 $pdf->SetXY(66, 127); 
 $pdf->Write(0, '1.5');  
 $pdf->Output('1.pdf', 'I');

【问题讨论】:

    标签: php pdf fpdf


    【解决方案1】:

    我过去曾经使用过FPDF,你说得对,设置 XY 位置非常麻烦。不过好像没有别的办法了。

    我唯一的建议是考虑

    $pdf->SetXY($pdf->GetX() + $x_value, $pdf->GetY() +  $y_value) 
    

    在您不确定应该放置的确切位置的地方。

    【讨论】:

    • 是的,这完全荒谬。这是一个常见的问题,我不知道为什么没有标准化的解决方案。我想知道写word文件是否更容易......我不知道。顺便说一句,您的代码将坐标从当前位置移动水平方向的 X_value 和垂直方向的 y_value,对吗?
    • 嗯,我真的很喜欢使用 FPDF。它很容易使用,但是我想当您需要长时间处理PDF文件时,您需要一种比xy协调更好的方法。是的,这是正确的。这只是要记住的事情,因为有时您只需要在“最后一个”XY 位置之后添加几行。这样,您就不需要记住确切的坐标点。只需添加一些即可。
    • 将值添加到当前位置也解决了从变量写入文本但不知道要占用多少行的问题。通过 XY 的显式值,您最终可能会打印出彼此重叠的线条......类似这样的东西:D
    猜你喜欢
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多