这段时间在做一个图片PDF的项目, 需要把网页转换成PDF, 找到了python的xhtml2pdf这个库, 个方面都满意,只是对中文的支持不好, html上的中文转换成PDF后都成了乱码, 貌似中文网站都没有找到合适的解决办法, 只能求助于万能的google, 果然不负我望, 废话不多说, 上代码:
django代码
class GeneratePdf(View):
def get(self, request, *args, **kwargs):
# Prepare context
data = {'today': datetime.date.today()}
template = get_template('pdf.html')
html = template.render(Context(data))
io = StringIO.StringIO()
pisa.CreatePDF(html, dest=io, link_callback=link_callback)
io.seek(0)
pdf = io.read()
io.close() # Don't forget to close the file handle
return HttpResponse(pdf, content_type=