zoukankan      html  css  js  c++  java
  • python生成pdf报告、python实现html转换为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、http://www.cnblogs.com/taceywong/p/5643978.html

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

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

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

    6、http://www.cnblogs.com/webRobot/p/6999665.html

  • 相关阅读:
    Elasticsearch Transport 模块创建及启动分析
    ElasticSearch中的JVM性能调优
    ElasticSearch 线程池类型分析之 ResizableBlockingQueue
    ElasticSearch 线程池类型分析之 ExecutorScalingQueue
    ElasticSearch 线程池类型分析之SizeBlockingQueue
    Redis的LRU算法
    hdu 1828 Picture(线段树扫描线矩形周长并)
    sublime text2 插件
    Python学习笔记(十四):模块高级
    梦想启航
  • 原文地址:https://www.cnblogs.com/shengulong/p/7994082.html
Copyright © 2011-2022 走看看