jeffro25
jeffro25

Reputation: 155

Create a pdf with fillable fields python

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!

Upvotes: 1

Views: 5335

Answers (1)

hopeful_parrot
hopeful_parrot

Reputation: 11

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)

Upvotes: 1

Related Questions