使用reportlab XPreformatted打印带有换行符的附加问号

发布于 2024-08-24 08:58:02 字数 369 浏览 7 评论 0原文

我正在使用 XPreformatted 打印一些预先格式化的文本,并且我有一个 换行问题。

换行符已正确翻译,但另外我得到了 每行末尾有“问号”。

这是我的输出:

first line?
second line?
some more text

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

我在数据库中检查了它以及“e”之间的所有内容 而“first linE Second line”中的“s”是换行符。

对于换行符,我的意思是当我按“输入”时在数据库中输入的内容;-)

因此对我来说,一方面新行是 正确解释为换行,此外还有一个错误的问号。

I'm using XPreformatted to print some preformated text and I have an
issue with the line breaks.

The line breaks are correctly translated but additionally I get a
"question mark" at the end of each line.

This is my output:

first line?
second line?
some more text

I'm using django with mysql and the database field is a simple varchar field.

I checked it in the database and everything what is between the "e"
and the "s" in "first linE Second line" is a new line character.

With new line character I mean what is entered in the database when I press "enter" ;-)

Thus for me it seems strange that on the one hand the new line is
correctly interpreted as a new line and additionally there is a wrong question mark.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬心 2024-08-31 08:58:02

好吧,我现在知道如何规避这种行为。我只是删除 \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)

ok I know now how to circumvent this behavior. I just remove the character which is before the \n . Its the byte character 13. Thus I created a gready repair algorithm to remove this character and my pdf generating world is fine again ;-)

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

How to use it:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文