Created
October 18, 2014 06:19
-
-
Save shuxiang/ce7a0687285c58d23ed1 to your computer and use it in GitHub Desktop.
flask operate pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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