わかりました、この動作を回避する方法がわかりました。\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)