2、sudo dpkg -i *.deb,如果报依赖错误执行下面语句再试sudo apt-get -f --fix-missing install 3、使用which wkhtmltopdf检查是否安装成功,输出结果为:/usr/local/bin/wkhtmltopdf General Options:--collate Collate when printing multiple copies 打印多份时进行校对--copies Number of copies to print into the pdf file (default 1) 要打印到pdf的副本数--extended-help Display more extensive help, detailing less common command switches-h, --help Display help-O, --orientation Set orientation to Landscape or Portrait 将方向设置为横向或纵向 -s, --page-size Set paper size to: A4, Letter, etc. 将纸张大小设置为:A4、Letter等。--password HTTP Authentication password HTTP身份验证密码-p, --proxy Use a proxy 使用代理-q, --quiet Be less verbose--username HTTP Authentication username HTTP身份验证用户名-V, --version Output version information an exit 将版本信息输出到出口
import pdfkitconfg = pdfkit.configuration(wkhtmltopdf="/usr/local/bin/wkhtmltopdf") # wkhtmltopdf的安装路径
options = {'page-size': 'A4','encoding':"UTF-8",'enable-local-file-access': True # 是否允许wkhtmltopdf访问本地资源,默认值为False。不加这行会报错 Exit with code 1 due to network error: ProtocolUnknownError'margin-top':'0.75in','margin-right':'0.75in','margin-bottom':'0.75in','margin-left':'0.75in','no-outline':None, # 为None时表示确定,则不生成目录'header-line':None, # 为None时表示确定,生成页眉下的线'header-spacing':'3', # 设置页眉与正文之间的距离,单位是毫米'header-right':'Quality Report', # 设置页眉右侧数据'header-font-size':10, # 设置页眉字体大小# 'header-html': HEADER_PATH, # 设置页眉数据,作为页眉的html页面必须有# 'footer-html': FOOTER_PATH,'footer-font-size':10, # 设置页脚字体大小# 'allow': ACESS_PATH,'animation':False, # 导出PDF一定要设置,否则显示不全(关闭图表动画)
}# 这个函数是从url里面获取内容, 这有3个参数,第一个是url或url列表,第二个是文件名,第三个就是khtmltopdf的路径, 返回值:成功返回True
url_list = ["https://www.cnblogs.com/mianbaoshu/p/13366074.html", "https://www.bbsmax.com/A/KE5Qj4G4dL/"]
pdfkit.from_url(url_list, 'url.pdf', options=options, configuration=confg)# from_file这个函数是从文件里面获取内容,这有3个参数,第一个是一个html文件或html文件列表,第二个是文生成的pdf的名字,第三个就是khtmltopdf的路径。可以使用参数cover来设置pdf封面
pdfkit.from_file(['my.html', ], 'html.pdf', options=options, configuration=confg,cover='http://localhost:8080/static/data/pdfHeader.html')# from_string这个函数是从一个字符串里面获取内容,这有3个参数,第一个是一个字符串,第二个是文生成的pdf的名字,第三个就是khtmltopdf的路径
html='''title
content
'''
pdfkit.from_string(html,'string.pdf', options=options, configuration=confg)
html中带css、png:设置参数'enable-local-file-access': True
import pdfkitfile = "固件详情.html"confg = pdfkit.configuration(wkhtmltopdf="/usr/local/bin/wkhtmltopdf")
wkhtmltopdf_options = {'enable-local-file-access': True # 是否允许wkhtmltopdf.exe访问本地资源,默认值为False。不加这行会报错 Exit with code 1 due to network error: ProtocolUnknownError
}
pdfkit.from_file(file, 'html.pdf', configuration=confg, options=wkhtmltopdf_options)
法3:百度了一天没结果。以为wkhtmltopdf不支持echarts,但是看到不少人能用,看来还是自己的问题 。一开始怀疑wkhtmltopdf 不支持canvas,后来排除。 问题很简单: 1、echarts 写在jquery的onload里,jquery的$(function(){});方法没有执行,也没多研究,把echarts 放到页面最下方,去掉onload方法。 2、显示echarts的div要设置一个高度和宽度,注意都要设置,浏览器不设置宽度能看到图,但是生成pdf不行。 3、加上参数 --javascript-delay 3000。 4、echarts 设置 animation:false,如果延迟时间够长应该不设置也可以。