Reportlab:修复标头,使其不能向上移动?

问题描述

我正在尝试使用python和ReportLab创建发票生成器,我使用画布制作我的pdf文件(table.wrapOn和table.drawOn方法)。现在,当我添加一个项目时,表格的标题会向上。我希望标题是固定的,并且每次添加项目时,表格都会往下移。

import json
from reportlab.pdfgen import canvas

with open('json/data.json') as json_file:
    products = json.load(json_file)


eindtotaal = 0

data = [
    ['Aantal','Productnaam','BTW','Prijs','Totaal (incl. BTW)']
]

for item in products:
    aantal = item['Aantal']
    omschrijving = item['Omschrijving']
    prijs = item['Prijs']
    totaal = '{:.2f}'.format(aantal*prijs)

    eindtotaal += aantal*prijs

    lijst = [aantal,omschrijving,item['BTW'],prijs,totaal]

    data.append(lijst)


data.append(['','',eindtotaal])



fileName = 'invoice.pdf'

from reportlab.platypus import SimpleDocTemplate
from reportlab.lib.pagesizes import A4

pdf = canvas.Canvas(fileName)

from reportlab.platypus import Table
table = Table(data)

from reportlab.platypus import TableStyle
from reportlab.lib import colors

style = TableStyle([
    ('TEXTCOLOR',(0,0),(-1,-1),colors.black),('ALIGN','CENTER'),(4,'RIGHT'),(1,'LEFT'),('FONTNAME','Courier-Bold'),('FONTSIZE',12),14),('BOTTOMPADDING',1),9),('TOPPADDING',9)
])

table.setStyle(style)

ts = TableStyle(
    [
    ('LINEBELOW',2,('LINEBELOW',-2),0.5,colors.grey)
    ]
)
table.setStyle(ts)


table.wrapOn(pdf,0)
table.drawOn(pdf,100,500)

pdf.showPage()

pdf.save()

我的json数据:

[
    {
        "Prijs": 249.99,"Aantal": 5,"Omschrijving": "Article 1","BTW": "21%"
    },{
        "Prijs": 21.95,"Aantal": 2,"Omschrijving": "Article 2","BTW": "21%"
    }

]

现在,当我向json数组中添加项目时,表格的标题就会上升。

对不起,荷兰语。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...