Django Weasyprint引导程序样式丢失



我在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',)])

最新更新