我有以下代码。我怎样才能让它变得更有活力:
- 我不必硬编码/指定单元格宽度
- HTML单元格应“可流动”,这意味着单元格长度会根据数据大小自动调整。应该也应用“自动换行”
- 列数可以变化
- 我可以从数据库表中获取数据,将其转换为html并将表格写入PDF
我尝试搜索,看起来PyFDF对此有支持 http://python.6.n6.nabble.com/PyFPDF-1-54b-HTML-rendering-templates-and-web2py-integration-td2120415.html 但看起来我必须使用web2py。有没有办法在不使用Web框架的情况下完成它?
import fpdf as pyfpdf
from fpdf import FPDF, HTMLMixin
class MyFPDF(FPDF, HTMLMixin):
pass
pdf=MyFPDF()
#First page
#pdf = pyfpdf.FPDF(format='letter')
pdf.add_page()
#set the font
pdf.set_font("Arial", size=10)
#define the html text
html = """<H1 align="center">Summary of Transactions</H1>
<h3>Date:</h3>
<h3>Branch:</h3>"""
html += """
<table border="1" align="center" width="100%">
<thead><tr><th width="20%">Date of Transaction</th><th width="20%">Name of Customer</th><th width="20%">Address</th><th width="20%">Contact Number</th><th width="20%">Status</th></tr></thead>
<tbody>
<tr><td>cell 1hgjhh jhjhjk jhjfsafsafsafsaf</td><td>cell 2</td><td>cell 3</td><td>cell 4</td><td>cell 5</td></tr>
<tr><td>cell 1</td><td>cell 2</td><td>cell 3</td><td>cell 4</td><td>cell 5</td></tr>
"""
html += '<tr><td>' + 'data' + '</td><td>' + 'data' +'</td> <td>' + 'data' +'</td> <td>' + 'data' +'</td><td>' + 'data' +'</td></tr>'
html += """</tbody></table>"""
#write the html text to PDF
pdf.write_html(html)
pdf.output('html.pdf','F')
我想要实现的目标类似于listing.pdf演示https://code.google.com/p/pyfpdf/wiki/Web2Py