我正在尝试通过在python django中使用pyfpdf创建PDF文件。以下代码段我正在尝试生成html代码的pdf。
from fpdf import FPDF, HTMLMixin
class WriteHtmlPDF(FPDF, HTMLMixin):
pass
html = f"""<h3>xxxxx</h3>
<div style="border:1px solid #000">
<table border="1" cellpadding="5" cellspacing="0" width="100%">
<tr><th width=20 align="left">xxxxxxxx:</th><td>xxxxxxxx</td></tr>
<tr><th width=20 align="left">xxxxxxxx:</th><td>xxxxxxxxx</td></tr>
<tr><th width=20 align="left">xxxxxxxx:</th><td>xxxxxxxxx</td></tr>
<tr><th width=20 align="left">xxxxxxxx:</th><td>xxxxxxxxx</td></tr>
</table>
</div>
<div>
<table border="1" cellpadding="5" cellspacing="0" align="center" width=100>
<tr>
<td width=33>xxxxxx: 1</td>
<td width=33>xxxxxx: 0</td>
<td width=33>xxxxxx: 1</td>
</tr>
</table>
</div>
<table style="border: 1px solid black;" border="1" cellpadding="5" cellspacing="0" width=100>
<thead align="center">
<tr style="background-color: #6AB9F7;">
<th>Sr No.</th>
<th>Name</th>
<th>xxxxx</th>
<th>xxxxx</th>
<th>xxxxx</th>
</tr>
</thead>
<tbody align="center">
<tr>
<td>1</td>
<td>xxxxxx</td>
<td>xxxxxx</td>
<td>xxxxxx</td>
<td>xxxxxx</td>
</tr>
</tbody>
</table>
"""
pdf = WriteHtmlPDF()
# First page
pdf.add_page()
pdf.write_html(html)
pdf.output('html.pdf', 'F')
我在HTML代码中使用了多个表格,并且在创建PDF时遇到了问题。以下是我得到的错误。
Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.6/dist-packages/fpdf/html.py", line 72, in handle_data
web_1 | l = [self.table_col_width[self.table_col_index]]
web_1 | IndexError: list index out of range
web_1 |
web_1 | During handling of the above exception, another exception occurred:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 41, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 187, in _get_response
web_1 | response = self.process_exception_by_middleware(e, request)
web_1 | File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 185, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/usr/local/lib/python3.6/dist-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
web_1 | return view_func(request, *args, **kwargs)
web_1 | File "./eauction/views.py", line 638, in get_login_report
web_1 | pdf.write_html(result_data)
web_1 | File "/usr/local/lib/python3.6/dist-packages/fpdf/html.py", line 401, in write_html
web_1 | h2p.feed(text)
web_1 | File "/usr/lib/python3.6/html/parser.py", line 111, in feed
web_1 | self.goahead(0)
web_1 | File "/usr/lib/python3.6/html/parser.py", line 163, in goahead
web_1 | self.handle_data(unescape(rawdata[i:j]))
web_1 | File "/usr/local/lib/python3.6/dist-packages/fpdf/html.py", line 74, in handle_data
web_1 | raise RuntimeError("Table column/cell width not specified, unable to continue")
web_1 | RuntimeError: Table column/cell width not specified, unable to continue
我尝试了GitHub解决方案,但仍然遇到相同的错误
答案 0 :(得分:0)
看起来WriteHtmlPDF()
不喜欢<thead></thead>
和<tbody></body>
标签。
一些小故障修复:
from fpdf import FPDF, HTMLMixin
import os
class WriteHtmlPDF(FPDF, HTMLMixin):
pass
pdf = WriteHtmlPDF()
# First page
pdf.add_page()
html = f"""<h3>xxxxx</h3>
<div style="border:1px solid #000">
<table border="1" cellpadding="5" cellspacing="0">
<tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxx</td></tr>
<tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
<tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
<tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
</table>
</div>
<div>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<td width="20">xxxxxx: 1</td>
<td width="20">xxxxxx: 0</td>
<td width="20">xxxxxx: 1</td>
</tr>
</table>
</div>
<table style="border: 1px solid black;" border="1" cellpadding="5" cellspacing="0">
<tr style="background-color: #6AB9F7;">
<td width="20">Sr. No</td>
<td width="20">Name</td>
<td width="20">xxxxxx</td>
<td width="20">xxxxxx</td>
<td width="20">xxxxxx</td>
</tr>
<tr>
<td width="20">1</td>
<td width="20">xxxxxx</td>
<td width="20">xxxxxx</td>
<td width="20">xxxxxx</td>
<td width="20">xxxxxx</td>
</tr>
</table>
"""
pdf.write_html(html)
fn = "html_tab.pdf"
pdf.output(fn, 'F')