我无法使用以下示例代码输出 PDF
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
def get_table(self, response):
buffer = StringIO()
doc = SimpleDocTemplate(buffer, pagesize=letter)
elements = []
data= [['00', '01', '02', '03', '04'],
['10', '11', '12', '13', '14'],
['20', '21', '22', '23', '24'],
['30', '31', '32', '33', '34']]
t=Table(data)
t.setStyle(TableStyle([('BACKGROUND',(1,1),(-2,-2),colors.green),
('TEXTCOLOR',(0,0),(1,-1),colors.red)]))
elements.append(t)
doc.build(elements)
response.out.write(buffer.getvalue())
buffer.close()
return response
我正在使用 webapp2.RequestHandler 并设置了响应标头,如下所示:
self.response.headers['Content-Type'] = 'application/pdf'
self.response.headers['Content-Disposition'] = 'filename=my.pdf'
我得到的结果是一个空白的 pdf 页面。