ReportLab:如何从段落的左上点开始写作,因为默认情况下它是从左下点

发布于 2025-02-06 06:05:16 字数 1053 浏览 3 评论 0原文

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4 
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER, TA_RIGHT

pdfmetrics.registerFont(TTFont('Dejavu', 'DejaVuSansCondensed.ttf'


packet_1 = io.BytesIO()
can = canvas.Canvas(packet_1, pagesize=A4)
styles = getSampleStyleSheet()
styleN = styles["BodyText"]


my_style = ParagraphStyle(name ='My style',
                          fontName='Dejavu',
                          fontSize=12,
                          alignment=TA_LEFT,
                          borderWidth = 1,
                          borderColor = 'black'
                          )

p3 = Paragraph('''abdc''', my_style)
p3.wrapOn(can, 500, 100)
p3.drawOn(can, 10, 300)

无论段落高度如何,文本都从底部开始。 如何使其从顶部开始?

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4 
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER, TA_RIGHT

pdfmetrics.registerFont(TTFont('Dejavu', 'DejaVuSansCondensed.ttf'


packet_1 = io.BytesIO()
can = canvas.Canvas(packet_1, pagesize=A4)
styles = getSampleStyleSheet()
styleN = styles["BodyText"]


my_style = ParagraphStyle(name ='My style',
                          fontName='Dejavu',
                          fontSize=12,
                          alignment=TA_LEFT,
                          borderWidth = 1,
                          borderColor = 'black'
                          )

p3 = Paragraph('''abdc''', my_style)
p3.wrapOn(can, 500, 100)
p3.drawOn(can, 10, 300)

Regardless of the paragraph height, the text starts at the bottom.
How to make it start from the top?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2025-02-13 06:05:16

添加bottoRP = 0 exp:exp:

...
can = canvas.Canvas(packet_1, pagesize=A4, bottomup=0)
...

add bottomup=0 exp:

...
can = canvas.Canvas(packet_1, pagesize=A4, bottomup=0)
...
日记撕了你也走了 2025-02-13 06:05:16

免责声明我是borb的作者。

BORB是一个纯Python PDF库,既可以提供低级(您可以在哪个确切的坐标内容中使用)和高级(Borb照顾布局)为您)模型。

您可以查看示例,在段落对齐中有整个部分。您可以发现在这里

在示例中,段落在精确的坐标处添加,并使用各种Alignment参数布置。

我将在此处复制相关示例的完整性:

from borb.pdf.document.document import Document
from borb.pdf.page.page import Page
from borb.pdf.canvas.layout.text.paragraph import Paragraph
from borb.pdf.pdf import PDF
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf.canvas.layout.layout_element import Alignment
from borb.pdf.canvas.layout.annotation.square_annotation import SquareAnnotation
from borb.pdf.canvas.color.color import HexColor

from decimal import Decimal


def main():
    # create Document
    doc: Document = Document()

    # create Page
    page: Page = Page()

    # add Page to Document
    doc.append_page(page)

    # define layout rectangle
    # fmt: off
    r: Rectangle = Rectangle(
        Decimal(59),                # x: 0 + page_margin
        Decimal(848 - 84 - 100),    # y: page_height - page_margin - height_of_textbox
        Decimal(595 - 59 * 2),      # width: page_width - 2 * page_margin
        Decimal(100),               # height
    )
    # fmt: on

    # this is a quick hack to easily get a rectangle on the page
    # which can be very useful for debugging
    page.append_annotation(SquareAnnotation(r, stroke_color=HexColor("#ff0000")))

    # the next line of code uses absolute positioning
    Paragraph("Hello World!", vertical_alignment=Alignment.TOP).layout(page, r)

    # store
    with open("output.pdf", "wb") as pdf_file_handle:
        PDF.dumps(pdf_file_handle, doc)


if __name__ == "__main__":
    main()

这应该产生以下PDF:

“在此处输入图像说明”

我在段落周围添加了一个红色矩形,以说明内容实际上是用vertaltical_alignment verticaltic_alignment < /code>设置为alignment.top

disclaimer I am the author of borb the library used in this answer.

borb is a pure Python PDF library that provides both a low-level (you decide at which exact coordinates content goes) and a high-level (borb takes care of the layout for you) model.

You can check out the examples, there's a whole section on Paragraph alignment. You can find that here.

In the examples, a Paragraph is added at exact coordinates, and laid out with various Alignment parameters.

I'll copy the relevant example here for completeness:

from borb.pdf.document.document import Document
from borb.pdf.page.page import Page
from borb.pdf.canvas.layout.text.paragraph import Paragraph
from borb.pdf.pdf import PDF
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf.canvas.layout.layout_element import Alignment
from borb.pdf.canvas.layout.annotation.square_annotation import SquareAnnotation
from borb.pdf.canvas.color.color import HexColor

from decimal import Decimal


def main():
    # create Document
    doc: Document = Document()

    # create Page
    page: Page = Page()

    # add Page to Document
    doc.append_page(page)

    # define layout rectangle
    # fmt: off
    r: Rectangle = Rectangle(
        Decimal(59),                # x: 0 + page_margin
        Decimal(848 - 84 - 100),    # y: page_height - page_margin - height_of_textbox
        Decimal(595 - 59 * 2),      # width: page_width - 2 * page_margin
        Decimal(100),               # height
    )
    # fmt: on

    # this is a quick hack to easily get a rectangle on the page
    # which can be very useful for debugging
    page.append_annotation(SquareAnnotation(r, stroke_color=HexColor("#ff0000")))

    # the next line of code uses absolute positioning
    Paragraph("Hello World!", vertical_alignment=Alignment.TOP).layout(page, r)

    # store
    with open("output.pdf", "wb") as pdf_file_handle:
        PDF.dumps(pdf_file_handle, doc)


if __name__ == "__main__":
    main()

This should produce the following PDF:

enter image description here

I added a red rectangle around the Paragraph to illustrate that the content is in fact laid out with vertical_alignment set to Alignment.TOP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文