如何在Flask应用程序中将样式表文件链接到pdfkit? [英] How to link stylesheet files to pdfkit in Flask application?

查看:117
本文介绍了如何在Flask应用程序中将样式表文件链接到pdfkit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Flask应用程序中使用pdfkit从html页面创建pdf,但是在使用pdfkit时无法加载静态文件(样式表).

I am trying to create pdf from html page using pdfkit inside Flask application, but I have troubles to load the static files (stylesheets) when using pdfkit.

我试图提出最小的例子.我已经有了这个文件结构

I've tried to come up with minimal example. I've got this file structure

App
 |_ static
 |  |- style.css
 |_ templates
 |  |- base.html
 |_ pdfs
 |  |- file.pdf
 |_ application.py

内部application.py:

import flask
import pdfkit
app = flask.Flask(__name__)

@app.route('/')
def index():

    page = flask.render_template('base.html')
    pdfkit.from_string(page, 'pdfs/file.pdf')
    return page

@app.route('/download', methods=['POST'])
def download():

    if flask.request.method == 'POST':
        flask.send_from_directory(
        directory='pdfs', filename='file.pdf', as_attachment=True)
    else:
        flask.redirect(flaks.url_for('index'))

if __name__ == '__main__':

    app.run()

样式表仅向td元素添加红色背景:

Stylesheet just adds red background to the td elements:

td {
    background-color: red;
}

最后是base.html:

<!DOCTYPE html>
<html>
<head>
  <title>Test App</title>
  <link href={{ url_for('static', filename='style.css') }} rel='stylesheet'>
</head>
<body>
  <div id='to_pdf'>
    <table>
      <tr>
        <th>Firstname</th>
      </tr>
      <tr>
        <td>Jill</td>
      </tr>
      <tr>
        <td>Eve</td>
      </tr>
    </table>
    <form action='/download' method='POST'>
      <button type="submit" value="Print" />PRINT</form>
    </form>
  </div>
</body>
</html>

很抱歉,代码很多,但这实际上是非常基本的. 我只想拥有一个按钮,单击该按钮即可下载pdf文件(此pdf是html文件,已转换为pdf.

Sorry for the loads of code, but it is actually pretty basic. All I want is to have button, which once clicked downloads a pdf file (this pdf is html file converted to pdf.

工作正常,但控制台输出如下:

It sort of works, but the console output is the following:

Loading pages (1/6)
Warning: Failed to load file:///static/style.css (ignore)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

问题

pdfkit不能在static文件夹中找到样式表文件.我遇到这个问题的应用程序更健壮,使用boostrap等,并且pdf输出格式错误是非常不可取的.

pdfkit, which basically invokes wkhtmltopdf cannot find the stylesheet files inside static folder. The application, where I have this problem is more robust, uses boostrap etc. and having the pdf output badly formatted is very undesirable..

推荐答案

假设您的应用具有config.py文件,则可以添加此配置值.

Assuming your app has config.py file you can add this config value.

ABS_PATH_STATIC_FOLDER = "var/www/.." #here is an absolute path to static dir

然后通过以下方式指定css文件:

Then specify css file by:

css = os.path.join(app.config['ABS_PATH_STATIC_FOLDER'], 'pdfstyle.css')

如果将css文件放置在静态文件夹或任何其他文件夹中,这将起作用

This will work if the css file is placed in static folder or any other folder

然后将CSS从字符串函数传递到pdfkit

Then pass on css to pdfkit fromstring function

pdfkit.from_string(page, 'pdfs/file.pdf', css=css)

这篇关于如何在Flask应用程序中将样式表文件链接到pdfkit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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