1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Python使用pdfkit wkhtmltopdf将html转换为pdf错误记录文档

Python使用pdfkit wkhtmltopdf将html转换为pdf错误记录文档

时间:2023-02-08 15:33:45

相关推荐

Python使用pdfkit wkhtmltopdf将html转换为pdf错误记录文档

1、首先,必须安装一下pdfkit这个模块库,使用命令:pip install pdfkit,安装完成后即可,只需在代码写入一行代码,导入即可:

import pdffkit

2、接着,我这边是尝试将一个html文件转换为pdf的,我的代码是这样的,点击一个按钮时就转换,代码很简单,主要附上python代码:

def export_pdf(request):pdfkit.from_file('test.html', r'D:\test\' + id + '.pdf')

注意,这里面的html文件必须为绝对路径,要不然可能在项目中会找不到文件,报文件找不到错误,之后点击按钮触发这个函数,结果报如下错误:

No wkhtmltopdf executable found: "b''"If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - /JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

3、接着,处理这个问题,说是我没安装wkhtmltopdf,不过确实没安装,于是去/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf安装windows底下的版本,安装完了之后再运行,还是报这个错误,好吧。

接下来就开始找解决办法了,改下代码如下:

def export_pdf(request): path_wk = r'D:\SoftWare\wkhtmltopdf\bin\wkhtmltopdf.exe' # 安装位置config = pdfkit.configuration(wkhtmltopdf=path_wk)pdfkit.from_file('test.html', r'D:\test' + id + '.pdf', configuration=config)

4、继续点击按钮,触发这个方法,发现报了一个这样的错误:

wkhtmltopdf reported an error:Loading pages (1/6)[> ] 0%[======> ] 10%[==============================> ] 50%[============================================================] 100%QPainter::begin(): Returned falseError: Unable to write to destinationExit with code 1, due to unknown error.

这是什么东西,看不懂,去查了好久都查不到要怎么解决。。。当然,未完待续:

5、于是看报错信息,报错信息如下所示:

主要看最后一行,点进去那个pdfkit.py,接着在to_pdf加个断点去调试一下,重新点击那个按钮,进来了,按F8键一步步走,如下图所示:

input为None去了,于是就可以知道传过的source有问题,此时的source是一个对象,还不是一个文件对象,就抛出异常了,因此代码有问题,改下代码如下:

def export_pdf(request):id = request.POST.get('id', '')path_wk = r'D:\SoftWare\wkhtmltopdf\bin\wkhtmltopdf.exe' # 安装位置config = pdfkit.configuration(wkhtmltopdf=path_wk)with open('test.html') as f:pdfkit.from_file(f, r'D:\test' + id + '.pdf', configuration=config)

6、此时又报个错误了,错误如下:

'gbk' codec can't decode byte 0xa2 in position 153: illegal multibyte sequence

编码格式问题,于是修改一下代码如下:

def export_pdf(request):id = request.POST.get('id', '')path_wk = r'D:\SoftWare\wkhtmltopdf\bin\wkhtmltopdf.exe' # 安装位置config = pdfkit.configuration(wkhtmltopdf=path_wk)with open('test.html', 'r', encoding='utf-8') as f:pdfkit.from_file(f, r'D:\test' + id + '.pdf', configuration=config)

7、继续点击按钮导出这个pdf,发现又报错了,又是之前那个错误:

wkhtmltopdf reported an error:Loading pages (1/6)[> ] 0%[======> ] 10%[==============================> ] 50%[============================================================] 100%

8、最后,找了一天终于知道问题在哪里了,就出现在这一行代码里面:

pdfkit.from_file(f, r'D:\test' + id + '.pdf', configuration=config)

第二个参数的问题,不能指向本地的某个路径,还是什么情况,我修改一下代码如下:

def export_pdf(request):id = request.POST.get('id', '')path_wk = r'D:\SoftWare\wkhtmltopdf\bin\wkhtmltopdf.exe' # 安装位置config = pdfkit.configuration(wkhtmltopdf=path_wk)with open('test.html', 'r', encoding='utf-8') as f:pdfkit.from_file(f, 'test.pdf', configuration=config)file = open('test.pdf', 'rb')response = FileResponse(file)response['Content-Type'] = 'application/pdf'response['Content-Disposition'] = 'attachment;filename="test.pdf"'return response

如果上面没返回一个HttpResponse的话,会报下面的错误:

The view test.export_pdf didn't return an HttpResponse object. It returned None instead.

9、这样便可以导出一个pdf文件了,并且直接下载到本地默认的地方,但是如果你得html有问题的话,导出来的就有问题,这里只是简单的一个测试代码而已。

10、以上就是我最近遇到的一些问题,记录一下,仅供大家学习参考,谢谢!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。