有没有人知道用PHP编辑PDF的好方法?最好是开源/零许可成本的方法。:)
我的思路是打开PDF文件,替换PDF中的文本,然后写出修改后的PDF版本?
在前端
发布于 2008-08-11 00:49:58
如果你采用“填充空白”的方法,你可以在页面上的任意位置精确定位文本。因此,将缺少的文本添加到文档中相对容易(如果不是有点繁琐的话)。例如,使用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文档由一组图元绘制操作组成:此处为直线,此处为图像,此处为文本块,等等。它不包含有关这些图元的布局意图的任何信息。
发布于 2010-11-15 20:31:15
有一个免费且易于使用的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');
https://stackoverflow.com/questions/7364
复制相似问题