3

我正在使用 XPreformatted 打印一些预先格式化的文本,但我遇到了换行问题。

换行符已正确翻译,但另外我在每行末尾都有一个“问号”。

这是我的输出:

first line?
second line?
some more text

我将 django 与 mysql 一起使用,数据库字段是一个简单的 varchar 字段。

我在数据库中检查了它,“第一行第二行”中“e”和“s”之间的所有内容都是换行符。换行符是指当我按“输入”时在数据库中输入的内容;-)

因此,对我来说,一方面新行被正确解释为新行,而且还有一个错误的问号,这似乎很奇怪。

希望在这里找到一些帮助最好的问候汤姆

4

1 回答 1

1

好的,我现在知道如何规避这种行为。我只是删除 \n 之前的字符。它是字节字符 13。因此,我创建了一个强大的修复算法来删除这个字符,我的 pdf 生成世界又好了;-)

def repair_string_for_xpreformatted_use(value):
    #remove element before \n
    #it is an empty element interpreted from XPreformatted as a question mark
    #i guess this element is coming from the mysql db. test it with postgres db!
    #this is definitely a greedy repair algorithm

    lb_index = value.find('\n')
    start_index = 0
    end_index = len(value)
    value_rep = ""
    while lb_index != -1:
        lb_index = value.find('\n', 1)
        byte_list = toBytes(value[lb_index-1])
        if byte_list[0] == 13:
            #13 is the strange byte character which should not be there. remove it.. bye bye number 13
            value_rep += value[start_index:lb_index-1]
        else:
            #do not remove the character. we do not want to strip some user information ;-)
            value_rep += value[start_index:lb_index]

        value = value[lb_index:end_index]
        if lb_index == (end_index -1) or lb_index == end_index:
            lb_index = -1
        end_index = len(value)
    return value_rep

如何使用它:

from reportlab.platypus import XPreformatted
footerstyle = ParagraphStyle(name='footerstyle', leading=6, spaceBefore=0, spaceAfter=0, textColor=gray2, fontName='Calibri', fontSize=5, alignment=TA_RIGHT)
footer_text_rep = repair_string_for_xpreformatted_use(footer_text)
footer_text_pre = XPreformatted(smart_str(footer_text_rep), footerstyle)
于 2010-03-10T16:48:26.587 回答