用PHP编辑PDF?

用PHP编辑PDF?

PDF Editing in PHP?

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

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

我过去曾使用FPDF以编程方式创建PDF文件,但有时发现它有些笨拙。


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

1
2
3
4
5
6
7
8
9
<?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文档由一组原始绘图操作组成:此处的行,此处的图像,此处的文本块等。它不包含有关这些基元的布局意图的任何信息。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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的pdf库非常相似,但是您不需要加载一堆额外的代码(zend框架)。它只是扩展了FPDF。

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

在这里,您基本上可以做同样的事情。加载PDF,在其顶部覆盖,然后保存到新的PDF。在FPDI中,您基本上将PDF插入为图像,因此您可以将所需的内容放在其上。

但这又使用了FPDF,所以如果您不想使用它,那么它将无法正常工作。


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

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

在此处查看文档


Tcpdf也是在php中生成pdf的很好的库
http://www.tcpdf.org/


PHP中的PDF / pdflib扩展文档很少(在bugs.php.net中已指出)-我建议您使用Zend库。


我对dompdf确实抱有很高的期望(这是一个很酷的主意),但是定位问题是我使用fpdf的主要因素。尽管这很乏味,因为必须设置每个元素。所有人都可以脱身,功能强大。

我在文档中的工作区下面放置了一个图像,以将布局放在合适的位置。即使对于列,它也总是足够的(只需要一点点php字符串计算,但是没有什么太令人头疼的了)。

祝好运。


我们使用pdflib从Rails应用程序创建PDF文件。它具有PHP绑定和大量其他语言。

我们使用商业版本,但它们也有免费/开源版本,这有一些限制。

不幸的是,这仅允许创建PDF。

如果您想打开和"编辑"现有文件,pdflib会提供提供此功能的产品,但要花费很多


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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);
?>


推荐阅读