ReportLab - 重叠的单词

时间:2014-02-13 11:42:08

标签: reportlab

HY,

我正在使用报告实验室进行pdf生成。我想绘制一个Paragraph的文本,代表一个标题,所以,如果标题比段落的可用更长,它将在下一行分割,如果标题长到适合段落的可用的宽度和高度将比调整文本大小。

它的工作正常,字体为10,但是,如果我选择字体较大的样式,例如:

title_style = ParagraphStyle("title", fontName='Helvetica', fontSize=50, alignment=TA_CENTER, backColor=None)

比如下图所示绘制文字时,单词重叠: sample image

这是我的代码:

def draw_on(canvas, x, y, paragraph, style, text, available_width, available_height, min_font_size=8):
    w, h = paragraph.wrap(available_width, available_height)
    temp_font_size = paragraph.style.fontSize

    while temp_font_size > min_font_size:

        if w <= available_width and h <= available_height:
            paragraph.drawOn(canvas, x, y)
            break
        else:

            temp_font_size -= 1
            style.fontSize = temp_font_size
            paragraph = Paragraph(text, style)
            w, h = paragraph.wrap(available_width, available_height)



def generate_pdf():

    c = canvas.Canvas("FirstPage.pdf")

    title_style = ParagraphStyle("title", fontName='Helvetica', fontSize=10, alignment=TA_CENTER, backColor=None)

    title_text = 'If title has a small font, everything s ok.'
    title_paragraph = Paragraph(title_text, title_style)
    title_paragraph_available_width = 2*inch
    title_paragraph_available_height = 1*inch
    title_min_font_size = 8

    draw_on(c, 2*inch, 5*inch, title_paragraph, title_style, title_text, title_paragraph_available_width,
            title_paragraph_available_height, title_min_font_size)


    c.showPage()
    c.save()

任何人都有一个想法,为什么会发生这种情况以及如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

用户指南第67页对此进行了描述:

  

fontSize和fontName标签很明显,但设置很重要   领先的。这是相邻文本行之间的间距;一个   好的经验法则是使这个点大小比点大20%。

因此,在您的情况下,您需要将leading = 50 * 1.2添加到ParagraphStyle

另外,作为附注,我建议你要么充分利用报告文件Platypus(Paragraph等)的全部潜力,要么坚持使用reportlabs pdfgen,这是更基本和易于编码。鸭嘴兽适合较长的文本和相对定位(想想乳胶),pdfgen更适合绝对定位(单词)。但这只是我的意见......

相关问题