简体   繁体   English

Reportlab:如何在pdf中对齐文本

[英]Reportlab: how to align text in pdf

I'm trying to convert csv to pdf file but when i add text(address) in pdf it does not align in proper way. 我正在尝试将csv转换为pdf文件,但是当我在pdf中添加text(address)时,它无法以正确的方式对齐。 Below image shows pdf details where From address does not aligned. 下图显示了pdf详细信息,其中发件人地址未对齐。 How to give space from left side please help me 如何从左边给空间请帮助我

Actual o/p in pdf 实际o / p pdf

在此处输入图片说明

Expected o/p 预期o / p

                                        From:
                                        SAIFEE SHIP SPARE PARTS AND CHANDLERS
                                        TRN: 100069582300003
                                        AL JADAF
                                        UNITED ARAB EMIRATES
                                        Fax: 0097148124444

i tried below code 我尝试下面的代码

         style_right = ParagraphStyle(name='right', parent=styles[
'Normal'], alignment=TA_RIGHT)

       # PDF Text - Content
        line11 = 'From:'
        line12 = 'SAIFEE SHIP SPARE PARTS AND CHANDLERS'
        line13 = 'TRN: 100069582300003'
        line14 = 'AL JADAF'
        line15 = 'UNITED ARAB EMIRATES'
        line16 = 'Fax: 0097148124444'

        elements.append(Paragraph(line11, style_right))
        elements.append(Paragraph(line12, style_right))
        elements.append(Paragraph(line13, style_right))
        elements.append(Paragraph(line14, style_right))
        elements.append(Paragraph(line15, style_right))
        elements.append(Paragraph(line16, style_right))
        elements.append(Spacer(inch, .25 * inch))

The PDF image is correct as per code (right aligned). PDF图像按照代码正确(右对齐)。 You can use left style and give x, y position (or something like that) for FROM address to start.(I did this kind of things in JasperReports, may be useful for you). 您可以使用左样式并指定x,y位置(或类似的位置)作为FROM地址的开头。(我在JasperReports中做了这种事情,可能对您有用)。

alignment=TA_RIGHT enforces every line to be at extreme right of the document. alignment=TA_RIGHT强制每一行都位于文档的最右边。

change your paragraph style to: 将您的段落样式更改为:

    style_right = ParagraphStyle(name='right',leftIndent=250)

adjust the value of leftIndent according to the width of your document. 根据文档的宽度调整leftIndent的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM