[我正在尝试使用pdf套件从我的新Flask应用制作pdf。我正在尝试从我的静态目录加载css文件,但它似乎无法正常工作。
我所依据的基本假设是模板内的css链接不会被拾取(如here中所述。
我尝试了多个建议,这些建议在这里出现在不同的问题中,但没有一个起作用:使用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
您是否尝试在html中导入CSS?
类似:
<link rel="stylesheet" href="/questionary/static/pdf.css"/>