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程序设计有所帮助。

相关文章

Python3 pandas 操作列表实例详解

1.首先需要安装pandas, 安装的时候可能由依赖的包需要安装,根据运行时候的提示,缺少哪个库,就pip 安装哪个库。 2.示例代码 import pandas as pd fro...

python如何使用Redis构建分布式锁

这篇文章主要介绍了python如何使用Redis构建分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在实际应用场景中,我们可能...

使用PyTorch训练一个图像分类器实例

使用PyTorch训练一个图像分类器实例

如下所示: import torch import torchvision import torchvision.transforms as transforms import ma...

python自定义类并使用的方法

本文实例讲述了python自定义类并使用的方法。分享给大家供大家参考。具体如下: class Person: def __init__(self, first, middle,...

一文带你了解Python中的字符串是什么

一文带你了解Python中的字符串是什么

在《 详解Python拼接字符串的七种方式 》这篇文章里,我提到过,字符串是程序员离不开的事情。后来,我看到了一个英文版本的说法: There are few guarantees in...