Forums

pypdftk Issue in Online Hosting.

Hi everyone, I am a novice in Python programming (and programming in general) and am trying to deploy a Flask project to pythonanywhere. My current issue is trying to use pypdftk to combine and fill some stored pdf form files. My approach worked great when I deployed it locally but has run into some issues. Here is my original error:

subprocess.CalledProcessError: Command '/usr/bin/pdftk /home/lewisjluck/CPApp/static/./pdf_templates/form.pdf fill_form /tmp/tmp5v62wd_f output /tmp/tmps3c5uf2t flatten' returned non-zero exit status 134.

After some digging I found this link: "https://stackoverflow.com/questions/65646864/non-zero-exit-status-1-due-to-pdf-file-not-found-when-using-pypdftk-to-fill-pd" of someone who has experienced similar issues in Django. They suggested putting the form templates in the same file as wsgi.py. I tried that as below but still the same error:

subprocess.CalledProcessError: Command '/usr/bin/pdftk /var/www/form.pdf fill_form /tmp/tmp4efb8uye output /tmp/tmppef8_4xl flatten' returned non-zero exit status 134.

I'm guessing the issue is to do with pypdftk not being able to handle the paths online? I have already made all the forms relative etc. using os.path, so am unsure how else to fix this.

Any help given would be greatly appreciated. Thanks so much! Lewis

You should make your paths absolute, not relative.

I have implemented it as follows:

THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
template_name = os.path.join(THIS_FOLDER, "./pdf_templates/form.pdf")

I think this was the recommended way to do absolute paths?

[edit by admin: formatting]

As a follow up, I have been looking and it appears in this page: https://help.pythonanywhere.com/pages/PDF/ it says pdftk can only be used in "Docker". Does this mean pypdftk will not work on my flask hosted web app? Perhaps the exit status 134 is not being able to find/use pdftk?

As a follow up, I have been looking and it appears in this page: https://help.pythonanywhere.com/pages/PDF/ it says pdftk can only be used in "Docker". Does this mean pypdftk will not work on my flask hosted web app? Perhaps the exit status 134 is not being able to find/use pdftk?

Thanks for the heads-up about that help page; it was out-of-date, and Docker specifically is not necessary for running pdftk -- instead you need our new virtualization system, which I've just enabled for your account. It will be active if you reload your website (unlike Docker, it will work in website code). Could you try that and see if it fixes the error?

Thank you so much, it is working perfectly now! Was worried I would have to dive down the hole of all sorts of pdf packages again. Thanks!

Excellent, thanks for confirming that!