【问题标题】:Form to PHP to PDF: Conversion Assistance表单到 PHP 到 PDF:转换帮助
【发布时间】:2018-03-15 19:30:56
【问题描述】:

所以,这里是瘦的。

我有一个 HTML 表单(其中会有很多取决于信函模板),它通过 PHP 将表单数据传递到信函模板。这……我已经成功了。

但是,我需要将数据传递到模板中,然后将该模板转换为 PDF。

有什么建议吗?和......去

【问题讨论】:

  • 感谢反对票。这是一个合法的问题。嘘!哈哈

标签: php pdf


【解决方案1】:

我最近使用 pdftk(服务器)这样做:https://www.pdflabs.com/tools/pdftk-server/

首先,将其安装在网络服务器或本地。

然后,这是我从网络上改编的一个PHP类(复制它并命名为PdfFormToPdftk.php):

<?php

class PdfFormToPdftk {
    /*
     * Path to raw PDF form
     * @var string
     */
    private $pdfurl;

     /*
     * Path to PDFKTK
     * @var string
     */
    private $pdftkpath;

    /*
     * Path to temp files
     * @var string
     */
    private $tmppath;

    /*
     * Errors
     * @var string
     */
    private $errors;

    /*
     * Last command done
     * @var string
     */
    private $lastcmd;

    /*
     * Form data
     * @var array
     */
    private $data;

    /*
     * Path to filled PDF form
     * @var string
     */
    private $output;

    /*
     * Flag for flattening the file
     * @var string
     */
    private $flatten;

    public function __construct($pdfurl, $data, $tmppath, $pdftkpath = '/usr/bin/pdftk') {
        $this->pdfurl = $pdfurl;
        $this->data = $data;
        $this->tmppath = $tmppath;
        $this->pdftkpath = $pdftkpath;
    }

    private function tempfile() {
        return tempnam($this->tmppath, gethostname());
    }

    public function fields($pretty = false) {
        $tmp = $this->tempfile();

        exec("{$this->pdftkpath} {$this->pdfurl} dump_data_fields > {$tmp}");
        $con = file_get_contents($tmp);

        unlink($tmp);
        return $pretty == true ? nl2br($con) : $con;
    }

    private function makeFdf() {
        $fdf = '%FDF-1.2
        1 0 obj<</FDF<< /Fields[';

        foreach ($this->data as $key => $value) {
            $fdf .= '<</T(' . $key . ')/V(' . $value . ')>>';
        }

        $fdf .= "] >> >>
        endobj
        trailer
        <</Root 1 0 R>>
        %%EOF";

        $fdf_file = $this->tempfile();
        file_put_contents($fdf_file, $fdf);

        return $fdf_file;
    }

    public function flatten() {
        $this->flatten = ' flatten';
        return $this;
    }

    private function generate() {

        $fdf = $this->makeFdf();
        $this->output = $this->tempfile();
        $cmd = "{$this->pdftkpath} {$this->pdfurl} fill_form {$fdf} output {$this->output}{$this->flatten} 2>&1";
        $this->lastcmd = $cmd;
        exec($cmd, $outputAndErrors, $returnValue);
        $this->errors = $outputAndErrors;
        unlink($fdf);
    }

    public function save($path = null) {
        if (is_null($path)) {
            return $this;
        }

        if (!$this->output) {
            $this->generate();
        }

        $dest = pathinfo($path, PATHINFO_DIRNAME);
        if (!file_exists($dest)) {
            mkdir($dest, 0775, true);
        }

        if (!copy($this->output, $path)) {
            echo "failed to copy $path...\n";
        }
        unlink($this->output);

        $this->output = $path;

        return $this;
    }

    public function download() {
        if (!$this->output) {
            $this->generate();
        }

        $filepath = $this->output;
        if (file_exists($filepath)) {

            header('Content-Description: File Transfer');
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment; filename=' . uniqid(gethostname()) . '.pdf');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filepath));

            readfile($filepath);

            exit;
        }
    }

}

你可以在一些脚本中这样使用它:

<?php
require_once 'PdfFormToPdftk.php';

$datas = ['firstname' => 'Foo', 'lastname' => 'Bar'];
$output_file = 'yourfile.pdf';
$template_pdf_file = 'templatefile.pdf'; //where your virgin pdf template contains at least 'firstname' and 'lastname' as editable fields. You might want to use Adobe Acrobat Pro and save it as a Adobe Static PDF Form
$path_to_pdftk_server = '/opt/pdflabs/pdftk/bin/pdftk'; // type 'which pdftk' in your console to find yours

$pdf = new PdfFormToPdftk($template_pdf_file, $datas, '/', $path_to_pdftk_server);
$file = $pdf->save($output_file);

【讨论】:

  • 好的,所以...它有点工作,不确定是不是因为我在本地运行免费版本,但它会创建 PDF,但是当我尝试打开它时,它说无法打开,因为文件可能已损坏或损坏。
  • 你可以这样做:var_dump($file);查看PDF生成过程中是否有任何错误。仅pdftk服务器没有付费版本。
  • 如果你在 mac OS x 上开发,你应该安装这个版本:pdflabs.com/tools/pdftk-the-pdf-toolkit/…(来源:stackoverflow.com/questions/32505951/pdftk-server-on-os-x-10-11
  • 请原谅这里的 N00B 问题,但在 private $pdfurl;private $pdftkpath; 这样的行中,我是设置这些变量还是这些默认值?再次抱歉这个问题。
  • $pdfurl 变量对应于您的空模板所在的路径。您最好将其设置为绝对 URL。 $pdftkpath 是您安装 pdftk 的路径。 /usr/bin/pdftk 有一个默认值 - 但您可能想要更改它。如果您愿意,这里有一个用 PHP 编写的更完整的 pdftk API:github.com/mikehaertl/php-pdftk
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 2011-09-13
  • 1970-01-01
相关资源
最近更新 更多