使用PHP将值插入可编辑的PDF中,并使其保持可编辑状态 [英] Insert values into editable PDF with PHP, and keep it editable

查看:133
本文介绍了使用PHP将值插入可编辑的PDF中,并使其保持可编辑状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有可编辑字段的PDF.我希望将值从HTML表单传递到此PDF中.我试过使用FPDF,它可以工作,但是将值传递给PDF后,pdf中的字段不再可编辑.

I have a PDF with editable fields. I wish to pass values from an HTML form into this PDF. I've tried using FPDF and it works, however the fields in the pdf are no longer editable after passing the values to the PDF.

另一个缺点是,当将值传递给PDF时,我们必须为每个字段指定精确的坐标.我可以用来保持"PDF可编辑"的其他工具有什么想法吗?

Another drawback is that we have to specify exact coordinates for each field when passing the values to the PDF. Any ideas on other tools I can use to keep the "PDF EDITABLE"?

我使用以下代码生成pdf http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/simple-demo/

I used the following code to generate pdf http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/simple-demo/

推荐答案

我无法在PHP中找到我真正喜欢的PDF填充方法.相反,我使用PHP生成XFDF,然后使用 pdftk 来将其推送到可填充的PDF中. (请注意,此代码要求命名您的PDF字段.)

I was unable to find a PDF filling method natively in PHP that I really liked. Instead, I use PHP to generate XFDF, then I use pdftk to push it into the fillable PDF. (Note that this code requires your PDF's fields to be named.)

以下是从关联数组生成XFDF的示例函数:

Here is an example function to generate XFDF from an associative array:

function forge_xfdf($file,$info,$enc='UTF-8'){
    $data='<?xml version="1.0" encoding="'.$enc.'"?>'."\n".
        '<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">'."\n".
        '<fields>'."\n";
    foreach($info as $field => $val){
        $data.='<field name="'.$field.'">'."\n";
        if(is_array($val)){
            $data.='<</T('.$field.')/V[';
            foreach($val as $opt)
                $data.='<value>'.$opt.'</value>'."\n";
        }else{
            $data.='<value>'.$val.'</value>'."\n";
        }
        $data.='</field>'."\n";
    }
    $data.='</fields>'."\n".
        '<ids original="'.md5($file).'" modified="'.time().'" />'."\n".
        '<f href="'.$file.'" />'."\n".
        '</xfdf>'."\n";
    return $data;
}

然后,我将该XFDF写入一个临时文件.

Then, I write that XFDF to a temporary file.

$empty_form      = '/path/to/fillable/pdf/form.pdf'
$fdf_filename    = tempnam(PDF_TEMP_DIR, 'fdf');
$output_filename = tempnam(PDF_TEMP_DIR, 'pdf');
$fdf_data = forge_xfdf($empty_form, $data, 'UTF-8');

if($fdf_fp = fopen($fdf_fn, 'wb')){
    fwrite($fdf_fp, $fdf_data);
    fclose($fdf_fp);

    $command = '/usr/local/bin/pdftk "'.$empty_form.'" fill_form "'.$fdf_filename.'" output "'.$output_file.'" dont_ask';
    passthru($command);

    // SEND THE FILE TO THE BROWSER

    unlink($output_file);
    unlink($fdf_filename);
}

如果要做需要不可编辑的PDF,请在命令中在单词dont_ask之前添加单词flatten.

If you do want a non-editable PDF, add the word flatten in the command before the word dont_ask.

如果您不喜欢填写PDF表单,也可以用HTML生成表单,然后使用 dompdf 即可将HTML转换为PDF.

If you aren't attached to filling a PDF form, you could generate the form in HTML as well, then use dompdf to convert from HTML to PDF.

这篇关于使用PHP将值插入可编辑的PDF中,并使其保持可编辑状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆