FPDF2 模板文本之间的多行空格,如换行符

问题描述 投票:0回答:1

如果我使用 FlexTemplate 并向其中输入多行文本。它显示行就像在每个换行符之间显示换行符一样。

我用普通的 multi_cell() 尝试了相同的文本内容,它显示得很好。但模板中没有。

python版本:3.11.1

fpdf2版本:2.7.9

这是我的代码,您可以在其中直接看到此行为:

from datetime import datetime
from fpdf import FPDF
from fpdf import FlexTemplate

ra_address = "Dummy Firstname Lastname Streetname. 123 12345 London"
title = "Y o u   A r e   A m a z i n g"
logo_path = "assets/Logo-test.png"
generic_info = "Tel.: 000 / 000 000 0 - 0\nFax: 000 / 000 000 1\nMail: [email protected]"
created = "Created: "+datetime.now().strftime("%d.%m.%Y")
legend = """My Legend: CL = Countless:  1 = yearly / 2 = half / 4 = quarter / 5 = monthly / 6 = single  Options: C = Cookie / ED = Endcake / LW = Lower Water / ST = Storm / IZ = IceZip / GL = Global / UG = Unknown Geodez\u00ad
KIZ:   KH = KiloHertz  /  VK = ValonK  /  TK = TalonK  / SFR = SuperForeignRequest  /  SB = Sabertooth  /  Y = Yes   /  N = No  STA:   A = Active  /  Q = Quiet  /  S = Sold  /  F = Foreign  CC = CoveredCOOP  OP=OverPowered"""
pagecount = "1"


elements = [
    { 'name': 'my_logo', 'type': 'I', 'x1': 250, 'y1': 6, 'x2': 280, 'y2': 36, 'font': None, 'size': 0.0,  'align': 'C', 'priority': 2, },
    { 'name': 'my_info', 'type': 'T', 'x1': 228.6, 'y1': 36.7, 'x2': 281.1, 'y2': 45.7, 'font': 'calibri', 'size': 9.0, 'align': 'C', 'priority': 2, 'multiline': True, 'wrapmode': 'WORD'},
    { 'name': 'ra_address', 'type': 'T', 'x1': 11, 'y1': 19.10, 'x2': 78.92, 'y2': 31.1, 'font': 'calibri', 'size': 12,  'priority': 2, 'multiline': True, 'wrapmode': 'WORD'},
    { 'name': 'title_box', 'type': 'B', 'x1': 100, 'y1': 12.0, 'x2': 192, 'y2': 21, 'align': 'C', 'priority': 0, 'background': 0xaaffaa},
    { 'name': 'title', 'type': 'T', 'x1': 100, 'y1': 12.0, 'x2': 192, 'y2': 21, 'font': 'calibri', 'size': 16, 'bold': 1, 'align': 'C', 'priority': 2, 'foreground': 0x000000},
    { 'name': 'created', 'type': 'T', 'x1': 250, 'y1': 49.1, 'x2': 280, 'y2': 54.1, 'font': 'calibri', 'size': 10, 'bold': title, 'align': 'C', 'priority': 2},
    { 'name': 'legend', 'type': 'T', 'x1': 11, 'y1': 190, 'x2': 287, 'y2': 197, 'font': 'calibri', 'size': 7, 'bold': title, 'priority': 2, 'multiline': True, 'wrapmode': 'CHAR'},
    { 'name': 'pagecount', 'type': 'T', 'x1': 267, 'y1': 195.1, 'x2': 281, 'y2': 201.5, 'font': 'calibri', 'size': 8, 'align': 'C', 'priority': 3, },
]
pdf = FPDF(format="A4", orientation="landscape")
pdf.set_title("My awesome Title")
pdf.set_author("Ex Calibur")
pdf.set_margin(0)

pdf.add_font("calibri", style="", fname="assets/calibri.ttf")
pdf.add_font("calibri", style="b", fname="assets/calibrib.ttf")
pdf.add_font("calibri", style="i", fname="assets/calibrii.ttf")
pdf.add_font("calibri", style="bi", fname="assets/calibriz.ttf")
pdf.set_font("calibri")
pdf.set_text_color("#000000")
pdf.set_draw_color("#aaaaaa")
pdf.set_fill_color("#fefefe")

pdf.add_page()

pdf.set_xy(228.6, 36.7)
# the generic info when done directly with multi_cell does a correct line height
pdf.multi_cell(new_x="RIGHT", new_y="TOP", w=0, text=generic_info, align='C')



tmpl = FlexTemplate( pdf, elements=elements)
tmpl["ra_address"] = ra_address
tmpl["my_logo"] = logo_path
tmpl["my_info"] = generic_info
tmpl["title"] = title
tmpl["created"] = created
tmpl["legend"] = legend
tmpl["pagecount"] = pagecount

tmpl.render()

pdf.set_xy(10, 58)
TABLE_DATA = [
    { "config": { "colspan": 8 }, "values": ["Group 1"]},
    { "config": { }, "values":["Col 1", "Col 2", "Col 3", "Col 4", "Col 5", "Col 6", "Col 7", "Col 8"]},
    { "config": { }, "values":["Test data", "123-456-789", "abcdef", "01.01.2024", "01.01.2025", "1", "Code", "_.___"] },
    { "config": { "colspan": 8 }, "values":["Nested table with mostly one single row"] }
]

with pdf.table() as table:
    for data_row in TABLE_DATA:
        row_config = data_row.get("config")
        values = data_row.get("values")
        row = table.row()
        for val in values:
            row.cell(val, colspan=row_config.get("colspan", 0))

pdf.output("template-test.pdf")

额外的行/空格

它应该是什么样子

谢谢你

python pdf-generation fpdf pyfpdf fpdf2
1个回答
0
投票

如此处解释:https://github.com/py-pdf/fpdf2/issues/1237

模板中多行文本的 y1 到 y2 尺寸需要单行的大小,而不是整个块的大小。

提示:y2 的一个较好值是:y2 = (font-size/2)+y1。

© www.soinside.com 2019 - 2024. All rights reserved.