【问题标题】:PHP Creating dynamic pdfPHP 创建动态pdf
【发布时间】:2014-09-29 21:23:28
【问题描述】:

我正在尝试使用 php 创建动态 pdf,

提交表单后,我可以加载 pdf,但是当我尝试编辑 pdf 时,它无法加载

我缩短了代码以使其保持直截了当

PDF 结构

Your Name is : <<NAME>>

PHP 动态脚本

set_time_limit(180); 

//Create Variable Names
$name = $_POST['name'];

function pdf_replace($pattern, $replacement, $string) {
    $len = strlen($pattern);
    $regexp = '';

    for($i = 0; $i <$len; $i++) {
        $regexp .= $pattern[$i];
        if($i < $len - 1) {
            $regexp .= "(\)\-{0,1}[0-9]*\(){0,1}";
        }
    }
    return ereg_replace ($regexp, $replacement, $string);
}

header('Content-Disposition: filename=cert.pdf');
header('Content-type: application/pdf');

//$date = date('F d, Y');

$filename = 'testform.pdf';
$fp = fopen($filename, 'r');
$output = fread($fp, filesize($filename));

fclose($fp);

//Replace the holders
$output = pdf_replace('<<NAME>>', $name, $output);


echo $output;

如果我注释掉输出,它会很好地加载表单,但是一旦我尝试运行该函数来替换占位符,它就无法加载。以前有人做过这样的事情吗?

【问题讨论】:

标签: php pdf


【解决方案1】:

我已经尝试了您的代码,我可以假设有以下原因之一可能导致问题:


当您调用 pdf_replace() 时,PHP 会在 ereg_replace() 函数上返回已弃用通知。这会破坏 PDF 结构并导致 PDF 无法加载。此函数自 PHP 5.3.0 起已弃用。简单的解决方案是改用preg_replace()。

function pdf_replace($pattern, $replacement, $string) {
    return preg_replace('/'.preg_quote($pattern,'/').'/', $replacement, $string);
}

如果您不能这样做,那么解决方案是编辑 php.ini 文件并编辑 error_reporting 参数。您可以将“^ E_DEPRECATED”添加到当前配置值以禁用已弃用通知。其他选项是在脚本开头添加 error_reporting() 并使用适当的值。


我没有看到您使用的 PDF,但一些 PDF 生成器对 PDF 源进行了编码。在这种情况下,在那里查找文本是一个问题。例如,我在 Mac 上尝试了“打印到 PDF”功能,但我无法在源代码中找到纯文本。所以 fopen 或 ereg_replace 都可以抱怨错误的文件格式。在这种情况下,您应该使用一些可以更巧妙地处理 PDF 的库。我更喜欢FPDF,但是这样的库很多。

【讨论】:

    【解决方案2】:

    我是一名网页设计师,我使用ezPDF 创建 pdf 文件以制作报告并在任何浏览器中查看。这是我寻找教程的网页:http://www.weberdev.com/get_example.php3?ExampleID=4804

    我希望这会有所帮助:)

    【讨论】:

      【解决方案3】:

      您的代码似乎有两个问题。

      首先,ereg_replace() 是一个已弃用函数。因此,php 脚本会引发错误,并且错误消息会破坏 pdf 结构。将函数更改为 preg_replace() ,它应该可以工作。

      其次,我使用示例 pdf 表单尝试了您的代码。似乎 ereg_replace() 无法处理某些字符。在我使用的 pdf-form 中,此函数在遇到特定字符即 § 后截断字符串(pdf-form 数据)。这就是为什么即使您使用 error_reporting(E_ALL ^ E_DEPRECATED); 抑制错误, 即使那样,代码也不会工作。

      所以,你最好选择 preg_replace();

      <?php
              set_time_limit(180); 
      
          //Create Variable Names
              $name = $_POST['name'];
      
              function pdf_replace($pattern, $replacement, $string) {
                  $len = strlen($pattern);
                  $regexp = '';
      
                  for($i = 0; $i <$len; $i++) {
                      $regexp .= $pattern[$i];
                      if($i < $len - 1) {
                          $regexp .= "(\)\-{0,1}[0-9]*\(){0,1}";
                      }
                  }
                  return preg_replace ($regexp, $replacement, $string);
              }
      
              header('Content-Disposition: filename=cert.pdf');
              header('Content-type: application/pdf');
      
              //$date = date('F d, Y');
      
              $filename = 'testform.pdf';
              $fp = fopen($filename, 'r');
              $output = fread($fp, filesize($filename));
      
              //fclose($fp);
      
              //Replace the holders
              $output = pdf_replace('<<NAME>>', $name, $output);
      
      
              echo $output;
      
      ?>
      

      【讨论】:

        【解决方案4】:

        如果对原始 PDF 文档没有非常具体的要求,就无法替换 PDF 中的文本字符串。

        此类项目中的一些cmets:

        PDF 文档使用字节偏移来允许快速访问文档中的特定对象。通过更改字符串,这些偏移量将变得无效,并且 PDF 文档可能被视为已损坏。

        最后,PDF 文档中的大多数内容流都会被压缩。因此,您正在搜索的字符串(可能)在这些流之一中,而不是“可见的”。

        呈现 PDF 后“看到”的字符串不必与 PDF 源中的字符串相同。 被替换的/新的字符串可能使用在所用字体中不可用的字符,或者它们通过单独的编码与其他字符匹配。

        还有更多需要考虑的事情......

        【讨论】:

          【解决方案5】:

          为什么你不使用 str_replace? 请检查此代码是否适合您(假设您可以在简单的文本编辑器中看到要替换的单词):

          // hide all errors so there will be no errors output which will break your PDF file 
          error_reporting(0);
          ini_set("display_errors", 0);
          //Create Variable Names
          $name = $_POST['name'];
          $filename = 'testform.pdf';
          $fp = fopen($filename, 'r');
          $output = fread($fp, filesize($filename));
          
          fclose($fp);
          
          //Replace the holders
          $output = str_replace('<<NAME>>', $name, $output);
          
          // I added the time just to avoid browser caching of the PDF file
          header('Content-Disposition: filename=cert'.time().'.pdf');
          header('Content-type: application/pdf');
          
          echo $output;
          

          有很多 PHP 项目使用 PHP 生成动态 PDF 文件,为什么不使用它们,为什么使用这个正则表达式?,可能你会有一些设计你会 将来必须添加到 pdf 中,例如表格徽标和东西,最简单的方法是将 HTML 转换为 PDF。 如果您正在寻找性能并且几乎 %100 看起来相似的 HTML 和 CSS,您应该使用 wkhtmltopdf,它免费且易于使用,它在您的服务器上运行,但它不是 PHP,它是您必须执行的可执行文件来自您的 PHP 代码。

          http://wkhtmltopdf.org/

          另一种选择是使用纯 PHP,这与以前的可执行文件不同

          http://www.tcpdf.org/

          我两者都用过,并且更喜欢 wkhtmltopdf,因为它在 HTML 和 CSS 中速度更快、成本最高

          【讨论】:

            猜你喜欢
            • 2012-01-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-17
            相关资源
            最近更新 更多