python產生pdf報告、python實現html轉換為pdf報告

來源:互聯網
上載者:User

標籤:.text   return   readlines   gadget   for   epo   readline   show   get   

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 pdfkitpdfkit.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/pythonfrom reportlab.pdfgen import canvasdef hello():    c = canvas.Canvas("helloworld.pdf")    c.drawString(100,100,"Hello,World")    c.showPage()    c.save()hello()

 

#!/usr/bin/env pythonimport subprocessimport datetimefrom reportlab.pdfgen import canvasfrom reportlab.lib.units import inchdef 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

python產生pdf報告、python實現html轉換為pdf報告

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.