10

我的服务器上有一堆 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,不知道如何在阅读文档后执行此操作

4

3 回答 3

16

尝试关注

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); 

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

于 2012-09-20T10:03:57.127 回答
9

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

像这样:

// 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"); 
}
于 2013-11-29T12:54:07.827 回答
1

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

于 2014-08-01T08:55:25.677 回答