Python-Markdown转PDF-报错OSError: wkhtmltopdf reported an error:Exit with code 1 due to network error: ProtocolUnknownError 解决办法
文章目录
一、问题
在使用 python将markdown格式文件转为pdf时,出现OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: ProtocolUnknownError的错误
OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: ProtocolUnknownError
上面的错误信息提示了是使用wkhtmltopdf时出错
(因为md转pdf需要用到wkhtmltopdf.exe,相关部分代码如下)
二、原因
本地文件访问权限被禁止了,所以在调用时才报错
三、解决办法
在python的程序中打开本地文件访问权限即可,主要添加"enable-local-file-access":True
权限打开前:
# HTML转PDF
pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8'})
权限打开后:
# HTML转PDF
pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8',"enable-local-file-access":True})
总结:
# HTML转PDF
# 本地访问权限未打开
# pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8'})
# "enable-local-file-access":True 打开本地文件访问权限
pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8',"enable-local-file-access":True})
成功运行:
md格式:
pdf格式: