Reportlab python Pdf

2024-05-15 10:58:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这种类型的代码,可以帮助我从数据库中的数据渲染构建pdf

但若一个字符串的长度太大,那个么它就会和其他字符串过度交叉,比如在账单头中

根据我的代码,任何关于此的解决方案。此Pdf是通过django中的reportlab python构建的

附上图片。 As you see values in Charges Description overcross the column of SAC section. I want to put half characters to next line if the length of Charges Description is too big to avoid overcross.

def sea_export_local_invoice_pdf(request, pk):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = ' filename="local_invoice.pdf"'
c = canvas.Canvas(response)

# _______________________ get object ___________________

lir_fed = SeaExLocalInvoiceReceivable.objects.get(pk=pk)c.setFont('Helvetica-Bold', 9)
    c.drawString(85, 548, 'Charges Description')
    c.line(201, 560, 201, 300)
    c.drawString(210, 548, 'SAC')
    c.line(237, 560, 237, 300)
    c.drawString(243, 548, 'Unit')
    c.line(268, 560, 268, 300)
    c.drawString(273, 548, 'Rate')
    c.line(296, 560, 296, 280)
    c.drawString(298, 548, 'Ex.Rate')
    c.line(332, 560, 332, 280)
    c.drawString(340, 548, 'Amount')
    c.line(381, 560, 381, 300)
    c.drawString(384, 548, 'IGST')
    c.line(407, 560, 407, 300)
    c.drawString(410, 548, 'CGST')
    c.line(437, 560, 437, 300)
    c.drawString(440, 548, 'SGST')
    c.line(467, 560, 467, 165)
    c.drawString(474, 548, 'Tax Amt')
    c.line(519, 560, 519, 280)
    c.drawString(533, 548, 'Total')
    c.line(25, 540, 575, 540)

    a = '07'
    # a = ndls_gst_cd.gst_code
    b = str(sm_lir.gst_code)

    if a == b:
        gstapplied = 1
        divide_total_tax = (float(all_lir.billing_gst_amount) / 2)
        c.drawString(470, 230, str(divide_total_tax))
        c.drawString(470, 215, str(divide_total_tax))
        c.drawString(470, 200, '0.0')

    else:
        gstapplied = 2
        c.drawString(470, 230, '0.0')
        c.drawString(470, 215, '0.0')
        c.drawString(470, 200, str(all_lir.billing_gst_amount))

y = 530
i = 1
for row in lir_fed:
    c.setFont('Helvetica', 7)
    c.drawString(32, y, str(i))
    c.drawString(48, y, str(row.billing_head))
    c.setFont('Helvetica', 7)
    c.drawString(205, y, str(row.sac))
    c.drawString(240, y, str(row.qty_unit))
    c.drawString(269, y, str(row.rate))
    c.drawString(302, y, str(row.ex_rate))
    c.drawString(335, y, str(row.amount))
    

Tags: topdfresponselinedescriptionrowpkdivide
2条回答

你需要像浏览器一样呈现HTML的东西。没有比铬更好的了Headless Chrome是您应该查看的选项。如果您不想费心设置headless Chrome,您可以试用PDFMark.com提供的API

免责声明:我是开发者

如果可以使用其他库生成PDF,请尝试pdfme。它会自动将文本包装到表格单元格中

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


使用这个库,您不必手动创建表行。你只要把表格的内容放进去就行了

相关问题 更多 >