Я пытаюсь создать файл PDF с помощью pyfpdf в Python Django. следующий фрагмент кода я пытаюсь создать PDF-код HTML.
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 решения, но все равно получаю ту же ошибку