【问题标题】:How to draw a paragraph from top to bottom on canvas?如何在画布上从上到下绘制段落?
【发布时间】:2018-07-26 14:40:53
【问题描述】:

我一直在尝试使用 PyPDF2 和 Reprortlab 创建一个 pdf。我需要用大量文本绘制一个流畅的段落。问题是段落的大小可能会有所不同。我想为所有页面保持段落的左上角(段落的开头)固定。问题是当我在固定位置(画布上的 x,y)绘制段落时,左下角停留在该位置(x,y)。我想这是 ReportLab 的默认行为。是否有调整或解决方法以从左上角而不是左下角开始段落,以便段落从相同位置开始而不管段落的大小?

【问题讨论】:

    标签: python reportlab pypdf2


    【解决方案1】:

    当使用段落的换行功能时,它将返回换行文本的总高度。这可以与文本所需的位置结合使用,以创建从 0,0 开始绘制文本的外观,即左上角而不是左下角。

    def draw_paragraph(canvas, msg, x, y, max_width, max_height):
        message_style = ParagraphStyle('Normal')
        message = msg.replace('\n', '<br />')
        message = Paragraph(message, style=message_style)
        w, h = message.wrap(max_width, max_height)
        message.drawOn(canvas, x, y - h)
    

    【讨论】:

      【解决方案2】:
      #can canvas
      #openbd is just font opensas it is custom font
        # you can remove fontname parameter 
        def draw_paragraphcontent(self, can, msg, x, y, max_width, max_height, color, fontsize, text_align=TA_CENTER,
                                fontname='openbd'):
      
          message_style = ParagraphStyle('Normal', fontName=fontname, textColor=color, fontSize=fontsize,
                                         alignment=text_align)
          message = msg.replace('\n', '<br />')
          message = Paragraph(message, style=message_style)
          w, h = message.wrap(max_width, max_height)
          message.drawOn(can, x, y - h)
          pass
      

      样本:

      段落样式中提供了这么多样式属性

      Reporlab dev pdf

      【讨论】:

        猜你喜欢
        • 2013-07-29
        • 2018-05-31
        • 1970-01-01
        • 2011-08-16
        • 2014-05-24
        • 1970-01-01
        • 2021-07-15
        • 2023-03-14
        • 1970-01-01
        相关资源
        最近更新 更多