繁体   English   中英

Django Weasyprint 引导样式丢失

[英]Django Weasyprint bootstrap styling lost

我在 Django 中有一个 HTML 模板,我正在使用 WeasyPrint 创建一个 PDF。 但是,当创建 PDF 时,我的引导程序样式丢失了:

html_string = render_to_string('index.html', {'data' : data, 'total' : total_price, 'cash' : cash_price, 'paid' : paid, 'hm_fees' : hm_fees, 'invoice_number' : invoice_number, 'total_sales' :total_sales})
html = HTML(string=html_string)
result = html.write_pdf()

response = HttpResponse(content_type='application/pdf;')
response['Content-Disposition'] = 'inline; filename=invoice.pdf'
response['Content-Transfer-Encoding'] = 'binary'
with tempfile.NamedTemporaryFile(delete=True) as output:
    output.write(result)
    output.flush()
    output.seek(0)
    response.write(output.read())

return response

任何帮助表示赞赏

我们需要将 WeasyPrint 中的 CSS 添加到 Django 视图中:

from weasyprint import HTML, CSS

还需要以这种方式引用引导文件:

result = html.write_pdf(stylesheets=[CSS('https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css',)])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM