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基于右递归解决八皇后问题的方法
May 25 Python
Python实现将HTML转换成doc格式文件的方法示例
Nov 20 Python
Python切片索引用法示例
May 15 Python
关于python列表增加元素的三种操作方法
Aug 22 Python
python定时按日期备份MySQL数据并压缩
Apr 19 Python
python实现连连看辅助(图像识别)
Mar 25 Python
Python Django简单实现session登录注销过程详解
Aug 06 Python
python实现复制文件到指定目录
Oct 16 Python
Python的赋值、深拷贝与浅拷贝的区别详解
Feb 12 Python
Python 数据的累加与统计的示例代码
Aug 03 Python
python如何导出微信公众号文章方法详解
Aug 31 Python
python时间time模块处理大全
Oct 25 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开发工具之vs2005图解
2008/01/12 PHP
php并发对MYSQL造成压力的解决方法
2013/02/21 PHP
ThinkPHP CURD方法之page方法详解
2014/06/18 PHP
高质量PHP代码的50个实用技巧必备(下)
2016/01/22 PHP
Yii2实现ActiveForm ajax提交
2017/05/26 PHP
Laravel框架执行原生SQL语句及使用paginate分页的方法
2018/08/17 PHP
php接口隔离原则实例分析
2019/11/11 PHP
jquery获取iframe中的dom对象(两种方法)
2013/07/02 Javascript
jQuery()方法的第二个参数详解
2015/04/29 Javascript
javascript实现的简单的表单验证
2015/07/10 Javascript
jQuery获得字体颜色16位码的方法
2016/02/20 Javascript
jquery简单插件制作(fn.extend)完整实例
2016/05/24 Javascript
js验证框架之RealyEasy验证详解
2016/06/08 Javascript
整理一下常见的IE错误
2016/11/18 Javascript
Vue项目中跨域问题解决方案
2018/06/05 Javascript
vue路由组件按需加载的几种方法小结
2018/07/12 Javascript
JavaScript自定义超时API代码实例
2020/04/30 Javascript
js实现金山打字通小游戏
2020/07/24 Javascript
Python随机数random模块使用指南
2016/09/09 Python
pygame实现弹力球及其变速效果
2017/07/03 Python
教你学会使用Python正则表达式
2017/09/07 Python
Python set常用操作函数集锦
2017/11/15 Python
Python搜索引擎实现原理和方法
2017/11/27 Python
Python简单计算文件MD5值的方法示例
2018/04/11 Python
django 实现celery动态设置周期任务执行时间
2019/11/19 Python
Python类中的装饰器在当前类中的声明与调用详解
2020/04/15 Python
python 8种必备的gui库
2020/08/27 Python
Python爬虫教程之利用正则表达式匹配网页内容
2020/12/08 Python
如何手工释放资源
2013/12/15 面试题
组织关系转移介绍信
2014/01/16 职场文书
小学生个人先进事迹材料
2014/05/08 职场文书
环保倡议书格式范文
2014/05/14 职场文书
人身损害赔偿协议书格式
2014/11/01 职场文书
2014年班务工作总结
2014/12/02 职场文书
2015年教师节主持词
2015/07/03 职场文书
导游词之澳门妈祖庙
2019/12/19 职场文书