Bootstrap CSS不适用于weasyprint [英] Bootstrap CSS is not applied in weasyprint

查看:162
本文介绍了Bootstrap CSS不适用于weasyprint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个flask应用程序,可以使用weasyprint从html生成.pdf并将其作为附件发送.但是显然没有应用bootstrap 4 css.我找不到解决方案. pdfkit可以很好地工作,但是我需要weasyprint,因为pythonanywhere.com不支持pdfkit.

I have made a flask application which generates .pdf from html with weasyprint and send it as attachment. But apparently bootstrap 4 css is not applied. I can't find a solution. It is working well with pdfkit but I need weasyprint, since pythonanywhere.com does not support pdfkit.

我尝试使用HTML中的bootstrap css作为文件链接引导程序,但是没有区别.

I have tried linking bootstrap, using bootstrap css as file in my html, but there was no difference.

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

This is my python part, which generates and sends 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.为了使其能够正确呈现,因此可能不值得.

I know this is an old thread but I stumbled over it while looking for an answer to the same question, so I'm adding my workaround in case it helps anyone else. I'm going to start by pointing out that although this does work Bootstrap really isn't intended to be used for printing, and weasyprint ignores anything inside a @media query so you're going to need to add a lot of css anyway in order to make it render correctly so it's probably not worth it.

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

Using the code from the original question, if you add the bootstrap cdn css to the stylesheets list it will render:

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渲染的问题,我发现此技巧在应用字体方面非常有用.

However, given the issues with Bootstrap rendering, I found this trick to be considerably more useful for applying fonts.

这篇关于Bootstrap CSS不适用于weasyprint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆