Python实现html转换为pdf报告(生成pdf报告)功能示例


Posted in Python onMay 04, 2019

本文实例讲述了Python实现html转换为pdf报告(生成pdf报告)功能。分享给大家供大家参考,具体如下:

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

安装python包:pip Install pdfkit

系统安装wkhtmltopdf:参考 https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

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

import pdfkit
pdfkit.from_url('http://google.com','out.pdf')  
pdfkit.from_file('test.html','out.pdf')  
pdfkit.from_string('Hello!','out.pdf')

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

pdfkit.from_url(['google.com','yandex.ru','engadget.com'],'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('http://google.com',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('http://google.com','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 = """ <html> <head> <meta name="pdfkit-page-size" content="Legal"/> <meta name="pdfkit-orientation" content="Landscape"/> </head> Hello World! </html> """
pdfkit.from_string(body,'out.pdf')#with --page-size=Legal and --orientation=Landscape

2、再说reporatlab

安装:

pip install reportlab

简单使用:

#!/usr/bin/python
from reportlab.pdfgen import canvas
def hello():
  c = canvas.Canvas("helloworld.pdf")
  c.drawString(100,100,"Hello,World")
  c.showPage()
  c.save()
hello()
#!/usr/bin/env python
import subprocess
import datetime
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
def disk_report():
  p = subprocess.Popen("df -h", shell=True, stdout=subprocess.PIPE)
#  print p.stdout.readlines()
  return p.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 in input:
    textobject.textLine(line.strip())
  c.drawText(textobject)
  c.showPage()
  c.save()
report = disk_report()
create_pdf(report)

参考:

1、https://github.com/twtrubiks/python-pdfkit-example

2、//3water.com/article/160638.htm

3、https://bitbucket.org/rptlab/reportlab

4、http://www.reportlab.com/opensource/

5、http://www.reportlab.com/docs/reportlab-userguide.pdf

6、https://3water.com/article/53233.htm

更多Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python struct.unpack
Sep 06 Python
Python基于pygame实现的弹力球效果(附源码)
Nov 11 Python
Python使用面向对象方式创建线程实现12306售票系统
Dec 24 Python
python实现二分查找算法
Sep 21 Python
pycharm 解除默认unittest模式的方法
Nov 30 Python
python分块读取大数据,避免内存不足的方法
Dec 10 Python
详解Python中的内建函数,可迭代对象,迭代器
Apr 29 Python
pandas 数据索引与选取的实现方法
Jun 21 Python
python绘制评估优化算法性能的测试函数
Jun 25 Python
Pandas 重塑(stack)和轴向旋转(pivot)的实现
Jul 22 Python
Django1.11配合uni-app发起微信支付的实现
Oct 12 Python
Python实现基于socket的udp传输与接收功能详解
Nov 15 Python
Python实现将HTML转成PDF的方法分析
May 04 #Python
Python第三方库face_recognition在windows上的安装过程
May 03 #Python
Python人脸识别第三方库face_recognition接口说明文档
May 03 #Python
Python使用到第三方库PyMuPDF图片与pdf相互转换
May 03 #Python
利用python将图片版PDF转文字版PDF
May 03 #Python
Python3.0中普通方法、类方法和静态方法的比较
May 03 #Python
Python Flask框架模板操作实例分析
May 03 #Python
You might like
PHP+AJAX实现无刷新注册(带用户名实时检测)
2007/01/02 PHP
PHP中strtotime函数使用方法分享
2012/01/10 PHP
一个php短网址的生成代码(仿微博短网址)
2014/05/07 PHP
php链表用法实例分析
2015/07/09 PHP
PHP基于SPL实现的迭代器模式示例
2018/04/22 PHP
php使用curl_init()和curl_multi_init()多线程的速度比较详解
2018/08/15 PHP
YII2框架中actions的作用与使用方法示例
2020/03/13 PHP
HR vs CL BO3 第一场 2.13
2021/03/10 DOTA
指定位置如果有图片显示图片,无图片显示广告的JS
2010/06/05 Javascript
JS的encodeURI和java的URLDecoder.decode使用介绍
2014/05/08 Javascript
JavaScript获取当前日期是星期几的方法
2015/04/06 Javascript
Flow之一个新的Javascript静态类型检查器
2015/12/21 Javascript
详解Bootstrap创建表单的三种格式(一)
2016/01/04 Javascript
js 判断一组日期是否是连续的简单实例
2016/07/11 Javascript
js从外部获取图片的实现方法
2016/08/05 Javascript
js 将input框中的输入自动转化成半角大写(税号输入框)
2017/02/16 Javascript
jQuery Validate 校验多个相同name的方法
2017/05/18 jQuery
JS实现json的序列化和反序列化功能示例
2017/06/13 Javascript
微信小程序 监听手势滑动切换页面实例详解
2017/06/15 Javascript
手动用webpack搭建第一个ReactApp的示例
2018/04/11 Javascript
koa router 多文件引入的方法示例
2019/05/22 Javascript
详解Webpack抽离第三方类库以及common解决方案
2020/03/30 Javascript
js中延迟加载和预加载的具体使用
2021/01/14 Javascript
Python查询IP地址归属完整代码
2017/06/21 Python
Python实现PS图像抽象画风效果的方法
2018/01/23 Python
Python基础之变量基本用法与进阶详解
2020/01/03 Python
使用sklearn对多分类的每个类别进行指标评价操作
2020/06/11 Python
西班牙伏林航空公司:Vueling
2016/08/05 全球购物
美国在线珠宝商店:SZUL
2017/02/11 全球购物
英国复古和经典球衣网站:Vintage Football Shirts
2018/10/05 全球购物
Koral官方网站:女性时尚运动服
2019/04/10 全球购物
智能室内花园:Click & Grow
2021/01/29 全球购物
澳大利亚电商Catch新西兰站:Catch.co.nz
2020/05/30 全球购物
小学新教师培训方案
2014/02/03 职场文书
学年个人总结范文
2015/03/05 职场文书
​(迎国庆)作文之我爱我的祖国
2019/09/19 职场文书