【问题标题】:Edit PDF en PHP? [closed]用 PHP 编辑 PDF? [关闭]
【发布时间】:2010-09-05 15:52:44
【问题描述】:

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

我的想法是打开一个 PDF 文件,替换 PDF 中的文本,然后写出 PDF 的修改版本?

在前端

【问题讨论】:

  • 我只用过 FPDF,觉得它很棒。真的很棒。
  • 我也试图找到一个快速的解决方案。我希望我的每个产品页面都使用相同的 pdf,但在每个 pdf 上都替换了产品编号和产品名称。我发现使用邮件合并(Word 或 OpenOffice)实际上是最简单的方法。然后我导出了所有的 pdf 文件并上传了它们。希望这对某人有所帮助。
  • 为什么被认为不够专注?

标签: php pdf


【解决方案1】:

如果您采用“填空”的方法,您可以将文本精确地放置在页面上您想要的任何位置。因此,将缺少的文本添加到文档中相对容易(如果不是有点乏味的话)。以 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');

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

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

【讨论】:

  • 这太棒了!我没有意识到 Zend Framework 是免费的,我被专有的 Zend Studio 弄糊涂了。
  • 提醒任何尝试使用此功能的人:它仅适用于在 Acrobat 版本 4 及之前版本中创建的 PDF。在第 4 版之后,Adobe 开始对文件进行编码,这使得对 PDF 进行编辑(或导入其他 PDF)变得更加困难。
  • PDF 1.4 支持has since been added.
  • 在没有 Zend 的情况下现在可以工作吗?
  • 如果我想替换[占位符字符串]之类的内容,有什么解决方案吗?
【解决方案2】:

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

【讨论】:

  • 优秀的图书馆——正是我所追求的,感谢推荐
  • 这很好,但在某些 pdf 中会出现此错误"FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI." 有什么解决方案吗?
  • 您需要将您的pdf转换为旧版本(pdf 1.4)或使用fpdi商业版。 我们提供解析器替代品作为单独的商业插件,它使 FPDI 能够处理使用这些压缩功能的文档。 source
  • 是否可以从其他网页设置SourceFile?例如:example.com/file.pdf?
【解决方案3】:

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

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

【讨论】:

  • -1 因为它没有回答问题。发布者想要编辑现有的 PDF,而不是从头开始创建它们。
  • 我来到这个页面是因为我想编辑一个 PDF,但这个答案对我来说似乎更有用,因为我明白为什么在 html 中从头开始构建而不是编辑现有的 PDF 可能更容易.
【解决方案4】:

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

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

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

但同样,它使用 FPDF,所以如果您不想使用它,那么它将无法工作。

【讨论】:

    【解决方案5】:

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

    我用它在项目中创建文档,效果很好。但从未编辑过。

    查看文档here

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

        • 对于大家搜索如何轻松添加页眉和页脚,TCPDF有一个非常简单有效的例子:tcpdf.org/examples/example_003
        • tcPDF 用于生成新的 PDF。问题是关于修改预先存在的 PDF。
        【解决方案8】:

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

        我们使用的是商业版,但他们也有free/open source version,这有一些限制。

        很遗憾,这只允许创建 PDF。

        如果您想打开和“编辑”现有文件,pdflib 会提供a product which does this this,但要花费LOT

        【讨论】:

          【解决方案9】:
          <?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);
          ?>
          

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-21
          • 1970-01-01
          • 2018-06-02
          • 1970-01-01
          相关资源
          最近更新 更多