Reportlab with line breaks using XPreformatted - additionally marked with a question mark

I use XPreformatted to print some pre-formatted text, and I have a problem with line breaks.

Line breaks are correctly translated, but in addition, I get a “question mark” at the end of each line.

This is my conclusion:

first line?
second line?
some more text

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

I checked it in the database and everything that is between the "e" and the "s" in the "first linE Second line" is the new line symbol. With a new line character, I mean what is entered into the database when I press "enter"; -)

Thus, it seems strange to me that, on the one hand, the new line is correctly interpreted as a new line, and, in addition, there is an incorrect question mark.

hope to find some help here best regards Tom

+3
source share
1 answer

ok Now I know how to get around this behavior. I just delete the character that comes before \ n. Its byte character is 13. Thus, I created a correction algorithm to remove this character, and my world of PDF file generation is again beautiful; -)

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)
+1
source

Source: https://habr.com/ru/post/1736279/


All Articles