我想在reportLab中为我的pdf文件设置西里尔字体。我找到了一些关于它的文章,例如:
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
答案 0 :(得分:2)
您可以创建自己的风格并随处使用吗?例如:
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY, fontName='Times', fontSize=10))
styles.add(ParagraphStyle(name='Justify-Bold', alignment=TA_JUSTIFY, fontName='Times-Bold'))
bold_style = styles['Justify-Bold']
normal_style = styles['Justify']
或:
doc_title = copy.copy(styles["Heading1"])
doc_title.alignment = TA_CENTER
doc_title.fontName = 'Times-Bold'
doc_title.fontSize = 16
答案 1 :(得分:0)
pdfmetrics.registerFont(TTFont('DejaVuSerif', 'DejaVuSerif.ttf'))
('FONT', (0, 0), (6, 0), 'DejaVuSerif'),