2 回答

TA貢獻2065條經驗 獲得超14個贊
試試這個使用“TopPadder”的可行示例,(結果是 Total 被推到 Frame 的底部,我假設你可以在它下面添加一個墊子,以控制底部的高度):
########################################################################
from reportlab.lib.units import mm
from reportlab.platypus import Paragraph, Spacer, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.flowables import TopPadder
import io
def create_pdf():
styles=getSampleStyleSheet()
# add invoice header
flowable_list = [
Spacer(1, 5 * mm),
Paragraph(f'Date: ...', styles['Normal']),
Spacer(1, 5 * mm),
]
# add invoice details
detail_list = [
('Item 1', 8, 45),
('Item 2', 1, 14),
]
row_list = [
[
Paragraph(f'desc', styles['Normal']),
# quantity,
# amount,
]
for desc, quantity, amount in detail_list]
story = []
story.append(
Table(
data=row_list,
colWidths=[100 * mm, 40 * mm, 40 * mm],
style=TableStyle([
('VALIGN', (0, 0), (-1, -1), 'TOP'),
# ... some other options...
])))
# add invoice footer; this should be at a specific position on the page
flowable_list.append(Spacer(1, 5 * mm))
flowable_list.append(TopPadder(Paragraph(f'Total: 0', styles['Normal'])))
# build PDF
buffer = io.BytesIO()
doc = SimpleDocTemplate("test2.pdf")
doc.build(flowable_list)
# ----------------------------------------------------------------------
if __name__ == "__main__":
create_pdf() # Printing the pdf

TA貢獻1829條經驗 獲得超7個贊
我還沒有找到更好的方法,所以我將添加我當前的解決方案;也許它會幫助一些未來有類似問題的讀者。
...
story.append(
Table(
data=row_list,
colWidths=[100*mm, 40*mm, 40*mm],
style=TableStyle([
('VALIGN', (0, 0), (-1, -1), 'TOP'),
... some other options ...
])))
# calculate real height of details table, so we can add a 'Spacer' for the missing
# part to have the invoice totals at the correct height at the bottom of the page
_, real_height = story[-1].wrap(doc_width, doc_height)
height_reserved_for_details = 100*mm
remaining_height = max(0, height_reserved_for_details - real_height)
story.append(Spacer(1, remaining_height))
flowable_list.append(Paragraph('Total: 0', pg_style_1))
添加回答
舉報