欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-10-14 22:22:43
本文实例讲述了python实现html转换为pdf报告(生成pdf报告)功能。分享给大家供大家参考,具体如下: 1、先说下html转换为pdf:其实支持直接生成,有三个函...

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

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

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