1

我有以下代码。如何使其更具动态性:
- 我不必硬编码/指定单元格宽度
- 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')

我想要实现的类似于这里的 Listings.pdf 演示https://code.google.com/p/pyfpdf/wiki/Web2Py

4

1 回答 1

0

我不确定它是否会有所帮助,但您可能想查看PyfPDF 模板

于 2013-03-16T16:32:30.173 回答