首页 > 解决方案 > 烧瓶pdfkit没有找到css

问题描述

我正在尝试使用 pdf 工具包从我的新 Flask 应用程序制作 pdf 文件。我正在尝试从我的静态目录加载一个 css 文件,但它似乎不起作用。

我依赖于不会拾取模板内链接的 CSS 的基本假设(如此所述)。

我在这里尝试了在不同问题中提出的多个建议,但没有一个有效:使用 url_for 加载文件路径,使用带有 _external=True 的 url_for,使用文字路径......

No such file or directory: 'pdf.css'尝试过的所有解决方案都出错了。

任何建议将不胜感激,我在这里迷路了。

谢谢!

我的pdf制作功能:

@login_required
def answers_pdf(username):
    # only the owner can download his own pdf
    if username != current_user.username:
        abort(403)
    rendered = render_template('pdf_template.html', user=current_user)

    css = ['static/pdf.css']

    pdf = pdfkit.from_string(
        rendered,
        False,
        css=css
    )

    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'attachment; filename=out.pdf'

    return response
My project tree:
├───questionary
│   ├───main
│   │   └───function in routes.py file here
│   ├───static
│   │   └───css here
│   ├───templates
│   │   └───pdf template here
|   run.py -- running the app as a module from here

标签: pythoncssflaskpdfkit

解决方案


您是否尝试在 html 中导入 css?

就像是:

<link rel="stylesheet" href="/questionary/static/pdf.css"/>

推荐阅读