Fill pdf form from django form

Hi all,

So during this corona virus, I have decided to learn and do something new for work.
I have done a few Django tutorials, so I am very new.

Mainly, I am looking for advice on how to proceed. I need to be able to print very specific premade (Acrobat) pdf forms. Due to data protection, there is no database. I just need to print a predefined pdf form from whatever is entered in a Django form. The idea is to reuse data from Django form to print multiple different pdf forms. I hope this is clear.

For a couple of days I have been searching for solutions and trying them out (reportlab, PyPDF2, pdfrw, fdfgen), but they seem to be too much for the task.

Thank you for your input and keep safe!

Sessionone

Trying to clarify what you’re asking for:

  1. Display a web form with fields that correspond to fields in a PDF

  2. Take that data and use it to populate the PDF. (Assumption: The PDF is created with data-entry fields to be filled outl)

  3. Return the filled PDF to the browser making the initial request.

Comment #1 - No database? Don’t use Django. Go with something like Flask or Bottle.

Comment #2 - There are no “trivial” solutions. PDFs are (can be) very complicated beasts. For simple tasks fdfgen/pdftk is probably your simplest solution. (I’ve never personally used fdfgen, it didn’t become generally available until well after the time I had to prefill PDF forms. But I have been using pdftk - what they’re now calling pdftk server - for well over 10 years now.)

Comment #3 - If the PDF isn’t created with form fields, you have a much more significant effort on your hands. If this is the case, you’re going to need to learn a lot about how PDFs are structured.

Ken

Hi Ken,

Thanks for reply.

  1. Yes.
  2. Yes. The form is made with Acrobat and has data entry fields.

Comment#1 - I probably said that too soon. There will be a small database of users. Certain user data will be passed on to the form. I decided to go with Django to learn both it and Python, and for the ability to manage users in admin.

At the moment, we are using the pdf forms for quick data entry and printing. We have a few of them. But the file sizes are huge and admin is nightmare. The organisation has asked me distribute them to other offices, so I have to change the forms for every office, which is, I think, waste of time. They are heavily modified with acrobat javascript.

Thanks again.

So yes, I can recommend using the “fill_form” function of pdftk to populate the forms. (I’ve used it quite successfully in the past.)
That leaves you with the need to create the fdf data file as input. Back when I did this, I “cheated”. I took the form to be completed, filled out the form, dumped the fdf from the file, and used that as a template for my app to fill out the fdf from data I supply. It does look like something like fdfgen may be useful for a more general case.

Ken

Thanks.

I am trying pdftk and fdfgen as per https://github.com/ccnmtl/fdfgen/, but I am getting error: Failed to open form data file: data.fdf No output created :frowning:

Do you have write access to the directory that you’re trying this from?

I wanted to verify that it worked, so I went ahead and did a pip install of fdfgen.
Then I did the following in an interactive session:

Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from fdfgen import forge_fdf
>>> fields = [('name', 'Ken Whitesell'), ('telephone', '555-1234')]
>>> fdf = forge_fdf("", fields, [],[],[])
>>> with open("data.fdf", "wb") as fdf_file:
...     fdf_file.write(fdf)
...
219
>>> quit()

data.fdf looks like this:

%FDF-1.2
%
1 0 obj
<</FDF<</Fields[<</T( n a m e)/V( K e n   W h i t e s e l l)/ClrF 2/ClrFf 1>><</T( t e l e p h o n e)/V( 5 5 5 - 1 2 3 4)/ClrF 2/ClrFf 1>>]
>>
>>
endobj
trailer

<<
/Root 1 0 R
>>
%%EOF
1 Like

Thanks!

Yes, it works! I was doing it wrong.

Now, there is a tiny problem of figuring out how to use this with django…

Thank you, again.

Sessionone