wkhtmltopdf Error "No such file or directory" (Django)
Asked Answered
N

4

6

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
Neurotomy answered 19/12, 2013 at 12:43 Comment(7)
I don't currently have a throwaway Django app on which I can try to reproduce this problem; however, if I were trying to debug it, the first thing I'd check is whether Django thinks the template file exists. e.g., Try adding import os.path to the top of the view, and printing os.path.exists(html_path) before the return.Zymase
Thanks for you answer @Brian Clapper. "os.path.exists" is for absolute path, i am using relative paths, well, if I change something in html path the error is "TemplateDoesNotExist(', '.join(not_found))", then the html path is not the problem.Neurotomy
FYI, os.path.exists() is not just for an absolute path. It works with relative paths.Zymase
Ok, "os.path.exists()" returned 'False' but the html path didn throw 'TemplateDoesNotExist' error, then, I think the path is correct.Neurotomy
do you solve this problem?Bertolde
Yes, I have 64 bits SO and I have installed for 32 bits, you have to check it and replace some files. I add extra information in my post.Neurotomy
You can answer your own question instead of editing it and pasting your solution. Answer your own question, and then accept your answer so the question is closed.Decencies
N
1

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
Neurotomy answered 15/7, 2014 at 14:43 Comment(1)
sudo apt-get install wkhtmltopdf, solved my problem.....thanksIsm
J
3

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

Janycejanyte answered 8/11, 2016 at 2:38 Comment(1)
I have searched for this solution for five freakin' days. Thanks man.Doridoria
L
2

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.

Lishalishe answered 11/12, 2016 at 19:23 Comment(0)
M
2

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.

Meade answered 26/8, 2019 at 19:10 Comment(2)
Can someone explain what {'quiet': True} does? Thanks!Pru
@BrendanMetcalfe {'quiet': True} means Be less verboseMeade
N
1

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
Neurotomy answered 15/7, 2014 at 14:43 Comment(1)
sudo apt-get install wkhtmltopdf, solved my problem.....thanksIsm

© 2022 - 2024 — McMap. All rights reserved.