<p>如果可以使用其他库生成PDF,请尝试<a href="https://github.com/aFelipeSP/pdfme" rel="nofollow noreferrer">pdfme</a>。它会自动将文本包装到表格单元格中</p>
<pre class="lang-py prettyprint-override"><code>from io import BytesIO
from django.http import HttpResponse
from pdfme import build_pdf
def sea_export_local_invoice_pdf(request, pk):
document = {
"sections": [
{
"content": [
{
"widths": [1, 5, 1, 1, 1, 1.2, 1.5, 1, 1, 1, 1.5, 1.5],
"style": {"s": 8, "cell_margin": 2},
"table": [
[
{'.b': 'S.No'},
{'.b': 'Charges Description'},
{'.b': 'SAC'},
{'.b': 'Unit'},
{'.b': 'Rate'},
{'.b': 'Ex.Rate'},
{'.b': 'Amount'},
{'.b': 'IGST'},
{'.b': 'CGST'},
{'.b': 'SGST'},
{'.b': 'Tax Amt'},
{'.b': 'Total'}
],
[
1,
"EMPTY EQUIP BALANCE AND HANDOVER CHARGE",
95.9, 2, 2, 2, '8.00', '2%', '0.0', '0.0', 0.16, 8.16
]
]
},
]
}
]
}
pdf_file = BytesIO()
build_pdf(document, pdf_file)
response = HttpResponse(pdf_file.read(), content_type='application/pdf')
response['Content-Disposition'] = ' filename="local_invoice.pdf"'
return response
</code></pre>
<p>使用这个库,您不必手动创建表行。你只要把表格的内容放进去就行了</p>