使用 fpdf 修改 php 中现有的 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12510117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using fpdf to modify existing pdf in php
提问by Steve Smith
I have a bank of pdfs on my server which when downloaded need text appended to every page. I am using fpdf to try and open the file, append the text to each page, close the file and serve to the browser.
我的服务器上有一组 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);
At the minute this just tries to put some text anywhere on the pdf and save it but I get the error
目前,这只是尝试在 pdf 上的任何位置放置一些文本并保存,但我收到错误
FPDF error: You have to add a page first!
If I can get it to do this I then need it to append the text to every page in the document rather than just 1, not sure how to do this having read the documentation
如果我可以做到这一点,那么我需要它将文本附加到文档中的每一页,而不仅仅是 1,不知道如何在阅读文档后做到这一点
回答by GBD
Try following
尝试以下
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pdf->AddPage();
Use this page as template, then
使用此页面作为模板,然后
$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);
Let me know if you have any errors
如果您有任何错误,请告诉我
回答by Flávio Silveira
Since you want all the pages with the text, one way to do it is putting the code in a loop.
由于您希望所有页面都带有文本,因此一种方法是将代码放入循环中。
Like this:
像这样:
// 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");
}
回答by cchantep
You can use Dhek graphical tool to design a template in JSON format, defining areas (bounds, name, ...) you want to add later on existing PDF (using FPDF and dynamic data). See doc at https://github.com/cchantep/dhek/blob/master/README.md#php-integration.
您可以使用 Dhek 图形工具设计 JSON 格式的模板,定义稍后要在现有 PDF 上添加的区域(边界、名称等)(使用 FPDF 和动态数据)。请参阅https://github.com/cchantep/dhek/blob/master/README.md#php-integration 上的文档。

