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