Python generates PDF reports, Python implements HTML conversion to PDF report

Source: Internet
Author: User
Tags wkhtmltopdf

1, first say HTML conversion to PDF: In fact, support directly generated, there are three functions Pdfkit.f

Install Python package: Pip install Pdfkit

System installation Wkhtmltopdf: Reference https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

Wkhtmltopdf:brew Install caskroom/cask/wkhtmltopdf under Mac

Import Pdfkitpdfkit.from_url ('http://google.com','out.pdf'  )    pdfkit.from_file ('test.html','out.pdf  ')   pdfkit.from_string ('hello! ','out.pdf')

Pass a URL or list of file names:

Pdfkit.from_url (['google.com','yandex.ru','  engadget.com'],'out.pdf')    
Pdfkit.from_file (['file1.html','file2.html') ,'out.pdf')

To pass an open file:

Withopen ('file.html') ASF:           pdfkit.from_file (F,'out.pdf  ')

if you want to make further processing of the generated PDF, you can read it into a variable: # set the output file to false and assign the result to a variable pdf=pdfkit.from_url (' http://google.com ', False)

You can set all the Wkhtmltopdf options. You can remove the '--' in front of the option name. If the option has no value, use None, falseor* as the dictionary value:

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)

when you convert a file, or string, you can specify the extended CSS file with CSS options. #a single CSS filecss='Example.css'Pdfkit.from_file ('file.html', Options=options, css=CSS)#multiple CSSfilescss=['Example.css','Example2.css'] Pdfkit.from_file ('file.html', Options=options, css=CSS) You can also pass any option through the META tags in your HTML:Body=       <meta name= "Pdfkit-page-size" Content= "Legal"/>  <meta name= " Pdfkit-orientation "content=" Landscape "/>    Hello world!    Span class= "St" >          Pdfkit.from_string (Body,'out.pdf')#With --page-size=legal and--orientation=landscape

2, besides Reporatlab

Installation: Pip Install Reportlab

Simple to use:

# !/usr/bin/python  from Import Canvas def Hello ():     = Canvas. Canvas ("helloworld.pdf")    c.drawstring (100,100,"  Hello,world")    c.showpage ()    c.save () Hello ( )

#!/usr/bin/env pythonImportsubprocessImportdatetime fromReportlab.pdfgenImportCanvas fromReportlab.lib.unitsImportinchdefdisk_report (): P= subprocess. Popen ("df-h", Shell=true, stdout=subprocess. PIPE)#print p.stdout.readlines ()    returnP.stdout.readlines ()defCreate_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) forLineinchInput:textobject.textLine (Line.strip ()) C.drawtext (Textobject) c.showpage () C.save () report=Disk_report () create_pdf (report)

Reference:

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 generates PDF reports, Python implements HTML conversion to PDF report

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.