1

The following python code successfully fills out a pdf form:

import pypdftk

data_dict = {key:value pairs}
PDF_PATH = 'form.pdf'                #form to be filled out in same folder as the file executing this code
out_file = 'out_file.pdf'            #completed pdf file
generated_pdf = pypdftk.fill_form(
    pdf_path = PDF_PATH,
    datas = data_dict,
    out_file = out_file,
    )

However, the same code used in my django project results in the following error message:

Error: Unable to find file.
Error: Failed to open PDF file:
   form.pdf
Errors encountered.  No output created.
Done.  Input errors, so no output created.

... REMAINDER OF TRACEBACK EXCLUDED FOR BREVITY IF YOU WANT TO SEE IT I'LL POST...

raise subprocess.CalledProcessError(retcode, cmd, output=output) output=output)                                      df fill_form C:\Users\Home\AppData\Local\Temp\tmpbqq__7c4 output out_file.pdf flatten
subprocess.CalledProcessError: Command 'pdftk l_a_r.pdf fill_form C:\Users\Home\AppData\Local\Temp\tmpbqq_0 87495_7c4 output out_file.pdf flatten'
returned non-zero exit status 1.

pypdftk is installed in the virtual environment the project is running in. The pdftk server is added as a windows path variable. In the above example, and every other time this has happened the temp file referenced at the end of the error message contains all of the expected data in XML.

I've tried the following combinations of code to try to make this work:

  1. Running the exact above code within a view function, with the pdf form to be filled in the same folder as the views.py file:
import pypdftk

def filler_view(request):
    form = MyForm()
    if request.method =='POST':
       #code to successfully populate dictionary data_dict with form data
       
       PDF_PATH = 'form.pdf'                #form to be filled out in same folder as the file executing this code
       out_file = 'out_file.pdf             #completed pdf file
       generated_pdf = pypdftk.fill_form(
       pdf_path = PDF_PATH,
       datas = data_dict,
       out_file = out_file,
       )
    return render(request, 'success.html')
  1. Storing the code and file in a folder and importing to call the relevant function within the view:
-appFolder
 -pfd_filler_folder
  -form.pdf
  -form_filler.py
 -views.py
views.py

from appFolder.pdf_filler_folder import form_filler as f

def filler_view(request):
    form = MyForm()
    if request.method =='POST':
       #code to successfully populate dictionary data_dict with form data
       f.fill_form(data_dict, 'output.pdf')

form_filler.py:
import pypdftk

def fill_form(data_dict, out_file):
    PDF_PATH = 'form.pdf'
    generated_pdf = pypdftk.fill_form(
        pdf_path = PDF_PATH,
        datas = data_dict,
        out_file = out_file,
    )
  1. Running both of the above with the full path from c:\... of the form.pdf file.

I've also verified that I can successfully fill a form with the executing .py file and the form.pdf file in same folder on two storage drives and from within the django project itself, when not being executed by the django project. pdftk finds the forms.py with no problems at all in this circumstance.

I believe that the file not found error message is key, as it seems to refer to the pdf form I'm trying to fill out. I've spent from 1500 till 1800 researching this, and I haven't managed to get it to work, although I am lead to believe that my error message indicates a missing parameter in the cl execution command. I'm not sure what this would be, as all parameters seem present and correct.

Interestingly enough, a friend of mine is experiencing the same error message just in windows. I'm aware that pdftk can sometimes be touchy in windows, and I think there's probably a nuance I'm missing here.

The outcome I'd like is to fill out a pdf form from within my django project, with data obtained from a form through a post request.

I'd welcome either someone enlightening me as to why pdftk is struggling to either see or use the form file whilst being used from within my django project and pointing me in the right direction

I'm aware that there are alternatives to using pdftk, but pdftk is the simplest, and honestly pypdftk is the only library I've found to reliably work with python to fill out pdf forms so far in Windows. I don't want to go down the route of generating my own replica form and populating it with data, but I'm aware that that is also an option.

halfer
  • 19,824
  • 17
  • 99
  • 186
justajolt
  • 141
  • 11
  • I asked the same question on reddit and got this response: 'It might be trying to look at the same folder as `manage.py` or `wsgi.py` (depending on how you're running it) -- since that's the script that is being run, so this relative path might not actually work.' This solution worked a charm. `form.py` is now being accessed, is populated as per data entered into the html form, and a duplicate pdf form is saved in the desired location. Further experimentation needed, but problem solved for now. I suspect that in production, the file may need to be in the same folder as the `wsgi.py` file. – justajolt Jan 10 '21 at 11:48

1 Answers1

1

Question answered just now on Reddit: When in Django, it is either wsgi.py or manage.py which is ultimately responsible for what goes on. On that basis, placing the form.pdf file in the same folder as wsgy.py solved the problem and the code now runs as intended, with an unbound form POSTing data back to a view, and a pdf form being filled out and a duplicate saved with said data. Hope that helps anyone else who comes up against this!

justajolt
  • 141
  • 11