1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python生成pdf报告模块_python生成pdf报告 python实现html转换为pdf报告

python生成pdf报告模块_python生成pdf报告 python实现html转换为pdf报告

时间:2019-03-13 10:56:07

相关推荐

python生成pdf报告模块_python生成pdf报告 python实现html转换为pdf报告

1、先说下html转换为pdf:其实支持直接生成,有三个函数pdfkit.f

安装python包:pip Install pdfkit

系统安装wkhtmltopdf:参考 /JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

mac下的wkhtmltopdf: brew install Caskroom/cask/wkhtmltopdf

importpdfkit

pdfkit.from_url(‘‘,‘out.pdf‘)

pdfkit.from_file(‘test.html‘,‘out.pdf‘)

pdfkit.from_string(‘Hello!‘,‘out.pdf‘)

传递一个url或者文件名列表:

pdfkit.from_url([‘‘,‘yandex.ru‘,‘‘],‘out.pdf‘)

pdfkit.from_file([‘file1.html‘,‘file2.html‘],‘out.pdf‘)

传递一个打开的文件:

withopen(‘file.html‘)asf:

pdfkit.from_file(f,‘out.pdf‘)

如果你想对生成的PDF作进一步处理, 你可以将其读取到一个变量中:#设置输出文件为False,将结果赋给一个变量pdf=pdfkit.from_url(‘‘,False)

你可以制定所有的 wkhtmltopdf选项 . 你可以移除选项名字前面的 ‘--‘ .如果选项没有值, 使用None, Falseor*作为字典值:

options={‘page-size‘:‘Letter‘,‘margin-top‘:‘0.75in‘,‘margin-right‘:‘0.75in‘,‘margin-bottom‘:‘0.75in‘,‘margin-left‘:‘0.75in‘,‘encoding‘:"UTF-8",‘no-outline‘:None

}

pdfkit.from_url(‘‘,‘out.pdf‘, options=options)

当你转换文件、或字符串的时候,你可以通过css选项指定扩展的 CSS 文件。#单个 CSS 文件

css=‘example.css‘pdfkit.from_file(‘file.html‘, options=options, css=css)#Multiple CSS

filescss=[‘example.css‘,‘example2.css‘] pdfkit.from_file(‘file.html‘, options=options, css=css)

你也可以通过你的HTML中的meta tags传递任意选项:body = """

Hello World!"""

pdfkit.from_string(body,‘out.pdf‘)#with --page-size=Legal and --orientation=Landscape

2、再说reporatlab

安装:pip install reportlab

简单使用:

#!/usr/bin/python

from reportlab.pdfgen importcanvasdefhello():

c= canvas.Canvas("helloworld.pdf")

c.drawString(100,100,"Hello,World")

c.showPage()

c.save()

hello()

#!/usr/bin/env python

importsubprocessimportdatetimefrom reportlab.pdfgen importcanvasfrom reportlab.lib.units importinchdefdisk_report():

p= subprocess.Popen("df -h", shell=True, stdout=subprocess.PIPE)#print p.stdout.readlines()

returnp.stdout.readlines()def create_pdf(input, output="disk_report.pdf"):

now=datetime.datetime.today()

date= now.strftime("%h %d %Y %H:%M:%S")

c=canvas.Canvas(output)

textobject=c.beginText()

textobject.setTextOrigin(inch,11*inch)

textobject.textLines(‘‘‘Disk Capcity Report: %s‘‘‘ %date)for line ininput:

textobject.textLine(line.strip())

c.drawText(textobject)

c.showPage()

c.save()

report=disk_report()

create_pdf(report)

参考:

1、/twtrubiks/python-pdfkit-example

2、/taceywong/p/5643978.html

3、/rptlab/reportlab

4、/opensource/

5、/docs/reportlab-userguide.pdf

6、/webRobot/p/6999665.html

原文:/shengulong/p/7994082.html

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