#!/usr/bin/env python
# -*- coding: utf-8 -*-
from reportlab.pdfgen.canvas import Canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.styles import ParagraphStyle,PropertySet
from reportlab.platypus import Paragraph
from reportlab.lib.enums import *
from reportlab.lib.colors import *
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, SimpleDocTemplate, PageBreak
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('msyh', 'msyh.ttf'))
stylesheet=getSampleStyleSheet()
import copy
c = Canvas('d:/test/temp.pdf', pagesize=letter)
#c.setFont('STSong-Light', 16)
c.setFont('msyh', 16)
y = 700
c.drawString(100, y, u'测试!!测试!!测试!!测试!!测试!!测试!!测试!!测试!!测试!!测试!!测试!!测试!!测试!!换行')
c.drawString(100, y+30, 'sdgsldjglsdjglsjdfgljsdfogijseoirjgklsdfgklsdjglkjsdlfjgsldjfglsjdfgkljsdlfkjgsdlkfjglsdkfjglsdjfglksjdflgj')
c.save()
from reportlab.lib import colors
from reportlab.graphics.shapes import *
from reportlab.graphics import renderPDF
from reportlab.graphics import renderPM
d = Drawing(400, 200)
d.add(Rect(50, 50, 300, 100, fillColor=colors.yellow))
#d.add(String(150,100, '您好', fontSize=18, fontName='STSong-Light', fillColor=colors.red))
d.add(String(150,100, '您好', fontSize=18, fontName='msyh', fillColor=colors.red))
#renderPDF.drawToFile(d, 'd:/test/example1.pdf', 'My First Drawing')
renderPM.drawToFile(d, 'd:/test/example1.png', 'PNG')
print "ok"
中文問題主要是使用中文字體
1、使用cid字體
2、使用ttf字體,轉PNG圖片隻能使用ttf字體不能使用cid字體
pie有中文的話也要設字體
d = Drawing(400, 400)
d.add(String(100, 175, "Test", textAnchor="middle"))
pc = Pie()
pc.x = 25
pc.y = 50
pc.data = [10, 20,30, 40, 50, 60]
pc.labels = ['一', '二', '三', u'四', u'五', u'六']
for i in range(6):
pc.slices[i].fontName = 'msyh'