【问题标题】:Generating PDF in Bangla Language with reportlab python library使用reportlab python库生成孟加拉语PDF
【发布时间】:2018-01-18 06:43:23
【问题描述】:

我尝试在 BANGALA labguage 中生成 PDF。在这里,我添加了 Bangla Uni-Code 字体。但是我的 PDF 没有生成正确的。在这里,我添加了 Imag 的代码片段和输出。为什么字体无法正常工作或在 Bangla 中无法正确生成 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

【问题讨论】:

  • 如果您对此有解决方案question请帮助。
  • @Jibon ,我从代码库中跳过 ReportLab 并使用另一个库 weasyprint。我认为它很方便和有用。

标签: python pdf-generation reportlab


【解决方案1】:

我可以看到默认 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多