在PHP中编辑PDF?

89

有没有人知道用PHP编辑PDF的好方法?最好是开源/零许可证成本的方法。 :)

我想的是打开一个PDF文件,替换其中的文本,然后输出修改后的PDF版本?

在前端方面


我只用过FPDF,觉得它非常棒。真的非常棒。 - SQLMason
我也在尝试寻找一个快速的解决方案。我想要每个产品页面都有相同的PDF文件,但是每个PDF文件中的产品编号和产品名称都不同。我发现使用邮件合并(Word或OpenOffice)实际上是最简单的方法。然后我导出了所有的PDF文件并上传它们。希望这能帮助到某些人。 - NotJay
为什么它被认为不够专注? - Gherman
9个回答

72

如果你采用“填空”的方法,你可以在页面上精确地定位文本。因此,向文档中添加缺失的文本相对容易(尽管可能有点繁琐)。例如,使用Zend框架:

<?php
require_once 'Zend/Pdf.php';

$pdf = Zend_Pdf::load('blank.pdf');
$page = $pdf->pages[0];
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 12);
$page->drawText('Hello world!', 72, 720);
$pdf->save('zend.pdf');

如果您想替换内联内容,比如"[placeholder string]",那么情况会变得更加复杂。虽然在技术上是可能的,但很可能会破坏页面的布局。

PDF文档由一组原始绘图操作组成:这里有一条线,这里有一个图像,那里有一块文本等。它不包含任何关于这些基元的布局意图的信息。


1
太好了!我没想到Zend Framework是免费的,我之前被专有的Zend Studio搞混了。 - Liam
8
提醒大家注意,如果您想使用这个工具的话,需要注意:它只能处理在Acrobat 4版本或之前创建的PDF文件。从Acrobat 4版本以后,Adobe开始对文件进行编码,使得对PDF文件进行编辑(或导入到其他PDF文件中)更加困难。 - Darryl Hein
4
PDF 1.4支持已经添加。 - Raphael Schweikert
2
现在没有 Zend,这个能正常工作吗? - William Entriken
1
有没有什么解决方案可以替换像[占位符字符串]这样的内容? - Starx
显示剩余3条评论

52

有一个免费且易于使用的PDF类可用于创建PDF文档。它叫做FPDF。与FPDI (http://www.setasign.de/products/pdf-php-solutions/fpdi) 结合使用,甚至可以编辑PDF文档。下面的代码展示了如何使用FPDF和FPDI来填写现有礼品券以及用户数据。

require_once('fpdf.php'); 
require_once('fpdi.php'); 
$pdf = new FPDI();

$pdf->AddPage(); 

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

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

2
优秀的库 - 正是我想要的,感谢您的推荐。 - james-geldart
1
这很好,但在某些PDF文件中会出现以下错误:““FPDF错误:此文档(testcopy.pdf)可能使用一种不受FPDI附带的免费解析器支持的压缩技术。””有什么解决方法吗? - user1360768
您需要将PDF转换为旧版本(PDF 1.4),或使用FPDI商业版。我们提供一个解析器替换作为单独的商业附加组件,使FPDI能够处理使用这些压缩功能的文档。来源 - SwissFr
能否从另一个网页设置 setSourceFile?例如:example.com/file.pdf? - Alex

20
如果你需要非常简单的PDF,那么Zend或FPDF就可以了。然而我发现它们很难使用并且让人沮丧。而且,由于API的工作方式,没有好的方法将内容与呈现和业务逻辑分开。
因此,我使用dompdf,它可以自动将HTML和CSS转换为PDF文档。您可以像为HTML页面一样布置模板并使用标准HTML语法。您甚至可以包含一个外部CSS文件。该库不是完美的,非常复杂的标记或css有时会被破坏,但我没有找到其他任何工具能够像这样良好地工作。

10
由于它没有回答问题,发帖者想编辑现有的PDF而不是从头创建PDF。 - Reimund
13
我来到这个页面是因为我想编辑一个PDF文件,但这个答案似乎更有用,因为我可以看到为什么从头开始用HTML构建可能比编辑现有的PDF更容易。 - Justin Vincent

3
Zend框架可以加载和编辑现有的PDF文件。我认为它也支持修订。
我在项目中使用它来创建文档,效果很好。虽然从未编辑过。
请查看此文档:这里

3

不确定是否可以采用此选项,但它与Zend的pdf库非常相似,但您不需要加载大量额外的代码(Zend框架)。它只是扩展了FPDF。

http://www.setasign.de/products/pdf-php-solutions/fpdi/

在这里,您基本上可以执行相同的操作。加载PDF文件,覆盖其上方,并将其保存为新的PDF文件。在FPDI中,您基本上将PDF文件作为图像插入,因此可以在其上放置任何内容。

但是,请注意,这需要使用FPDF,因此如果您不想使用FPDF,则无法实现该功能。


2

PHP中PDF/pdflib扩展的文档很稀少(在bugs.php.net上已经有人注意到了)- 我建议您使用Zend库。


1

对于所有寻求轻松添加页眉和页脚方法的人,TCPDF有一个非常简单而有效的示例:https://tcpdf.org/examples/example_003/ - Alen Šimunic
4
tcPDF 用于生成新的 PDF,这个问题是关于修改现有的 PDF。 - Gherman

-1
我们使用pdflib从我们的rails应用程序创建PDF文件。它具有PHP和许多其他语言的绑定。
我们使用商业版本,但他们也有一个免费/开源版本,具有一些限制。
不幸的是,这仅允许创建PDF。
如果您想打开和“编辑”现有文件,pdflib提供一种产品可以做到这一点, 但价格非常昂贵很多

-2
<?php

//getting new instance
$pdfFile = new_pdf();

PDF_open_file($pdfFile, " ");

//document info
pdf_set_info($pdfFile, "Auther", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Creator", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Title", "PDFlib");
pdf_set_info($pdfFile, "Subject", "Using PDFlib");

//starting our page and define the width and highet of the document
pdf_begin_page($pdfFile, 595, 842);

//check if Arial font is found, or exit
if($font = PDF_findfont($pdfFile, "Arial", "winansi", 1)) {
    PDF_setfont($pdfFile, $font, 12);
} else {
    echo ("Font Not Found!");
    PDF_end_page($pdfFile);
    PDF_close($pdfFile);
    PDF_delete($pdfFile);
    exit();
}

//start writing from the point 50,780
PDF_show_xy($pdfFile, "This Text In Arial Font", 50, 780);
PDF_end_page($pdfFile);
PDF_close($pdfFile);

//store the pdf document in $pdf
$pdf = PDF_get_buffer($pdfFile);
//get  the len to tell the browser about it
$pdflen = strlen($pdfFile);

//telling the browser about the pdf document
header("Content-type: application/pdf");
header("Content-length: $pdflen");
header("Content-Disposition: inline; filename=phpMade.pdf");
//output the document
print($pdf);
//delete the object
PDF_delete($pdfFile);
?>

8
你在使用哪个库? - Hugo Estrada
1
pdf_set_info($pdfFile, "Title", "PDFlib"); - user183037
仍然不清楚这是哪个库。 - Deniz Celebi
我相信他指的是内置的PHP pdflib扩展。 https://www.php.net/manual/en/pdf.examples-basic.php - Kostiantyn
添加描述。 - Diego Lope Loyola

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接