我在django框架中使用reportlab pdf库,以便用hindi语言创建pdf报告。我从PostgreSql数据库获取数据,如下所述。
waterdate month year rivername stationname distname
2011-06-22 00:00:00 June 2011 नयार मरोरा पौड़ी
2011-06-22 00:00:00 June 2011 गंगा हरिद्वार हरिद्वार
2011-06-22 00:00:00 June 2011 गंगा नरोरा/डी0एस0 बुलन्दशहर
2011-06-22 00:00:00 June 2011 गंगा फतेहगढ़ फर्रूखाबाद
2011-06-22 00:00:00 June 2011 गंगा गुमटिया कन्नौज
我已编写以下代码将上表显示为pdf文件。
printing.py
from reportlab.lib.pagesizes import letter,A4
from reportlab.platypus import SimpleDocTemplate,Paragraph,Table,TableStyle
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_CENTER,TA_JUSTIFY
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from .models import Rainfall
pdfmetrics.registerFont(TTFont('Hindi1', 'gargi.ttf'))
class printReport:
def rainfall_report(self):
buffer = self.buffer
doc = SimpleDocTemplate(self.buffer,rightMargin=10,leftMargin=10,topMargin=25,bottomMargin=25,pagesize=self.pagesize)
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name="TableHeader",alignment=TA_CENTER,))
styles.add(ParagraphStyle(name="ParagraphTitle", fontSize='11',alignment=TA_JUSTIFY,fontName="FreeSansBold"))
styles.add(ParagraphStyle(name="Justify", fontSize='11',alignment=TA_JUSTIFY,fontName="FreeSansBold"))
data = []
data.append(Paragraph( 'Rainfall Data', styles['Title']))
data.append(Spacer(1,12))
table_data =[]
table_data.append([
Paragraph('Date', styles['TableHeader']),
Paragraph('Month', styles['TableHeader']),
Paragraph('Year',styles['TableHeader']),
Paragraph('River Name',styles['TableHeader']),
Paragraph('Station Name',styles['TableHeader']),
Paragraph('District Name',styles['TableHeader'])
])
rainfall_data = Rainfall.objects.all()[:10]
for rainfall in rainfall_data:
table_data.append([
rainfall.waterdate,
rainfall.month,
rainfall.year,
rainfall.rivername,
rainfall.stationname,
rainfall.districtname
])
user_table = Table(table_data,colWidths=[doc.width/7.0]*7)
user_table.hAlign = 'CENTER'
user_table.setStyle(TableStyle(
[ ('INNERGRID', (0,0), (-1,-1), 0.25 , colors.black),
('BOX',(0,0),(-1,-1,),0.5, colors.black ),
('VALIGN',(0,0),(-1,0), 'MIDDLE'),
('BACKGROUND',(0,0),(-1,0), colors.gray ),
('TEXTCOLOR', (0,1), (-1,-1), colors.black),
('VALIGN',(2,1),(-3,-1),'MIDDLE'),
('ALIGN',(0,1),(-1,-1),'CENTRE'),
( 'FONT' , (3,0),(-1,-1) , 'Hindi1') ]))
data.append(user_table)
doc.build(data)
pdf=buffer.getvalue()
buffer.close()
return pdf
以上代码将数据显示为pdf文件,如下所示。
正如您所见,站名和 distname 列的印地语单词不会像上面提到的数据库表那样正确。 对于instanse,हरिद्वारword在pdf文件中没有正确推迟。
请帮我解决这个问题。 任何帮助都会有很大的帮助。