PDF编辑在PHP中?

有谁知道在PHP中编辑PDF文件的好方法吗? 优选开源/零许可成本方法。 🙂

我正在考虑打开PDF文件,replacePDF中的文本,然后写出修改后的PDF版本?

我以前使用FPDF编程创buildPDF文件,但有时发现它有点笨拙。

如果您正在采取“填补空白”的方法,您可以在页面上的任何位置精确定位文本。 因此,将缺失的文本添加到文档中是相对容易的(如果不是很乏味)。 例如使用Zend Framework:

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

如果您试图replace内联内容,例如“[占位符string]”,则会变得更加复杂。 尽pipe技术上可行,但您可能会弄乱页面的布局。

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

有一个免费的,易于使用的PDF类创buildPDF文档。 这就是所谓的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'); 

如果你需要真正简单的PDF,那么Zend或FPDF是好的。 不过,我发现他们很难和令人沮丧的工作。 另外,由于API的工作方式,没有好的方法将内容从演示文稿和业务逻辑中分离出来。

出于这个原因,我使用dompdf ,它自动将HTML和CSS转换为PDF文档。 您可以像为HTML页面一样布置模板,并使用标准的HTML语法。 你甚至可以包含一个外部的CSS文件。 图书馆是不完美的,非常复杂的标记或CSS有时会受到损坏,但我还没有发现任何其他的工作。

Zend Framework可以加载和编辑现有的PDF文件。 我认为它也支持修改。

我用它在一个项目中创build文档,而且效果很好。 从来没有编辑过。

看看这里的文档

不知道这是否是一个选项,但它的工作方式与Zend的pdf库非常相似,但不需要加载一堆额外的代码(zend框架)。 它只是扩展了FPDF。

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

在这里,你基本上可以做同样的事情。 加载PDF,写在上面,然后保存到一个新的PDF。 在FPDI中,您基本上将PDF插入为图像,以便您可以随意放置任何内容。

但是,这又一次使用FPDF,所以如果你不想使用它,那么它将无法工作。

PHP中的PDF / pdflib扩展文档是稀疏的(在bugs.php.net中已经注意到了这一点) – 我推荐你使用Zend库。

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

我真的对dompdf抱有很高的期望(这是一个很酷的想法),但是定位问题是我使用fpdf的一个主要因素。 虽然每一个元素都必须设置,但它是乏味的; 所有人都出去了

我在文档的工作区下面放置一个图像,以便将我的布局放在顶部以适应。 即使对于列来说,它总是足够的(需要一点点的PHPstring计算,但没有什么太可怕了)。

祝你好运。

Tcpdf也是一个很好的负责生成PDF在PHP http://www.tcpdf.org/

我们使用pdflib从我们的rails应用程序创buildPDF文件。 它绑定了PHP和其他许多种语言。

我们使用的商业版本,但他们也有一个自由/开放源码版本 ,有一些限制。

不幸的是,这只允许创buildPDF文件。

如果你想打开和“编辑”现有的文件,pdflib确实提供了这样做的产品 ,但成本很高