首页 > 解决方案 > Pdfkit 没有在 Flask 中将我的 Pygal svg 图转换为 pdf

问题描述

我使用 Pygal 绘制了条形图,但是当我使用命令时pdfkit.from_string(....)

这是我的代码片段(routes.py 文件)

@app.route('/home', methods=['POST'])
def get_pdf():
        products=['A', 'B', 'C']
        quants=[200,50,50]

        line_chart = pygal.Bar()
        line_chart.title = 'Stock Availability'
        line_chart.x_labels = products
        line_chart.add('Products',quants)
        line_chart = line_chart.render_data_uri()

        x='name'
        rendered = render_template('inventoryAdmin/status.html',status_list=status_list, cD=countDictionary, line_chart=line_chart)
        pdf = pdfkit.from_string(rendered, False, configuration=config, options=kitoptions)
        response = make_response(pdf)
        response.headers['content-Type']='application/pdf'
        response.headers['Content-Disposition']='attached; filename=.pdf'
        return response
    return redirect(url_for('status'))

这是我的 .html 文件

<div class="container">
        <legend class="border-bottom mb-4">Unallocated Items</legend>
            <div class="container" align="left">
                    <embed type="image/svg+xml" src={{line_chart|safe}} style='max-width:1000px'/>
            </div>
    </div>  

但这是 pdf 的外观图表不可见 在此处输入图像描述

我应该怎么做才能使图表可见?

标签: pythonhtmlflaskpdfkit

解决方案


推荐阅读