有谁知道在PHP中编辑PDF的好方法?优选地是开源/零许可证成本方法.:)
我正在考虑打开PDF文件,替换PDF中的文本然后写出PDF的修改版本?
我过去使用FPDF以编程方式创建了PDF文件,但有时发现它有点笨拙.
gro*_*rom 67
如果您采用"填空"方法,则可以在页面上的任何位置精确定位文本.因此,将缺少的文本添加到文档中相对容易(如果不是有点乏味).例如使用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');
Run Code Online (Sandbox Code Playgroud)
如果您尝试替换内联内容,例如"[占位符字符串]",则会变得更加复杂.虽然技术上可行,但您可能会弄乱页面布局.
PDF文档由一组原始绘图操作组成:此处为line,此处为image,其中为text chunk等.它不包含有关这些基元的布局意图的任何信息.
小智 40
有一个免费且易于使用的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');
Run Code Online (Sandbox Code Playgroud)
Ada*_*utt 19
如果您需要非常简单的PDF,那么Zend或FPDF就可以了.然而,我发现它们很难和令人沮丧.此外,由于API的工作方式,没有好的方法将内容与表示与业务逻辑分开.
出于这个原因,我使用dompdf,它自动将HTML和CSS转换为PDF文档.您可以像对HTML页面一样布局模板并使用标准HTML语法.您甚至可以包含外部CSS文件.库不完美,非常复杂的标记或css有时会被破坏,但我还没有发现其他任何有用的东西.