python pdfkit html转pdf响应式* django例

pip install pdfkit

本例用django做的请求,换成对应框架即可

此方法可将html页面转成pdf下载

 #!/usr/bin/env python
# coding:utf-8 import pdfkit
import random
import datetime
from django.shortcuts import HttpResponse #size:'A6','A4' etc...
def get_pdf(url, size):
options = {'page-size':size, 'margin-top':'','margin-right':'', 'margin-bottom':'', 'margin-left':''}
output = '/tmp/x.pdf-%d' % random.randint(1000, 9999) pdfkit.from_url(str(url), output, options=options)
response = HttpResponse(content_type='application/pdf') now = datetime.datetime.now()
pdfname = now.strftime('%Y%m%d-%H%M%S')
response['Content-Disposition'] = 'attachment; filename="'+pdfname+'.pdf"' with file(output) as fp:
data = fp.read() response.write(data)
return response
上一篇:容器技术|Docker三剑客之docker-machine


下一篇:为何你的php代码没有写结束标签