0

我正在使用 reportlab 包创建带有画布的 pdf 发票。表格网格与内容位置不匹配。

这是代码:

def create_canvas(filename,pagesize,bottomup):
    return canvas.Canvas(filename+".pdf",pagesize=pagesize,bottomup=bottomup)

def invoice():
    c = create_canvas("demo",A4,0)
    header = ['Product name', 'Quantity', 'Unit price', "Amount"]
    
    unit_price1= 30.00
    qty1= 3
    unit_price2= 20.00
    qty2 = 1
    amount_1 = unit_price1 * qty1
    amount_2 = unit_price2 * qty2
    prod1 = ["T-shirt", str(qty1), "$"+str(unit_price1), "$"+str(amount_1)]
    prod2 = ["small bag", str(qty2), "$"+str(unit_price2), "$"+str(amount_2)]
    data= [header,
            prod1,
            prod2,
            ]

    data.reverse() #without this line, the table will be reversed
    style=[('GRID',(0,0),(-1,-1),1,colors.black),
            # ('BOX',(0,0),(-1,-1),1,colors.black)
            ('ALIGN',(0,0),(-1,-1),'CENTER')
            ]

    
    t=Table(data,colWidths=[150,100,100,100],style=style)
    t.wrapOn(c,80,420)
    t.drawOn(c,80,420)

这是输出

如何修复画布中的表格对齐?

PS:我无法使用 SimpleDocTemplate,因为我无法自定义元素位置。

4

0 回答 0