如何在Django / Python中自动填充PDF表单? [英] How can I auto-populate a PDF form in Django/Python?

查看:186
本文介绍了如何在Django / Python中自动填充PDF表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF表单,我想自动填充我的Django Web应用程序的数据,然后提供给用户下载。什么python图书馆可以让我轻松预先填写PDF表单?

解决方案

如果您生成非常动态的PDF并需要以编程方式控制,那么Reportlab很棒所有这一切:数据和布局。



为了填写现有PDF中的表单,reportlab是过分的,您将基本不得不在reportlab中从头重新构建PDF,而不是仅仅使用PDF格式的表单已经做好了。



PDF表单与 FDF 数据一起工作。当我不得不这样做时,我将一个PHP FDF库移植到Python,并将其发布为 fdfgen 。我用它来生成带有表单数据的fdf文件,然后使用 pdftk 推送fdf转换为PDF格式并生成输出。



整个过程的工作原理如下:


  1. 您(或设计师)在Acrobat中设计PDF或任何标记表单字段并记下字段名称(我不确定这是如何完成的;我们的设计师执行此步骤)。假设你的表格有名字和电话字段。

  2. 使用fdfgen创建一个FDF文件:

      from fdfgen import forge_fdf 
    fields = [('name','John Smith'),('phone','555-1234')]
    fdf = forge_fdf(,fields,[],[], ])
    fdf_file = open(data.fdf,w)
    fdf_file.write(fdf)
    fdf_file.close()


  3. 然后运行pdftk合并并展开:

      pdftk form.pdf fill_form data.fdf output output.pdf flatten 

    并填写,扁平化(意味着不再有可编辑的表单域)pdf将在output.pdf中。


这是一个有点复杂,pdftk可能是一个痛苦的安装(需要一个java堆栈,在Ubuntu 9.10上有bug,必须解决),但它是我能够想出的最简单的过程,工作流很方便(即,我们的设计师可以使所有他们想要的PDF布局的变化,并且只要他们不T改变字段的名字,我可以滴在新的,一切都继续工作)。



我为缺乏关于fdfgen文档的道歉。 forge_fdf()是真的,你应该需要的唯一功能,它有一个文档字符串来解释的参数。我只是从来没有完全得到解决,以用它做更多。


I have PDF forms that I want to autopopulate with data from my Django web application and then offer to the user to download. What python library would let me easily pre-populate PDF forms? These forms are intended to be printed out.

解决方案

Reportlab is great if you're generating very dynamic PDFs and need to programmatically control all of it: data and layout.

To just fill out forms in existing PDFs, reportlab is overkill and you'll basically have to rebuild the PDF from scratch in reportlab instead of just taking a PDF with a form that's already been made.

PDF forms work with FDF data. I ported a PHP FDF library to Python a while back when I had to do this and released it as fdfgen. I use that to generate an fdf file with the data for the form, then use pdftk to push the fdf into a PDF form and generate the output.

The whole process works like this:

  1. You (or a designer) design the PDF in Acrobat or whatever and mark the form fields and take note of the field names (I'm not sure exactly how this is done; our designer does this step). Let's say your form has fields "name" and "telephone".
  2. Use fdfgen to create a FDF file:

    from fdfgen import forge_fdf
    fields = [('name','John Smith'),('telephone','555-1234')]
    fdf = forge_fdf("",fields,[],[],[])
    fdf_file = open("data.fdf","w")
    fdf_file.write(fdf)
    fdf_file.close()
    

  3. Then you run pdftk to merge and flatten:

    pdftk form.pdf fill_form data.fdf output output.pdf flatten
    

    and a filled out, flattened (meaning that there are no longer editable form fields) pdf will be in output.pdf.

It's a bit complicated, and pdftk can be a pain to install (requires a java stack and there are bugs on Ubuntu 9.10 that have to be worked around) but it's the simplest process I've been able to come up with yet and the workflow is convenient (ie, our designers can make all the layout changes to the PDF they want and as long as they don't change the names of the fields, I can drop the new one in and everything keeps working).

I apologize for the lack of docs on fdfgen. forge_fdf() is really the only function you should need and it has a docstrings to explain the arguments. I've just never quite gotten around to doing more with it.

这篇关于如何在Django / Python中自动填充PDF表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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