Create a pdf with fillable fields python

扶醉桌前 提交于 2019-12-12 04:34:39

问题


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.

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!


回答1:


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.

I used this lines:

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)


来源:https://stackoverflow.com/questions/41710994/create-a-pdf-with-fillable-fields-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!