python 自动填充表单_如何在Django / Python中自动填充PDF表单?

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:

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".

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()

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值