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

yipeiwu_com5年前Python基础

本文实例讲述了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、//www.jb51.net/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、/post/53233.htm

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

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

相关文章

django-rest-swagger的优化使用方法

如下所示: requirements.txt django==1.10.5 djangorestframework==3.5.3 django-rest-swagger==2.1...

利用Python绘制数据的瀑布图的教程

利用Python绘制数据的瀑布图的教程

介绍 对于绘制某些类型的数据来说,瀑布图是一种十分有用的工具。不足为奇的是,我们可以使用Pandas和matplotlib创建一个可重复的瀑布图。 在往下进行之前,我想先告诉大家我指代的...

Python实现从订阅源下载图片的方法

本文实例讲述了Python实现从订阅源下载图片的方法。分享给大家供大家参考。具体如下: 这段代码是基于python 3.4实现的,和python2.X 比起来有了好多差别啊。 这是一个练...

浅谈Django学习migrate和makemigrations的差别

本文主要研究的是Django中migrate和makemigrations的差别,具体如下。 在你改动了 model.py的内容之后执行下面的命令: Python manger.py m...

Pandas时间序列重采样(resample)方法中closed、label的作用详解

Pandas提供了便捷的方式对时间序列进行重采样,根据时间粒度的变大或者变小分为降采样和升采样: 降采样:时间粒度变大。例如,原来是按天统计的数据,现在变成按周统计。降采样会涉及到...