用PHP进行PDF编辑?
有谁知道用PHP编辑PDF的好方法?最好是开源/零许可证费用方法。 :)
我正在考虑打开PDF文件,替换PDF中的文本然后写出PDF的修改版本的方法吗?
我过去使用FPDF以编程方式创建PDF文件,但有时发现它有些笨拙。
解决方案
回答
我们使用pdflib从Rails应用程序创建PDF文件。它具有PHP和许多其他语言的绑定。
我们使用商业版本,但它们也有一个自由/开源版本,这有一些限制。
不幸的是,这仅允许创建PDF。
如果我们要打开和"编辑"现有文件,pdflib会提供提供此功能的产品,但要花费很多
回答
Zend Framework可以加载和编辑现有的PDF文件。我认为它也支持修订。
我使用它在项目中创建文档,并且效果很好。从来没有编辑过。
在这里查看文档
回答
如果我们采用"填空"方法,则可以将文本精确地定位在页面上的任何位置。因此,将丢失的文本添加到文档中相对容易(即使不是很乏味)。例如,使用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');
如果我们要替换内联内容,例如" [占位符字符串]",它将变得更加复杂。尽管从技术上讲是可行的,但我们很可能会弄乱页面的布局。
PDF文档由一组原始绘图操作组成:此处的行,此处的图像,此处的文本块等。它不包含有关这些基元的布局意图的任何信息。
回答
我建议我们使用Zend库,而PHP中的PDF / pdflib扩展文档很少(在bugs.php.net中已指出)。
回答
如果我们需要非常简单的PDF,则Zend或者FPDF很好。但是我发现他们很难和沮丧地合作。同样,由于API的工作方式,也没有很好的方法将内容与表示和业务逻辑分开。
因此,我使用dompdf,它将自动将HTML和CSS转换为PDF文档。我们可以像处理HTML页面一样布置模板,并使用标准HTML语法。我们甚至可以包含一个外部CSS文件。该库不是完美的,非常复杂的标记或者CSS有时会被弄乱,但我还没有发现其他可行的方法。
回答
不知道这是否是一个选项,但是它的工作方式与Zend的pdf库非常相似,但是我们不需要加载一堆额外的代码(zend框架)。它只是扩展了FPDF。
http://www.setasign.de/products/pdf-php-solutions/fpdi/
在这里,我们基本上可以做同样的事情。加载PDF,在其顶部覆盖,然后保存到新的PDF。在FPDI中,我们基本上将PDF插入为图像,因此可以将所需的内容放在其上。
但这又使用了FPDF,因此,如果我们不想使用它,那么它将无法正常工作。
回答
我对dompdf确实抱有很高的期望(这是一个很不错的主意),但是定位问题是我使用fpdf的主要因素。尽管这很乏味,因为必须设置每个元素。当所有人离开时,它是强大的。
我在文档中的工作区下面放置了一个图像,以将布局放在合适的位置。即使对于列,它也总是足够的(只需要一点点php字符串计算,但是没有什么太令人头疼的了)。
祝你好运。
回答
<?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); ?>
回答
有一个免费且易于使用的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');