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.

python 自动填充表单,如何在Django / Python中自动填充PDF表单?相关推荐

  1. python个位怎么表示_如何在django管理中表示位标志int字段?

    我有一个数据模型,它的位字段定义如下:alter table MemberFlags add column title varchar(50) not null default ''; alter t ...

  2. android getter不起作用,java - 如何在Android Studio中自动生成getter和setter

    java - 如何在Android Studio中自动生成getter和setter Android Studio中是否有一个快捷方式可以自动生成给定类中的getter和setter? 14个解决方案 ...

  3. 如何在SQL Server中自动进行表分区

    In this article, we will demonstrate specific ways to automate table partitioning in SQL Server. Thi ...

  4. django mysql 创建表_关于 django ORM 中,数据库建表方式的问题

    本人以前是做客户端的,做后端没多久. 一直有一个问题困扰我,正好现在手上有一个系统在做. 系统逻辑是写一个爬虫在网上爬取一个网站的信息, 经过加工处理后,使用 django 来展示和交互. 表结构简化 ...

  5. python生成shell脚本_Python设置在shell脚本中自动补全功能的方法

    本篇博客将会简短的介绍,如何在ubuntu中设置python自动补全功能. 需求:由于python中的内建函数较多,我们在百纳乘时,可能记不清函数的名字,同时自动补全功能,加快了我们开发的效率. 方法 ...

  6. java 合并和拆分单元格_如何在Microsoft Word中合并和拆分表和单元格

    java 合并和拆分单元格 You can easily merge and split cells in Microsoft Word to make your tables more intere ...

  7. 利用iTextSharp填写中文(中日韩)PDF表单(完整解决方案)

    或者说中日韩文)表单填写的问题,本不想回答这类问题,因为相关的注意事项都已经在我的博客里说了,但现在看来还是有必要再啰唆下了,如果再有问题的话,希望带着Money来问,拜托了. 下面这段代码根据iTe ...

  8. python tkinter画笑脸_python – 如何在tkinter窗口中绘制图像

    如何在tkinter窗口中绘制图像(我使用的是 python 3.3)?我正在寻找一个可以在tkinter窗口的给定位置绘制图像的语句. 是啊- 任何答案将不胜感激.这里是我想要使用代码的程序的源代码 ...

  9. android 应用自动重启,Android:如何在“强制关闭”后自动重启应用程序?

    Android:如何在"强制关闭"后自动重启应用程序? 在Android应用程序中,如果我们没有得到正确的例外,我们通常会收到"强制关闭"错误. 如果强行关闭, ...

最新文章

  1. 独家|OpenCV1.11 使用OpenCV制作一个低成本立体摄像机
  2. C语言博客作业05--指针
  3. Qt Creator使用其他构建系统
  4. (转)springboot:添加JSP支持
  5. boost序列化(Serialization)
  6. tsql是mysql中的吗_Mysql中的sql是如何执行的
  7. 文件的读操作 c# 1614526130
  8. Html 点透镂空遮罩,swift 实现遮罩部分区域“挖洞”效果和点击事件穿透
  9. linux# 解读wmctrl一览输出的项目
  10. GBase 8a 报错gbase_forward_result failed to write result for client
  11. 微信小程序怎样关闭直播插件
  12. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java学生综合考评系统b8vlm
  13. amc 美国数学竞赛能用计算机吗,关于AMC美国数学竞赛的QA
  14. 【读书笔记】曾国藩的正面与侧面(二)
  15. 为何老人与年轻人都更愿意选择OPPO?这些用户需求值得关注
  16. CSS文本设置超出2行显示省略号
  17. vue-cl 有一些静态资源不想经过webpack打包那么应该public 下面
  18. TCP/IP协议知识梳理
  19. Python 编辑器哪个好用
  20. 【操作系统】判断题汇总大全

热门文章

  1. 使用jtest工具压测Apache Traffic Server
  2. UE4材质着色器全面学习教程
  3. 两个类相互包含引用的问题--类前向声明
  4. git submodule 使用场景汇总
  5. leetcode-295 数据流的中位数
  6. Contos7 克隆实例 以及 配置网络-服务-等相关信息
  7. LuoguP2617 Dynamic Rankings (动态主席树学习理解)
  8. s9.16作业,员工信息表
  9. Vue - 表单
  10. Navicat连接数据库成功,新建查询时提示错误“Cannot create file ……”