Generating PDF in Bangla Language with reportlab python library

喜欢而已 提交于 2019-12-13 18:19:45

问题


I have try to generate PDF in BANGALA labguage. Here I added Bangla Uni-Code font. But My PDF is not genated correclty. here I add Code Snippet and output of Imag. Why Font is not working properly or PDF is not generated properly in Bangla?

Here is my code

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


回答1:


I can see Default canvas_basefontname is "Helvetica" see in reportlab config doc so I change the canvas_basefontname to my required font. that`s it. I add bellow code fount in this link.

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


来源:https://stackoverflow.com/questions/45606548/generating-pdf-in-bangla-language-with-reportlab-python-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!