我正在尝试使用 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 和 appers “路径权限被拒绝”
有什么问题?,缺少什么?,有什么问题?
我正在使用: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