tl;dr:在 HTML 到 PDF 的转换期间提供 CSS 文件时,Python-PDFkit 会生成错误。
我正在使用 Python-PDFkit(使用基于 Qt 的 WkHTMLtoPDF)将 HTML 文件转换为 PDF 文件。HTML 由 Jinja2 生成,包含 base64 图像、文本和类似 Bootstrap 的自定义 CSS 文件。当没有提供 CSS 文件时转换是正确的:
import pdfkit
pdfkit.from_file(file_name + ".html", file_name + ".pdf", options=pdfkit_options)
但是当我添加 CSS 文件时:
css_files = ["boosted-4.2.2-dist/css/Helvetica.css","boosted-4.2.2-dist/css/Icons.css", "boosted-4.2.2-dist/css/boosted.css"]
pdfkit.from_file(file_name + ".html", file_name + ".pdf", options=pdfkit_options, css=css_files)
然后 PDFkit 产生一个 WkHTMLtoPDF 错误:
Traceback (most recent call last):
File "App.py", line 77, in <module>
report_app.get_reports(client)
File "App.py", line 69, in get_reports
self.get_client_server_report(client[0], server[0])
File "App.py", line 59, in get_client_server_report
pdfkit.from_file(file_name + ".html", file_name + ".pdf", options=pdfkit_options, css=css_files)
File "/data/dev/project/ENV/lib/python3.6/site-packages/pdfkit/api.py", line 49, in from_file
return r.to_pdf(output_path)
File "/data/dev/project/ENV/lib/python3.6/site-packages/pdfkit/pdfkit.py", line 159, in to_pdf
raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, stderr))
OSError: wkhtmltopdf exited with non-zero code -8. error:
load glyph failed err=24 face=0x40727c0, glyph=0
QFontEngine: Glyph neither outline nor bitmap format=0
load glyph failed err=24 face=0x40727c0, glyph=3
QFontEngine: Glyph neither outline nor bitmap format=0
怎么了?任何想法?我正在开发 CentOS 和 Python 3 环境。