python - 使用reportlab python库生成孟加拉语PDF

标签 python pdf-generation reportlab

我尝试在 BANGALA labguage 中生成 PDF。这里我添加了 Bangla Uni-Code 字体。但我的 PDF 生成不正确。在这里我添加代码片段和 Imag 的输出。为什么孟加拉语中的字体无法正常工作或 PDF 无法正确生成?

这是我的代码

import os
from io import BytesIO
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4, inch, landscape
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Frame
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT, TA_JUSTIFY
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from django.http import HttpResponse


def download_pdf_file(request):

    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
    buffer = BytesIO()


    doc = SimpleDocTemplate(buffer, pagesize=A4, rightMargin=30, leftMargin=30, topMargin=30,
                            bottomMargin=18)
    doc.pagesize = landscape(A4)
    elements = []
    fontdiractory="/home/osman/font/"
    pdfmetrics.registerFont(TTFont('bangla', os.path.join(fontdiractory, 'Siyamrupali_1_01.ttf')))
    styles = getSampleStyleSheet()
    style_centre = ParagraphStyle(name='centre', parent=styles['Heading2'],fontName='bangla', alignment=TA_CENTER)

    p = Paragraph("গণপ্রজাতন্ত্রী বাংলাদেশ", style_centre)
    elements.append(p)
    p = Paragraph("UEO Office", style_centre)
    elements.append(p)
    p = Paragraph("Upazilla: "+'Tongipara'+ " District: "+'Gopalgonj', style_centre)
    elements.append(p)

    data = [
        ['Govt. Primary School','','','','','','',''],
        ['Ebtedaiye Madrasha','','','','','','',''],
        ['গণপ্রজাতন্ত্রী বাংলাদেশ','','','','','','',''],
        ['Primary school','','','','','','',''],
        ['Primary school','','','','','','',''],
        ['Primary school','','','','','','','']
    ]

    # TODO: Get this line right instead of just copying it from the docs
    style = TableStyle([('ALIGN', (1, 1), (-2, -2), 'RIGHT'),
                        ('TEXTCOLOR', (1, 1), (-2, -2), colors.red),
                        ('VALIGN', (0, 0), (0, -1), 'TOP'),
                        ('TEXTCOLOR', (0, 0), (0, -1), colors.blue),
                        ('ALIGN', (0, -1), (-1, -1), 'CENTER'),
                        ('VALIGN', (0, -1), (-1, -1), 'MIDDLE'),
                        ('TEXTCOLOR', (0, -1), (-1, -1), colors.green),
                        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
                        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
                        ])

    data2 = [[Paragraph(cell, style_centre) for cell in row] for row in data]
    t = Table(data2)
    t.setStyle(style)
    elements.append(t)

    doc.build(elements)
    response.write(buffer.getvalue())
    buffer.close()
    return response

Output Image

最佳答案

我可以看到默认的canvas_basefontname是“Helvetica”see in reportlab config doc所以我将 canvas_basefontname 更改为我所需的字体。就是这样。我在 link 中添加以下代码。

from reportlab import rl_config
rl_config._SAVED['canvas_basefontname'] = 'bangla'
rl_config._startUp()

关于python - 使用reportlab python库生成孟加拉语PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45606548/

相关文章:

python - 删除应用后无法删除用户

python - 创建可变数量的类实例

python - 在Python中解析带有嵌套列表的字典列表中的值

python - 如何以列而不是行的形式显示表格中的数据?

python - reportlab 值错误 : Invalid color value 'initial'

python - 如何在 jinja2 中使用 enumerate(zip(seq1,seq2))?

pdf-generation - 文本宽度和高度的 HPDF 单位

pdf-generation - iText:具有不同单元格/列宽度的不同表格行

python - 如何在Python中使用PDFKit发送cookie?

python - 一个段落中的 Reportlab 粗体和普通文本