Skip to main content
 首页 » 编程设计

python之ReportLabs Django 集成

2024年11月01日29zengkefu

我正在关注 this link在 Django 中集成 ReportLabs。我做的一切都正确,但我收到一条错误消息 -

Exception Type: RenderPMError 
 
Exception Value:     
Can't setFont(Times-Roman) missing the T1 files? 
Originally <type 'exceptions.TypeError'>: makeT1Font() argument 2 must be string, not None 

我哪里错了?我想该字体在 ReportLabs 包中不可用。我该如何从这里开始?

请您参考如下方法:

注册字体:

import os 
from reportlab.pdfbase import pdfmetrics   
from reportlab.pdfbase.ttfonts import TTFont 
 
pdfmetrics.registerFont(TTFont("Arial", os.path.join(settings.PROJECT_ROOT, 'static', 'fonts', 'arial.ttf'))) # here i have my fonts for PDF's 

使用它,例如:

page = canvas.Canvas(response, pagesize=A4, pageCompression = 0) 
 
canvas.setFont('Arial',9)