【问题标题】:Using fpdf to modify existing pdf in php使用fpdf修改php中已有的pdf
【发布时间】:2012-09-12 16:06:45
【问题描述】:

我的服务器上有一堆 pdf,下载时需要将文本附加到每个页面。我正在使用 fpdf 尝试打开文件,将文本附加到每个页面,关闭文件并提供给浏览器。

$pdf = new FPDI();

$pdf->setSourceFile($filename); 
// import page 1 
$tplIdx = $pdf->importPage(1); 
//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 
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 

// now write some text above the imported page 
$pdf->SetFont('Arial', '', '13'); 
$pdf->SetTextColor(0,0,0);
//set position in pdf document
$pdf->SetXY(20, 20);
//first parameter defines the line height
$pdf->Write(0, 'gift code');
//force the browser to download the output
$pdf->Output('gift_coupon_generated.pdf', 'D');

header("location: ".$filename);

此刻,这只是尝试将一些文本放在 pdf 上的任何位置并保存,但我收到错误

FPDF error: You have to add a page first!

如果我能做到这一点,那么我需要它将文本附加到文档中的每一页,而不仅仅是 1,阅读文档后不确定如何做到这一点

【问题讨论】:

    标签: php pdf fpdf


    【解决方案1】:

    尝试关注

    require_once('fpdf.php');
    require_once('fpdi.php');
    
    $pdf =& new FPDI();
    $pdf->AddPage();
    

    使用此页面作为模板,然后

    $pdf->setSourceFile($filename); 
    // import page 1 
    $tplIdx = $pdf->importPage(1); 
    //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 
    $pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 
    

    如果您有任何错误,请告诉我

    【讨论】:

    • 仅在 $x 和 $y $outPdf->useTemplate($outPdf->importPage($i), null, null, 0, 0, true); 中使用 Nulls 为我工作。否则它会将页面剪切为 A4。
    【解决方案2】:

    由于您希望所有页面都包含文本,因此一种方法是将代码放入循环中。

    像这样:

    // Get total of the pages
    $pages_count = $pdf->setSourceFile('your_file.pdf'); 
    
    for($i = 1; $i <= $pages_count; $i++)
    {
        $pdf->AddPage(); 
    
        $tplIdx = $pdf->importPage($i);
    
        $pdf->useTemplate($tplIdx, 0, 0); 
    
    
        $pdf->SetFont('Arial'); 
        $pdf->SetTextColor(255,0,0); 
        $pdf->SetXY(25, 25); 
        $pdf->Write(0, "This is just a simple text"); 
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 Dhek 图形工具设计 JSON 格式的模板,定义您以后要在现有 PDF 上添加的区域(边界、名称、...)(使用 FPDF 和动态数据)。请参阅https://github.com/cchantep/dhek/blob/master/README.md#php-integration 的文档。

      【讨论】:

        猜你喜欢
        • 2011-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多