Python 3 Flask在Heroku上安装wkhtmltopdf [英] Python 3 flask install wkhtmltopdf on heroku
问题描述
我在heroku python应用程序(烧瓶)上安装wkhtmltopdf二进制文件时遇到问题.
I have a problem to install the wkhtmltopdf binary on my heroku python app (flask).
一年前(python 2),我已经遇到问题了,但是我可以通过将wkhtmltopdf-pack
添加到需求中并将其安装在heroku上,然后将config var设置为WKHTMLTOPDF_BINARY=wkhtmltopdf-pack
来解决此问题.这是我的旧线程
A year ago (python 2) I already had an issue, but I was able to solve it by first adding the wkhtmltopdf-pack
to the requirements and installing it on heroku and then setting the config var to WKHTMLTOPDF_BINARY=wkhtmltopdf-pack
. Here is my old thread
现在的问题:
我正在尝试对python 3使用相同的方法,但是wkhtmltopdf-pack的任何版本均无效,每次推送都被拒绝,我无法安装.
I am trying to use the same approach for python 3, but no version of the wkhtmltopdf-pack works, every push gets rejected and I cant install it.
我在要求中尝试了以下版本:
I tried these versions in the requirements:
wkhtmltopdf-pack==0.12.5
wkhtmltopdf-pack==0.12.4
wkhtmltopdf-pack==0.12.3
wkhtmltopdf-pack==0.12.3.0.post1
wkhtmltopdf-pack==0.12.2.4
我得到这些错误:
No matching distribution
或
error: can't copy 'bin/wkhtmltopdf-pack': doesn't exist or not a regular file
,我记得一旦它告诉我有一个SyntaxError
并且它无法解码某些内容.
and I remember once it told me there was a SyntaxError
and it could not decode something.
替代方法:
似乎也可以使用buildpack,所以我尝试添加一个buildpack:
It seems it is also possible to use a buildpack, so I tried adding a buildpack:
heroku buildpacks:add https://github.com/dscout/wkhtmltopdf-buildpack.git
我看到已经添加了buildpack,但是没有安装,也没有wkhtmltopdf的配置变量.我不明白如何触发安装,在所有关于buildpack的文档中,其写为添加buildpack,您就可以开始使用"了.
I see that the buildpack has been added, but there was no installation and there is also no config var for wkhtmltopdf. I dont understand how to trigger the installation, in all documantations for buildpacks its written "add the buildpack and you are ready to go".
尝试创建PDF会给我一个服务器错误,在这里:
Trying to create a PDF gives me a server error here:
OSError: No wkhtmltopdf executable found: "b''"
我设法安装了buildpack:
I managed to install the buildpack:
推送成功,但是没有创建config var,我也不知道二进制文件的路径是什么.
The push was successful, but no config var has been created and I have no clue what the path to the binary is.
编辑
我能够通过heroku bash查找文件:
I was able to find the files through heroku bash:
app bin dev etc lib lib64 lost + found proc sbin sys tmp usr var
app bin dev etc lib lib64 lost+found proc sbin sys tmp usr var
/ $ cd app
~ $ cd vendor
~/vendor $ dir
wkhtmltox
~/vendor $ cd wkhtmltox
~/vendor/wkhtmltox $ dir
lib
~/vendor/wkhtmltox $ cd lib
~/vendor/wkhtmltox/lib $ dir
libwkhtmltox.so libwkhtmltox.so.0 libwkhtmltox.so.0.12 libwkhtmltox.so.0.12.3
~/vendor/wkhtmltox/lib $ exit
现在我尝试了所有这些文件,但是都给出了错误:
Now I tried to all these files but all give an error:
OSError: wkhtmltopdf exited with non-zero code -11. error
这是我设置路径的方式:
Here is how I set the path:
# WKHTMLTOPDF config
if 'DYNO' in os.environ:
print ('loading wkhtmltopdf path on heroku')
MYDIR = os.path.dirname(__file__)
WKHTMLTOPDF_CMD = os.path.join(MYDIR + "/vendor/wkhtmltox/lib/", "libwkhtmltox.so")
else:
print ('loading wkhtmltopdf path on localhost')
MYDIR = os.path.dirname(__file__)
WKHTMLTOPDF_CMD = os.path.join(MYDIR + "/static/executables/bin/", "wkhtmltopdf.exe")
推荐答案
按照第一种方法,我能够自行解决问题.
I was able to solve the problem on my own, following my first approach.
我在pypi上找到了另一个wkhtmltopdf-pack
,并将其添加到我的requirements.txt
:
I found an other wkhtmltopdf-pack
on pypi and added it to my requirements.txt
:
wkhtmltopdf-pack-ng==0.12.3.0
Heroku能够安装此软件包.
Heroku was able to install this pack.
之后,我为wkhtmltopdf添加了config var:
After that I added the config var for wkhtmltopdf:
heroku config:set WKHTMLTOPDF_BINARY=wkhtmltopdf-pack
安装现已完成.我现在需要在我的应用上使用正确的路径:
The installation is now complete. I need to use the correct path now on my app:
if 'DYNO' in os.environ:
print ('loading wkhtmltopdf path on heroku')
WKHTMLTOPDF_CMD = subprocess.Popen(
['which', os.environ.get('WKHTMLTOPDF_BINARY', 'wkhtmltopdf-pack')], # Note we default to 'wkhtmltopdf' as the binary name
stdout=subprocess.PIPE).communicate()[0].strip()
else:
print ('loading wkhtmltopdf path on localhost')
MYDIR = os.path.dirname(__file__)
WKHTMLTOPDF_CMD = os.path.join(MYDIR + "/static/executables/bin/", "wkhtmltopdf.exe")
就这样.
这篇关于Python 3 Flask在Heroku上安装wkhtmltopdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!