用 PHP 编辑 PDF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:09:14  来源:igfitidea点击:

PDF Editing in PHP?

phppdf

提问by kaybenleroll

Does anyone know of a good method for editing PDFs in PHP? Preferably open-source/zero-license cost methods. :)

有谁知道在 PHP 中编辑 PDF 的好方法?最好是开源/零许可成本的方法。:)

I am thinking along the lines of opening a PDF file, replacing text in the PDF and then writing out the modified version of the PDF?

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

I have programmatically created PDF files in the past using FPDF, but found it a little unwieldy at times.

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

采纳答案by grom

If you are taking a 'fill in the blank' approach, you can precisely position text anywhere you want on the page. So it's relatively easy (if not a bit tedious) to add the missing text to the document. For example with Zend Framework:

如果您采用的是“填空”方法,您可以将文本精确地放置在页面上您想要的任何位置。因此,将丢失的文本添加到文档中相对容易(如果不是有点乏味)。例如使用 Zend 框架:

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

If you're trying to replace inline content, such as a "[placeholder string]," it gets much more complicated. While it's technically possible to do, you're likely to mess up the layout of the page.

如果您尝试替换内联内容,例如“[占位符字符串]”,则会变得更加复杂。虽然这在技术上是可行的,但您可能会弄乱页面的布局。

A PDF document is comprised of a set of primitive drawing operations: line here, image here, text chunk there, etc. It does not contain any information about the layout intent of those primitives.

PDF 文档由一组图元绘制操作组成:此处为线条、此处为图像、此处为文本块等。它不包含有关这些图元布局意图的任何信息。

回答by metatron

There is a free and easy to use PDF class to create PDF documents. It's called FPDF. In combination with FPDI (http://www.setasign.de/products/pdf-php-solutions/fpdi) it is even possible to edit PDF documents. The following code shows how to use FPDF and FPDI to fill an existing gift coupon with the user data.

有一个免费且易于使用的 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');

回答by AdamTheHutt

If you need really simple PDFs, then Zend or FPDFis fine. However I find them difficult and frustrating to work with. Also, because of the way the API works, there's no good way to separate content from presentation from business logic.

如果您需要非常简单的 PDF,那么 Zend 或FPDF就可以了。但是,我发现与他们合作很困难且令人沮丧。此外,由于 API 的工作方式,没有将内容与表示与业务逻辑分开的好方法。

For that reason, I use dompdf, which automatically converts HTML and CSS to PDF documents. You can lay out a template just as you would for an HTML page and use standard HTML syntax. You can even include an external CSS file. The library isn't perfect and very complex markup or css sometimes gets mangled, but I haven't found anything else that works as well.

出于这个原因,我使用dompdf,它会自动将 HTML 和 CSS 转换为 PDF 文档。您可以像对 HTML 页面一样布置模板并使用标准 HTML 语法。您甚至可以包含一个外部 CSS 文件。该库并不完美,非常复杂的标记或 css 有时会被破坏,但我还没有找到任何其他有效的方法。

回答by Darryl Hein

Don't know if this is an option, but it would work very similar to Zend's pdf library, but you don't need to load a bunch of extra code (the zend framework). It just extends FPDF.

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

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

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

Here you can basically do the same thing. Load the PDF, write over top of it, and then save to a new PDF. In FPDI you basically insert the PDF as an image so you can put whatever you want over it.

在这里你基本上可以做同样的事情。加载 PDF,在上面书写,然后保存为新的 PDF。在 FPDI 中,您基本上将 PDF 作为图像插入,这样您就可以在上面放任何您想要的内容。

But again, this uses FPDF, so if you don't want to use that, then it won't work.

但同样,这使用了 FPDF,所以如果你不想使用它,那么它就行不通了。

回答by Juan

Zend Framework can load and edit existing PDF files. I think it supports revisions too.

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

I use it to create docs in a project, and it works great. Never edited one though.

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

Check out the doc here

这里查看文档

回答by Mufaddal

Tcpdf is also a good liabrary for generating pdf in php http://www.tcpdf.org/

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

回答by Ross

The PDF/pdflib extension documentation in PHP is sparse (something that has been noted in bugs.php.net) - I reccommend you use the Zend library.

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

回答by josh.chavanne

I really had high hopes for dompdf (it is a cool idea) but the positioning issue are a major factor in my using fpdf. Though it is tedious as every element has to be set; it is powerful as all get out.

我真的对 dompdf 寄予厚望(这是一个很酷的主意),但定位问题是我使用 fpdf 的主要因素。虽然它很乏味,因为每个元素都必须设置;当所有人都出去时,它是强大的。

I lay an image underneath my workspace in the document to put my layout on top of to fit. Its always been sufficient even for columns (requires a tiny bit of php string calculation, but nothing too terribly heady).

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

Good luck.

祝你好运。

回答by Orion Edwards

We use pdflibto create PDF files from our rails apps. It has bindings for PHP, and a ton of other languages.

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

We use the commmercial version, but they also have a free/open source versionwhich has some limitations.

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

Unfortunately, this only allows creation of PDF's.

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

If you want to open and 'edit' existing files, pdflib do provide a product which does this this, but costs a LOT

如果您想打开和“编辑”现有文件,pdflib 确实提供了一个可以执行此操作的产品,但要花费很多

回答by Nitin

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