表格到PHP到PDF:转换协助

时间:2017-10-04 11:58:18

标签: php pdf

所以,这是瘦的。

我有一个HTML表单(其中有许多取决于Letter模板),它通过PHP将表单数据传递给Letter模板。这......我已经成功完成了。

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

有什么建议吗? AND ...... GO

1 个答案:

答案 0 :(得分: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);