创建带有可填充字段python的pdf [英] Create a pdf with fillable fields python

查看:235
本文介绍了创建带有可填充字段python的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我的任务是创建一个pdf,使最终用户可以将信息输入pdf并进行打印或保存,或者.我尝试创建的pdf是从具有可填充字段的pdf模板渲染的.我的问题是,每次使用任何python库(pypdf2,pdfrw,reportlabs等)创建此pdf时,它都会使其变平,并且导出后字段不再可填充.有没有什么可以实现这个目标的?只要我能使用平面模板文件并在其上呈现html表单,对我来说并没有太大关系. pdf是用acrobat制作的,我确保删除了密码.

So I have been tasked with creating a pdf that allows the end user to enter information into the pdf and print it or save it, either or. The pdf I am trying to create is being rendered from a pdf template that has fillable fields. The problem I have is that every time I use any python library(pypdf2, pdfrw, reportlabs, etc...) to create this pdf, it flattens it and the fields are no longer fillable after export. Is there anything out there that will accomplish this goal? It doesn't really matter to me if I have to take the flat template file and render an html form onto it, so long as it works. The pdf was made in acrobat and I made sure to remove the password.

有问题的pdf是在acrobat pro中创建的.我的python版本是2.7.

The pdf in question was created in acrobat pro. My python version is 2.7.

如果以前有人这样做,那么该信息将非常有帮助.谢谢!

If anyone has done this before, that information would be super helpful. Thanks!

推荐答案

遇到同样的问题.刚刚发现,output.pdf中的表格不能用okular或firefox填充,但是在google-chrome或Chrome浏览器中打开时仍然可以填充.

Facing the same problem. Just found out, the forms in the output.pdf are not fillable in okular or firefox, but still able to fill when opened in google-chrome or chromium-browser.

我用了以下几行:

from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas

c = canvas.Canvas('watermark.pdf')
c.drawImage('testplot.png', 350, 550, width=150, height=150)  
c.save()

output_file = PdfFileWriter()
watermark = PdfFileReader(open("watermark.pdf", 'rb'))
input_file = PdfFileReader(file('template.pdf', 'rb'))

page_count = input_file.getNumPages()
for page_number in range(page_count):
    print "Plotting png to {} of {}".format(page_number, page_count)
    input_page = input_file.getPage(page_number)
    input_page.mergePage(watermark.getPage(0))
    output_file.addPage(input_page)

with open('output.pdf', 'wb') as outputStream:
    output_file.write(outputStream)

这篇关于创建带有可填充字段python的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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