简体   繁体   English

header_frame 上的图像仅显示在 xhtml2pdf 生成的 pdf 的第一页中

[英]Image on header_frame displaying only in the first page of a pdf generated by xhtml2pdf

I'm developing a web app using python 2.7 with Flask.我正在使用带有 Flask 的 python 2.7 开发网络应用程序。 I'm using xhtml2pdf to generate a pdf based on a template with Jinja2.我正在使用 xhtml2pdf 基于 Jinja2 的模板生成 pdf。 I want to put an image on the header_frame of the template, but it's only showing on the first page.我想在模板的 header_frame 上放一张图片,但它只显示在第一页上。 I tried doing it with text, and it works, the header is shown on all the pages.我尝试用文本来做,它有效,标题显示在所有页面上。

Here's the view code:这是视图代码:

@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

Here's the create_pdf function:这是 create_pdf 函数:

def create_pdf(pdf_data):
    pdf = StringIO()
    pisa.CreatePDF(StringIO(pdf_data), pdf)
    resp = pdf.getvalue()
    pdf.close()
    return resp

And here's the template (the important part):这是模板(重要的部分):

<!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>

Hope you can help me.希望您能够帮助我。 Thanks.谢谢。

I faced the same trouble at this week, we had some updates on python and xhtml2pdf but after 2 hours trying I solved it out.本周我遇到了同样的问题,我们对pythonxhtml2pdf进行了一些更新,但经过 2 个小时的尝试,我解决了。

I'm using xhtml2pdf => 0.2.4 and Python => 3.7我正在使用xhtml2pdf => 0.2.4Python => 3.7

The solution:解决方案:

I created one generic method to create pdf such as you:我创建了一种通用方法来创建 pdf,例如您:

from xhtml2pdf import pisa
from io import StringIO

...

def pdf_converter(self,html):

    pisaStatus = pisa.CreatePDF(StringIO(html))
    return pisaStatus.dest

Then on view:然后查看:

@my_bp.route('/...my_root...')
@roles_required('admin')
def export_pdf():
    from xxxx.utils.export_pdf import ExportPDF
    a = ExportPDF()
    pdf = a.pdf_converter(...my_method_that_creates_html...)

    pdf.seek(0)

    return send_file(pdf, attachment_filename="Report_xxxx.pdf", as_attachment=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM