wkhtmltopdf Error "No such file or directory" (Django)

16.6k views Asked by At

I am trying to use wkhtmltopdf to create a PDF from HTML code. I have used xhtml2pdf and pisa but I have had problems with css styles. well, wkhtmltopdf is my last option.

I have followed this steps. https://github.com/incuna/django-wkhtmltopdf

sudo aptitude install libfontconfig
pip install django-wkhtmltopdf

and if I add 'wkhtmltopdf' to my INSTALLED_APPS it will recognize the wkhtmltopdf path.

INSTALLED_APPS = ["wkhtmltopdf"]

Well, when I try to generate a PDF generate an error.

this is my code:

from wkhtmltopdf.views import PDFTemplateResponse 

 context = {
            'linkedin1':self.linkedin1,
            'twitter1':self.twitter1,
            'facebook1':self.facebook1,
            'current': get_current_base_url,
           }


    html_path = "app/folder/page1_1.html"
    response = PDFTemplateResponse(request=request,
                                   template=html_path,
                                   filename="hello.pdf",
                                   context= context,
                                   show_content_in_browser=False,
                                   cmd_options=settings.WKHTMLTOPDF_CMD_OPTIONS,
                                   )

In settings:

WKHTMLTOPDF_CMD_OPTIONS = {
'quiet': True,
}

My error:

 File "/home/user/.virtualenvs/app/local/lib/python2.7/site-   packages/django/core/handlers/base.py", line 136, in get_response
response = response.render()
 File "/home/user/.virtualenvs/app/local/lib/python2.7/site-packages/django/template/response.py", line 104, in render
self._set_content(self.rendered_content)
File "/home/user/.virtualenvs/app/local/lib/python2.7/site- packages/wkhtmltopdf/views.py", line 144, in rendered_content
footer_filename=footer_filename)
File "/home/user/.virtualenvs/app/local/lib/python2.7/site-packages/wkhtmltopdf/views.py", line 103, in convert_to_pdf
return wkhtmltopdf(pages=[filename], **cmd_options)
File "/home/user/.virtualenvs/app/local/lib/python2.7/site-packages/wkhtmltopdf/utils.py", line 92, in wkhtmltopdf
return check_output(ck_args, **ck_kwargs)
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

I think it cant't find 'wkhtmltopdf'.

My wkhtmltopdf install path is:

/home/user/.virtualenvs/app/local/lib/python2.7/site-packages/wkhtmltopdf/

well I add in settings:

WKHTMLTOPDF_CMD = '/home/user/.virtualenvs/app/local/lib/python2.7/site-packages/wkhtmltopdf/'

To find the wkhtmltopdf's and appers "path permission denied"

What is wrong?, what is missing?, what is the problem?.

I am using: Ububtu server 12.04/64 bits, Django 1.4, Pyrhon 2.7,

Thanks for your answers.

EDIT 1:

I fixed mi problem doing that following this tutorial

http://www.cristalab.com/tutoriales/crear-pdf-en-django-y-virtualenv-c110664l/

apt-get build-dep libqt4-gui libqt4-network libqt4-webkit
apt-get install openssl build-essential xorg git-core git-doc libssl-dev wkhtmltopdf 

in settings

WKHTMLTOPDF_CMD = "/usr/bin/wkhtmltopdf"

But appered new error:

File "/home/user/.virtualenvs/app/local/lib/python2.7/site- packages/django/template/response.py", line 104, in render
self._set_content(self.rendered_content)
File "/home/user/.virtualenvs/app/local/lib/python2.7/site-  packages/wkhtmltopdf/views.py", line 144, in rendered_content
footer_filename=footer_filename)
File "/home/user/.virtualenvs/app/local/lib/python2.7/site-packages/wkhtmltopdf/views.py", line 103, in convert_to_pdf
return wkhtmltopdf(pages=[filename], **cmd_options)
File "/home/user/.virtualenvs/app/local/lib/python2.7/site-packages/wkhtmltopdf/utils.py", line 92, in wkhtmltopdf
return check_output(ck_args, **ck_kwargs)
File "/usr/lib/python2.7/subprocess.py", line 544, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['/usr/bin/wkhtmltopdf', '--encoding', u'utf8', '--quiet', '/tmp/wkhtmltopdfE_THjv.html', '-']' returned non-zero exit status 

Solved:

----------- July 14 -----------

0) Create a ~/tmp/ directory and change into it

cd ~
mkdir tmp
cd tmp

1) libwkhtmltox-0.11.0_rc1-amd64.tar.bz2 and wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2 from http://code.google.com/p/wkhtmltopdf/downloads/list

 wget https://wkhtmltopdf.googlecode.com/files/libwkhtmltox-0.11.0_rc1-amd64.tar.bz2

 wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

2) Create subfolders and move the files into them:

 mkdir libwkhtmltox
 mkdir wkhtmltopdf

 mv libwkhtmltox-0.11.0_rc1-amd64.tar.bz2 libwkhtmltox
 mv wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2 wkhtmltopdf

3) go in folders previously created and unzip the files.

 cd libwkhtmltox
 tar -xvjf libwkhtmltox-0.11.0_rc1-amd64.tar.bz2 
 cd ../wkhtmltopdf
 tar -xvjf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2 

4) access to ‘lib’. there we find a file named ‘libwkhtmltox.so’, then move it

cd ../libwkhtmltox/lib/
sudo cp libwkhtmltox.so /usr/local/lib/

5) access to include, there we find a folder named ‘wkhtmltox’, then move it

cd ../include/
sudo cp -avr wkhtmltox/ /usr/local/include/

6) after unzip the files we find the file “wkhtmltopdf-amd64”, move it.

cd ../../wkhtmltopdf/
sudo cp wkhtmltopdf-amd64 /usr/bin/

7) I was following this post. http://www.cristalab.com/tutoriales/crear-pdf-en-django-y-virtualenv-c110664l/

8) install

sudo aptitude install libfontconfig

sudo apt-get build-dep libqt4-gui libqt4-network libqt4-webkit 

sudo apt-get install openssl build-essential xorg git-core git-doc libssl-dev wkhtmltopdf

9) I was following this post, https://github.com/incuna/django-wkhtmltopdf

10) later install the application

 pip install django-wkhtmltopdf
4

There are 4 answers

1
Java Developer On BEST ANSWER

I solved it following the next steps:

0) Create a ~/tmp/ directory and change into it

cd ~ 
mkdir tmp 
cd tmp

1) libwkhtmltox-0.11.0_rc1-amd64.tar.bz2 and wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2 from http://code.google.com/p/wkhtmltopdf/downloads/list

wget https://wkhtmltopdf.googlecode.com/files/libwkhtmltox-0.11.0_rc1-amd64.tar.bz2

wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

2) Create subfolders and move the files into them:

mkdir libwkhtmltox 
mkdir wkhtmltopdf

mv libwkhtmltox-0.11.0_rc1-amd64.tar.bz2 libwkhtmltox 
mv wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2 wkhtmltopdf

3) go in folders previously created and unzip the files.

cd libwkhtmltox tar -xvjf libwkhtmltox-0.11.0_rc1-amd64.tar.bz2 
cd   ../wkhtmltopdf tar -xvjf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

4) access to ‘lib’. there we find a file named ‘libwkhtmltox.so’, then move it

cd ../libwkhtmltox/lib/ 
sudo cp libwkhtmltox.so /usr/local/lib/

5) access to include, there we find a folder named ‘wkhtmltox’, then move it

cd ../include/ 
sudo cp -avr wkhtmltox/ /usr/local/include/

6) after unzip the files we find the file “wkhtmltopdf-amd64”, move it.

cd ../../wkhtmltopdf/ 
sudo cp wkhtmltopdf-amd64 /usr/bin/

7) I was following this post. http://www.cristalab.com/tutoriales/crear-pdf-en-django-y-virtualenv-c110664l/

8) install

sudo aptitude install libfontconfig

sudo apt-get build-dep libqt4-gui libqt4-network libqt4-webkit

sudo apt-get install openssl build-essential xorg git-core git-doc libssl-dev wkhtmltopdf

9) I was following this post, https://github.com/incuna/django-wkhtmltopdf

10) later install the application

pip install django-wkhtmltopdf
2
Anurag Misra On

Easiest Solution for this problem:

Download binary version of wkhtmltopdf as per your operation system

Wkhtmltopdf binary file download link

Add below coding in settings.py of your project.

WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'
WKHTMLTOPDF_CMD_OPTIONS = {
    'quiet': True,
}

This will solve your problem.

1
Rick Westera On

I encountered this problem after a linux upgrade. I found the solution at this link: https://github.com/incuna/django-wkhtmltopdf/issues/75

In my case, the basic solution is find out where wkhtmltopdf is stored using which:

$ which wkhtmltopdf
/usr/local/bin/wkhtmltopdf

Then add the following line to settings.py:

WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'

(i.e. the location returned by which)

N.B. If which wkhtmltopdf returns blank, you probably haven’t properly installed wkhtmltopdf. You can do so from https://wkhtmltopdf.org/downloads.html

0
Sergii Shcherbak On

There is another solution: on top of your pip install, just install the relevant version of wkhtmltopdf binary from here: http://wkhtmltopdf.org/downloads.html. In my case, this solved the problem.