在php中使用fpdf修改现有pdf
我的服务器上有一个PDF库,下载时需要在每个页面上添加文本。我正在使用fpdf尝试打开文件,将文本附加到每个页面,关闭文件并提供给浏览器在php中使用fpdf修改现有pdf,php,pdf,fpdf,Php,Pdf,Fpdf,我的服务器上有一个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 h
$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页,阅读文档后不确定如何执行此操作
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);
如果您有任何错误,请告诉我,因为您希望所有页面都包含文本,一种方法是将代码放入循环中 像这样:
// 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");
}
//获取页面总数
$pages\u count=$pdf->setSourceFile('your\u file.pdf');
对于($i=1;$i 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,“这只是一个简单的文本”);
}
您可以使用Dhek图形工具设计JSON格式的模板,定义稍后要添加到现有PDF(使用FPDF和动态数据)上的区域(边界、名称等)。请参阅。上的文档,仅在$x和$y中使用空值$outPdf->useTemplate($outPdf->importPage($i),空值,空值,0,0,真值);
。否则它会将页面剪切为A4。