1
我想爲我的pdf文件在reportLab中設置西里爾字體。我發現一些關於它的文章,例如:setFont在ReportLab中,Django
pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf'))
canvas.setFont('Vera', 32)
但問題是我不使用畫布。我發現這個視圖創建一個表格,並且它不使用canvas
。如何設置我的字體? `
def print_invoice(request, order_id):
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4, inch, landscape
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Arimo-Regular', 'static/themes/font/Arimo-Regular.ttf'))
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
buff = StringIO()
menu_pdf = SimpleDocTemplate(buff, pagesize=A4, rightMargin=30,leftMargin=30, topMargin=30,bottomMargin=18)
menu_pdf.pagesize = landscape(A4)
elements = []
data = [
["fd", "Price per bottle", "Bottles in package",
"Price for package", "Number of packages", "Total price"],
]
order = Order.objects.get(pk=order_id)
for item in order.orderproduct_set.all():
product = item.product
array = [product.brand.name, str(product.price), str(product.package.product_amount),
str(product.package.package_price), str(item.product_amount), str(item.price)]
data.append(array)
#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),
])
#Configure style and word wrap
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
data2 = [[Paragraph(cell, s) for cell in row] for row in data]
t = Table(data2)
t.setStyle(style)
#Send the data and build the file
elements.append(t)
menu_pdf.build(elements)
response.write(buff.getvalue())
buff.close()
return response