Skip to content

Instantly share code, notes, and snippets.

@shuxiang
Created October 18, 2014 06:19
Show Gist options
  • Save shuxiang/ce7a0687285c58d23ed1 to your computer and use it in GitHub Desktop.
Save shuxiang/ce7a0687285c58d23ed1 to your computer and use it in GitHub Desktop.
flask operate pdf
from reportlab.pdfgen import canvas
from io import BytesIO
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
# 采用微软雅黑字体
def some_view():
pdfmetrics.registerFont(TTFont('msyh', os.path.join(veteransApp.config['PROJECT_DIR'], '../msyh.ttf')))
# Create the PDF object, using the response object as its "file."
buffer = BytesIO()
p = canvas.Canvas(buffer)
p.setFont("msyh", 12)
p.drawString(100, 100, u"Hello world 测试中文.")
# Close the PDF object cleanly, and we're done.
p.showPage()
p.save()
pdf = buffer.getvalue()
buffer.close()
resp = make_response(pdf)
resp.headers['Content-Disposition'] = 'attachment; filename=report.pdf'
resp.headers["Content-type"] = "application/pdf"
return resp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment