html转pdf的一个小示例

def get_html(request):
    data = {"name": "alex", "age": 12, "sex": "男"}
    return render(request, "login.html", {"data": data})


def export_pdf(request):
    options = {
        'page-size': 'A4',
    }
    path_wk = r"D:\wk\wkhtmltopdf\bin\wkhtmltopdf.exe"  这个路径是wkhtmltopdf的安装路径
    config = pdfkit.configuration(wkhtmltopdf=path_wk)
    path = "127.0.0.1:8000/get_html"
    pdf = pdfkit.from_url(path, False, options=options, configuration=config)
    response = HttpResponse(pdf)
    response['Content-Type'] = 'application/octet-stream
    指定文件的名称, response['Content-Disposition'] = 'attachment;filename="liuzhanghao.pdf"' return response

 

上一篇:PHP-wkhtmltopdf:如何使用?


下一篇:Odoo 12开发之报表和服务端 QWeb