gpt4 book ai didi

python - Bootstrap CSS 未应用于 weasyprint

转载 作者:行者123 更新时间:2023-11-28 15:11:44 28 4
gpt4 key购买 nike

我制作了一个 flask 应用程序,它使用 weasyprint 从 html 生成 .pdf 并将其作为附件发送。但显然没有应用 bootstrap 4 css。我找不到解决办法。它与 pdfkit 配合得很好,但我需要 weasyprint,因为 pythonanywhere.com 不支持 pdfkit。

我试过链接 bootstrap,在我的 html 中使用 bootstrap css 作为文件,但没有区别。

这是我的 python 部分,它生成并发送 pdf。

@app.route('/pdf_send', methods=('GET', 'POST'))
@login_required
def pdf_send():

rendered = render_template(
'pdf_send2.html',
name=name_g,
surname=surname_g,
email=email_g,
address=address_g,
invoice_no=invoice_no_g,
dict_g=dict_g,
bendra_suma_g = ('%.2f' % round(float(bendra_suma_g[0]), 2)),
send=send_g,
today=today_g
)

css_file = ('static/bootstrap.css')
filename = 'SF-' + invoice_no_g +'.pdf'
html = HTML(string=rendered)
css = CSS(filename=css_file)
html.write_pdf(filename, stylesheets=[css])
send_email(filename, email_g)

return redirect(url_for('index'))

最佳答案

我知道这是一个旧线程,但我在寻找同一问题的答案时偶然发现了它,所以我添加了我的解决方法以防它对其他人有帮助。我将首先指出,虽然这确实有效,但 Bootstrap 实际上并不打算用于打印,并且 weasyprint 会忽略 @media 查询中的任何内容,因此您将需要无论如何添加很多 css 以使其正确呈现,所以它可能不值得。

使用原始问题中的代码,如果您将 bootstrap cdn css 添加到 stylesheets 列表,它将呈现:

html = HTML(string=rendered)
css = CSS(filename=css_file)
html.write_pdf(filename, stylesheets=[css, "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"])

但是,考虑到 Bootstrap 渲染的问题,我发现这个技巧对于应用字体更为有用。

关于python - Bootstrap CSS 未应用于 weasyprint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49496398/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com