pdfkit生成pdf文件,could not cennect to display

这篇博客记录了在Docker环境下使用pdfkit生成PDF时遇到的问题,主要错误是 wkhtmltopdf 无法连接显示器。解决方案涉及到在Dockerfile中配置QT_QPA_PLATFORM为offscreen,并确保字体文件被正确挂载。博客还分享了完整的Dockerfile示例,以及解决此问题所需的知识点,包括对pdfkit的使用、Linux安装程序和Docker环境变量配置的理解。
摘要由CSDN通过智能技术生成
Traceback (most recent call last):
znzx_plat              | 2021-06-21T14:39:53.284967442Z   File "/var/www/Assist/views.py", line 665, in builddiary_save
znzx_plat              | 2021-06-21T14:39:53.285206860Z     retCode, doc = Form2File(u"施工日记", bda.content, request.user, builddiary_name, builddiary_date)
znzx_plat              | 2021-06-21T14:39:53.285225684Z   File "/var/www/Assist/utility.py", line 33, in Form2File
znzx_plat              | 2021-06-21T14:39:53.285230785Z     url_to_pdf("http://baidu.com","baidu.pdf")
znzx_plat              | 2021-06-21T14:39:53.285235360Z   File "/var/www/Assist/utility.py", line 27, in url_to_pdf
znzx_plat              | 2021-06-21T14:39:53.285253610Z     pdfkit.from_url(url, to_file, configuration=config)
znzx_plat              | 2021-06-21T14:39:53.285445705Z   File "/usr/local/lib/python2.7/site-packages/pdfkit/api.py", line 26, in from_url
znzx_plat              | 2021-06-21T14:39:53.285710940Z     return r.to_pdf(output_path)
znzx_plat              | 2021-06-21T14:39:53.285752467Z   File "/usr/local/lib/python2.7/site-packages/pdfkit/pdfkit.py", line 159, in to_pdf
znzx_plat              | 2021-06-21T14:39:53.285807003Z     raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, stderr))
znzx_plat              | 2021-06-21T14:39:53.285996993Z IOError: wkhtmltopdf exited with non-zero code -6. error:

今天遇到这个很纠结的问题,分析下原因。

1、之前没有熟练使用pdfkit,不知道具体的使用步骤。

2、不熟悉linux上安装程序

3、对docker容器的环境变量配置不熟悉。

现在进行问题分析:

这个错误主要是说我们的程序调用了gui,连接不上显示器。

因为用的是docker容器直接贴代码吧

FROM django:1.9.8-python2 
ENV LANG C.UTF-8
RUN mkdir /var/www
RUN apt-get update && \
    apt-get -yq update && \
    apt-get install -y xvfb && \
    apt-get install -y xvfb libxfont1 xfonts-encodings xfonts-utils xfonts-base xfonts-75dpi && \
    apt-get install -y wkhtmltopdf
#RUN apt-get install vim

RUN pip install pip==20.3.4 -i https://pypi.douban.com/simple
RUN pip install django-bootstrap3==8.2.3 -i https://pypi.douban.com/simple
RUN pip install django-ckeditor==5.6.1 -i https://pypi.douban.com/simple
RUN pip install django-cors-headers==2.1.0 -i https://pypi.douban.com/simple
RUN pip install django-crispy-forms==1.6.1 -i https://pypi.douban.com/simple
RUN pip install django-crontab==0.7.1 -i https://pypi.douban.com/simple
RUN pip install django-filter==1.0.4 -i https://pypi.douban.com/simple
RUN pip install django-filters==0.2.1 -i https://pypi.douban.com/simple
RUN pip install django-formtools==1.0 -i https://pypi.douban.com/simple
RUN pip install django-js-asset==1.1.0 -i https://pypi.douban.com/simple
RUN pip install django-jsonfield==1.0.1 -i https://pypi.douban.com/simple
RUN pip install django-rest-framework==0.1.0 -i https://pypi.douban.com/simple
RUN pip install django-reversion==2.0.9 -i https://pypi.douban.com/simple
RUN pip install django-simple-serializer==2.0.6 -i https://pypi.douban.com/simple
RUN pip install django-suit==0.2.25 -i https://pypi.douban.com/simple
RUN pip install djangorestframework==3.6.3 -i https://pypi.douban.com/simple
RUN pip install djangorestframework-filters==0.10.1 -i https://pypi.douban.com/simple
RUN pip install PyMySQL==0.7.11 -i https://pypi.douban.com/simple
RUN pip install MySQL-python==1.2.3 -i https://pypi.douban.com/simple
RUN pip install APScheduler==3.4.0 -i https://pypi.douban.com/simple
RUN pip install pymemcache==1.3.5 -i https://pypi.douban.com/simple
RUN pip install python-memcached==1.58 -i https://pypi.douban.com/simple
RUN pip install xlrd==1.0.0 -i https://pypi.douban.com/simple
RUN pip install urllib3==1.23 -i https://pypi.douban.com/simple
RUN pip install wechatpy==1.7.4 -i https://pypi.douban.com/simple
RUN pip install xlwt==1.1.1 -i https://pypi.douban.com/simple
RUN pip install xpinyin==0.5.5 -i https://pypi.douban.com/simple
RUN pip install redis==3.3.4 -i https://pypi.douban.com/simple
RUN pip install pycrypto==2.6.1 -i https://pypi.douban.com/simple
RUN pip install pdfkit==0.6.1 -i https://pypi.douban.com/simple
RUN pip install Pillow==2.3.0 -i https://pypi.douban.com/simple
RUN pip install pyPdf==1.13 -i https://pypi.douban.com/simple
RUN pip install opencv-python==4.1.0.25 -i https://pypi.douban.com/simple
RUN pip install openpyxl==2.4.7 -i https://pypi.douban.com/simple
RUN pip install oss2==2.3.4 -i https://pypi.douban.com/simple
RUN pip install numpy==1.13.3 -i https://pypi.douban.com/simple
RUN pip install requests==2.19.1 -i https://pypi.douban.com/simple
RUN pip install schedule==0.4.2 -i https://pypi.douban.com/simple
RUN pip install scipy==1.0.0 -i https://pypi.douban.com/simple
RUN pip install Serializer==0.2.1 -i https://pypi.douban.com/simple
RUN pip install six==1.11.0 -i https://pypi.douban.com/simple
RUN pip install qrcode==5.3 -i https://pypi.douban.com/simple
RUN pip install lxml==4.1.1 -i https://pypi.douban.com/simple
RUN pip install dss==0.8 -i https://pypi.douban.com/simple
RUN pip install bz2file==0.98 -i https://pypi.douban.com/simple
RUN pip install certifi==2018.4.16 -i https://pypi.douban.com/simple
RUN pip install django-simple-sso==0.13.2 -i https://pypi.douban.com/simple
RUN pip install django-decorator-include==1.3 -i https://pypi.douban.com/simple
RUN pip install grpcio==1.31.0 -i https://pypi.douban.com/simple
RUN pip install protobuf -i https://pypi.douban.com/simple
RUN pip install grpcio-tools==1.31.0 -i https://pypi.douban.com/simple
RUN pip install docxtpl==0.10.0 -i https://pypi.douban.com/simple
RUN pip install pika -i https://pypi.douban.com/simple
#CMD python /var/www/manage.py runserver 0.0.0.0:8000
ENV QT_QPA_PLATFORM offscreen

注意最后一行的环境变量配置,卡在这里很久很久。。。。

创建完镜像后不要忘记把字体文件加进去

刚开始忘记挂载了,所以用了最最原始的方式,copy宿主机文件到容器中,别见笑

docker cp doctemplate/simsun.ttc znzx_plat:/usr/lib/x86_64-linux-gnu/fonts/

fonts文件夹要自己创建哦

 

不懂的评论咨询,希望能帮助更多的人少走弯路。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值