wkhtmltopdf错误“没有这样的文件或目录”(Django)

时间:2013-12-19 12:43:18

标签: python django wkhtmltopdf pisa xhtml2pdf

我正在尝试使用wkhtmltopdf从HTML代码创建PDF。我使用过xhtml2pdf和pisa但是我遇到了css样式的问题。好吧,wkhtmltopdf是我的最后一个选择。

我已按照此步骤操作。 https://github.com/incuna/django-wkhtmltopdf

sudo aptitude install libfontconfig
pip install django-wkhtmltopdf

如果我将'wkhtmltopdf'添加到我的INSTALLED_APPS,它将识别wkhtmltopdf路径。

INSTALLED_APPS = ["wkhtmltopdf"]

好吧,当我尝试生成PDF时会产生错误。

这是我的代码:

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,
                                   )

在设置中:

WKHTMLTOPDF_CMD_OPTIONS = {
'quiet': True,
}

我的错误:

 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

我认为它找不到'wkhtmltopdf'。

我的wkhtmltopdf安装路径是:

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

我添加了设置:

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

找到wkhtmltopdf和上诉“路径权限被拒绝”

有什么问题?缺少什么?,问题是什么?。

我正在使用: Ububtu服务器12.04 / 64位, Django 1.4, Pyrhon 2.7,

感谢您的回答。

编辑1:

我按照本教程

修复了mi问题

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 

在设置中

WKHTMLTOPDF_CMD = "/usr/bin/wkhtmltopdf"

但是出现了新的错误:

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 

解决:

----------- 7月14日-----------

0)创建一个〜/ tmp /目录并更改为

cd ~
mkdir tmp
cd tmp

1)来自http://code.google.com/p/wkhtmltopdf/downloads/list的libwkhtmltox-0.11.0_rc1-amd64.tar.bz2和wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

 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)创建子文件夹并将文件移入其中:

 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)进入之前创建的文件夹并解压缩文件。

 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)访问'lib'。在那里我们找到一个名为'libwkhtmltox.so'的文件,然后移动它

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

5)访问include,我们找到一个名为'wkhtmltox'的文件夹,然后移动它

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

6)解压缩文件后我们找到文件“wkhtmltopdf-amd64”,移动它。

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

7)我正在关注这篇文章。 http://www.cristalab.com/tutoriales/crear-pdf-en-django-y-virtualenv-c110664l/

8)安装

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)我正在关注这篇文章,https://github.com/incuna/django-wkhtmltopdf

10)稍后安装应用程序

 pip install django-wkhtmltopdf

4 个答案:

答案 0 :(得分:1)

我按照接下来的步骤解决了这个问题:

0)创建一个〜/ tmp /目录并更改为

cd ~ 
mkdir tmp 
cd tmp

1)来自http://code.google.com/p/wkhtmltopdf/downloads/list的libwkhtmltox-0.11.0_rc1-amd64.tar.bz2和wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

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)创建子文件夹并将文件移入其中:

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)进入之前创建的文件夹并解压缩文件。

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)访问'lib'。在那里我们找到一个名为'libwkhtmltox.so'的文件,然后移动它

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

5)访问include,我们找到一个名为'wkhtmltox'的文件夹,然后移动它

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

6)解压缩文件后我们找到文件“wkhtmltopdf-amd64”,移动它。

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

7)我正在关注这篇文章。 http://www.cristalab.com/tutoriales/crear-pdf-en-django-y-virtualenv-c110664l/

8)安装

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)我正在关注这篇文章,https://github.com/incuna/django-wkhtmltopdf

10)稍后安装应用程序

pip install django-wkhtmltopdf

答案 1 :(得分:1)

我在linux升级后遇到了这个问题。我在此链接找到了解决方案:https://github.com/incuna/django-wkhtmltopdf/issues/75

就我而言,基本解决方案是找出使用which存储wkhtmltopdf的位置:

$ which wkhtmltopdf
/usr/local/bin/wkhtmltopdf

然后将以下行添加到settings.py

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

(即返回的位置)

N.B。如果which wkhtmltopdf返回空白,则可能未正确安装wkhtmltopdf。您可以从https://wkhtmltopdf.org/downloads.html

执行此操作

答案 2 :(得分:1)

还有另一种解决方案:在你的pip安装之上,只需从这里安装相关版本的wkhtmltopdf二进制文件:http://wkhtmltopdf.org/downloads.html。就我而言,这解决了这个问题。

答案 3 :(得分:0)

此问题的最简单解决方案:

根据您的操作系统下载 wkhtmltopdf 的二进制版本

Wkhtmltopdf binary file download link

在项目的settings.py中添加以下代码。

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

这将解决您的问题。