3

XPreformatted を使用して書式設定済みのテキストを印刷していますが、改行に問題があります。

改行は正しく翻訳されていますが、さらに各行の最後に「疑問符」が表示されます。

これは私の出力です:

first line?
second line?
some more text

私は mysql で django を使用しており、データベース フィールドは単純な varchar フィールドです。

データベースで確認したところ、「first lineE Second line」の「e」と「s」の間にあるものはすべて改行文字です。改行文字とは、「Enter」を押したときにデータベースに入力されるものを意味します;-)

したがって、一方では改行が改行として正しく解釈され、さらに間違った疑問符があるのは奇妙に思えます。

ここで助けを見つけたいと思っています。よろしくトム

4

1 に答える 1

1

わかりました、この動作を回避する方法がわかりました。\n の前にある文字を削除するだけです。そのバイト文字は 13 です。したがって、この文字を削除するための gready repair アルゴリズムを作成し、私の 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 に答える