- 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 pdfkit
-
- confg = 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)
- import pdfkit
-
- file = "固件详情.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)