标题框上的图像仅显示在xhtml2pd生成的pdf的第一页中

2024-09-27 00:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python2.7和Flask开发一个web应用程序。我使用xhtml2pdf生成一个基于Jinja2模板的pdf。 我想把一个图像放在模板的页眉框上,但它只显示在第一页上。 我试着用文本来做,结果很管用,页眉显示在所有页面上。在

以下是视图代码:

@report.route('/commissionist/<period>/<commissionist_id>')
@login_required
def commissionist_report(period, commissionist_id):
    period_d = periods.Period(datetime.fromtimestamp(mktime(strptime(period, '%m-%Y'))))
    carga = load_model.get_commissionist_load(period_d, commissionist_id)

    resp = create_pdf(render_template('report/report.html', commissionist_load=carga, period=period, period_datetime=period_d.to_datetime()))
    response = make_response(resp)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'inline; filename=Informe '+carga.commissionist.name+'.pdf'

    return response

以下是create_pdf函数:

^{pr2}$

以下是模板(重要部分):

<!DOCTYPE html>
<html>
    <head>
        <title>Informe</title>
        {% block extra_css %}
            <style>
                @page {
                    size: letter portrait;
                    @frame header_frame {           /* Static Frame */
                    -pdf-frame-content: header_content;
                    left: 50pt; width: 512pt; top: 50pt; height: 50pt;
                    }
                    @frame content_frame {          /* Content Frame */
                    left: 50pt; width: 512pt; top: 110pt; height: 632pt;
                    }
                    @frame footer_frame {           /* Another static Frame */
                    -pdf-frame-content: footer_content;
                    left: 50pt; width: 512pt; top: 772pt; height: 20pt;
                    }
                }
            </style>
        {% endblock %}
    </head>
    <body>
    {% block body %}
        <div id="header_content">
            <img src="path\to\file.png" width="25%">
        </div>
        <div id="footer_content">Página <pdf:pagenumber/>
            de <pdf:pagecount/>
        </div>

        <!--The rest of the html-->

    {% endblock %}
    </body>
</html>

希望你能帮助我。谢谢。在


Tags: reportdiv模板iddatetimepdfresponsehtml

热门问题