16.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
原文地址:https://www.cnblogs.com/liuzhanghao/p/11765418.html