当前位置: 首页 > 编程日记 > 正文

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.

相关文章:

模拟宽度自适应的输入框

看代码&#xff1a; !DOCTYPE HTML><html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8"><style type"text/css"> h2 { margin:0; padding:10px 0; font-size:14px; } .mod-retweet { bac…

洛谷 - P1426 - 小鱼会有危险吗 - 模拟

https://www.luogu.org/problemnew/show/P1426 题目说的是小鱼进入探测器一秒后就会有危险&#xff0c;所以不应该让小鱼先游&#xff0c;而是先检测探测器。 #include<bits/stdc.h> using namespace std; #define ll long longint s,x;int main(){scanf("%d%d"…

Linux系统性能分析:内存 优化

整体的内存基本原理和内存性能指标、性能瓶颈分析以及优化思路可参考如下导图 原始xmind文件路径Mind-Mapping

zoj 1010 (线段相交判断+多边形求面积)

链接&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId10 AreaTime Limit: 2 Seconds Memory Limit: 65536 KB Special JudgeJerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has though…

军用软件概算计价规范_工程造价五算:估算、概算、预算、结算、决算

估算、概算、预算、结算、决算估算即投资估算。是在决策阶段就建设项目建设总投资进行的科学估计。决策阶段又分为机会研究、项目建议书、初步可行性研究、详细可行性研究四个阶段&#xff0c;随着项目逐步的细化具体化&#xff0c;按照投资估算规程&#xff0c;可以得到不同精…

openssh配置终极一帖

一、什么是opensshOpenSSH 是 SSH &#xff08;Secure SHell&#xff09; 协议的免费开源实现。SSH协议族 可以用来进行远程控制&#xff0c; 或在计算机之间传送文件。而实现此功能的传统方式&#xff0c;如telnet(终端仿真协议)、 rcp ftp、 rlogin、rsh都是极为不安全的&a…

读书:历史 -- 海上丝绸之路

罗德里希普塔克 — 德国汉学家 海上丝路连结了古代世界贸易往来&#xff0c;见证了中华文明在人类历史中的枢纽位置。 王权集中的朝代中每一个流传后世的国家层级的决策无不彰显国家机器得强壮&#xff0c;但同样也很脆弱&#xff0c;决策者不可能时刻都能做出最为正确得选择。…

一.Linq to JSON是用来干什么的?

Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和修改部分的数据而非反序列化全部. 二.创建JSON数组和对象在进行Linq to JSON之前,首先要了解一下用于操作…

add python3.7 to path是什么意思_一起读源码:为什么 loguru 的时间 rotation 不能只精确到天...

摄影&#xff1a;产品经理猪耳朵与鹌鹑蛋做的皮蛋今天的问题来自未闻 Code 粉丝交流群&#xff1a;“loguru 每天自动生成的日志名字&#xff0c;可以只精确到日吗&#xff1f;”如下图所示&#xff1a;这里的每天自动生成日志的名字是什么意思呢&#xff1f;实际上指的就是rot…

hdu 4263(有限制的生成树)

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid4263 思路&#xff1a;将红边和蓝边单独求一次生成树&#xff0c;求的红边最多可以加入的边数cntr&#xff0c;蓝边最多可以加入的边数cntb&#xff0c;只要k满足条件k>(n-1-cntr)&&k<cntb&#…

Synchronized的两个用法

Synchronized的作用&#xff1a; 能够保证在同一时刻最多只有一个线程执行该段代码&#xff0c;以达到保证并发安全的效果 Synchronized的两个用法&#xff1a; 1&#xff09;对象锁 包括方法锁&#xff08;默认锁对象为this当前实例对象&#xff09;和同步代码块锁&#xff08…

h5引入不同的js文件怎样让第二个js使用第一个js文件中的函数_px2rem-loader使用及注意事项...

1.安装lib-flexible.js&#xff1b; //基于vue-cli配置手淘的lib-flexible rem&#xff0c;实现移动端自适应2.安装px2rem-loader&#xff1b;//使用 webpack 的 px2rem-loader,自动将px转换为rem3.在项目入口文件main.js中引入lib-flexible&#xff1b;//&#xff08;import …

C++中的explicitkeyword

在C程序中非常少有人去使用explicitkeyword&#xff0c;不可否认&#xff0c;在平时的实践中确实非常少能用的上。再说C的功能强大&#xff0c;往往一个问题能够利用好几种C特性去解决。但略微留心一下就会发现现有的MFC库或者C标准库中的相关类声明中explicit出现的频率是非常…

Entity Framework Code First在Oracle下的伪实现

为什么要说是伪实现&#xff0c;因为还做不到类似MsSql中那样完全的功能。Oralce中的数据库还是要我们自己手动去创建的。这里&#xff0c;我们舍掉了Model First中的EDMX文件&#xff0c;自己在代码里面写模型与映射关系&#xff0c;这又有点像是Code First模型了&#xff0c;…

leetcode-206 反转链表

描述如下&#xff1a; 反转一个单链表。 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 方法一&#xff1a;原地反转 数据结构如下 struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};ListN…

ios采用什么技术_在不锈钢技术成熟的今天,为什么汽车不采用呢?不仅仅是价格问题...

文/憨憨评车想必对于那些经常开车的人都会知道&#xff0c;我们的车子在行驶了几年之后&#xff0c;在性能方面必定是会有所下降的。然而还有一点也是非常让人头疼的&#xff0c;那就是车子的生锈问题。一旦车子的车身出现生锈情况的话&#xff0c;就会给人一种破破烂烂的感觉。…

Effective STL 为包含指针的关联容器指定比较类型

// 为包含指针的关联容器指定比较类型.cpp : 定义控制台应用程序的入口点。 //#include "stdafx.h" #include <set> #include <string> #include <iostream>using namespace std;struct StringPtrLess:public binary_function<const string*…

Android中处理崩溃异常

2019独角兽企业重金招聘Python工程师标准>>> 大家都知道&#xff0c;现在安装Android系统的手机版本和设备千差万别&#xff0c;在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象&#xff0c;开发者个人不可能购买所有设备逐个调试&#xff0c;所以…

python面试基本题(你需要的)

1、冒泡排序 lis [56,12,1,8,354,10,100,34,56,7,23,456,234,-58]def sortport():for i in range(len(lis)-1):for j in range(len(lis)-1-i):if lis[j] > lis[j1]:lis[j],lis[j1] lis[j1],lis[j]return lis 2、计算x的n次方的方法 def power(x, n):s 1while n > 0:n …

leetcode-92 反转链表II

题目描述如下&#xff1a; 反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。 说明: 1 ≤ m ≤ n ≤ 链表长度。 示例: 输入: 1->2->3->4->5->NULL, m 2, n 4 输出: 1->4->3->2->5->NULL 很明显这个题目是206 反转链表的进阶版 需要记…

地铁框架保护的原理_地铁屏蔽门是如何保证通讯的稳定?

地铁作为人们出行首选交通方式&#xff0c;安全可靠尤为重要&#xff0c;在复杂的地铁控制系统中&#xff0c;如何保障通讯的稳定性呢&#xff1f;本篇文章将从地铁系统中通讯单元的简单拓扑谈谈通讯防护的方案。随着我国经济的快速发展&#xff0c;地铁工程项目建设也处在快速…

HDU1548:A strange lift(Dijkstra或BFS)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid1548 题意&#xff1a;电梯每层有一个数&#xff0c;例如第n层有个数k&#xff0c; 那么这一层只能上k层或下k层&#xff0c;但是不能低于一层或高于n层&#xff0c; 给定起点与终点&#xff0c;要求出最少要按几次键 我的…

(转)C语言位运算详解

地址&#xff1a;http://www.cnblogs.com/911/archive/2008/05/20/1203477.html C语言位运算详解 作者&#xff1a;911说明&#xff1a;本文参考了http://www2.tsu.edu.cn/www/cjc/online/cyuyan/&#xff0c;算是对其的修正&#xff0c;在此将本文列为原创&#xff0c;实有抄袭…

[bzoj2300] [HAOI2011]防线修建

Description 近来A国和B国的矛盾激化&#xff0c;为了预防不测&#xff0c;A国准备修建一条长长的防线&#xff0c;当然修建防线的话&#xff0c;肯定要把需要保护的城市修在防线内部了。可是A国上层现在还犹豫不决&#xff0c;到底该把哪些城市作为保护对象呢&#xff1f;又由…

leetcode-142 环形链表II

给定一个链表&#xff0c;返回链表开始入环的第一个节点。 如果链表无环&#xff0c;则返回 null。 为了表示给定链表中的环&#xff0c;我们使用整数 pos 来表示链表尾连接到链表中的位置&#xff08;索引从 0 开始&#xff09;。 如果 pos 是 -1&#xff0c;则在该链表中没有…

1.低权限的程序向高权限的程序发消息 2.慎用setcurrentdirectory

1.低权限的程序向高权限的程序发消息 2.慎用setcurrentdirectory转载于:https://www.cnblogs.com/chunyou128/p/3921903.html

增加service_.NET Core + Kubernetes:Service

通过 .NET Core Kubernetes&#xff1a;Deployment 文章的介绍&#xff0c;我们可以通过 Deployment 控制器快速创建一组 Pod 来提供服务&#xff0c;每个 Pod 都会被分配一个集群内可见的虚拟 IP 地址&#xff0c;然后通过一个独立的 Endpoint(Pod IP ContainerPort)进行访问…

IIS配置相关问题:Framework 4.5 在IIS 7.5中运行

<system.webServer> <validation validateIntegratedModeConfiguration"false" /> <!--4.5 在IIS7.5中运行的时候--> <modules runAllManagedModulesForAllRequests"true" /> </system.webServer>转载于:https://…

[优先队列] 洛谷 P2085 最小函数值

题目描述 有n个函数&#xff0c;分别为F1,F2,...,Fn。定义Fi(x)Ai*x^2Bi*xCi (x∈N*)。给定这些Ai、Bi和Ci&#xff0c;请求出所有函数的所有函数值中最小的m个&#xff08;如有重复的要输出多个&#xff09;。 输入输出格式 输入格式&#xff1a; 输入数据&#xff1a;第一行输…