FileNotFoundError: [Errno 2] No such file or directory: 'which' when using python pdfkit
Asked Answered
E

1

6

I have a strange error. I try to use pdfkit to convert an HTML string to PDF, but i get a "No such file or directory: 'which'". I know that pdfkit uses wkhtmltopdf, and it tries to find this utility by using which. I have no idea what's happening. 'which' works ok and wkhtmltopdf is installed on /usr/bin/. I get the error in this line:

pdf = pdfkit.from_string(html, False, options=options)

and the traceback:

Traceback:  

File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/srv/zboss-git/zboss/issues/views.py" in export_comments
  453.     pdf = pdfkit.from_string(html, False, options=options)

File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/api.py" in from_string
  66.                configuration=configuration)

File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/pdfkit.py" in __init__
  38.         self.configuration = (Configuration() if configuration is None

File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/configuration.py" in __init__
  18.                     ['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()

File "/usr/lib/python3.5/subprocess.py" in __init__
  947.                                 restore_signals, start_new_session)

File "/usr/lib/python3.5/subprocess.py" in _execute_child
  1551.                     raise child_exception_type(errno_num, err_msg)

Exception Type: FileNotFoundError at /issues/export_comments/
Exception Value: [Errno 2] No such file or directory: 'which'
Ecumenicalism answered 31/8, 2018 at 8:2 Comment(0)
E
5

I found a solution.

According the documentation of python-pdfkit:

Warning! Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from wkhtmltopdf site or you can use this script.

Script:

https://github.com/JazzCore/python-pdfkit/blob/master/travis/before-script.sh

I uninstall my version and run this version. I also set up manually my configuration:

    config = pdfkit.configuration(wkhtmltopdf=bytes('/usr/bin/wkhtmltopdf', 'utf-8'))
    pdf = pdfkit.from_string(html, False, options=options, configuration=config)

and voila!

Ecumenicalism answered 3/9, 2018 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.