簡體   English   中英

header_frame 上的圖像僅顯示在 xhtml2pdf 生成的 pdf 的第一頁中

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

我正在使用帶有 Flask 的 python 2.7 開發網絡應用程序。 我正在使用 xhtml2pdf 基於 Jinja2 的模板生成 pdf。 我想在模板的 header_frame 上放一張圖片,但它只顯示在第一頁上。 我嘗試用文本來做,它有效,標題顯示在所有頁面上。

這是視圖代碼:

@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 函數:

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

這是模板(重要的部分):

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

希望您能夠幫助我。 謝謝。

本周我遇到了同樣的問題,我們對pythonxhtml2pdf進行了一些更新,但經過 2 個小時的嘗試,我解決了。

我正在使用xhtml2pdf => 0.2.4Python => 3.7

解決方案:

我創建了一種通用方法來創建 pdf,例如您:

from xhtml2pdf import pisa
from io import StringIO

...

def pdf_converter(self,html):

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

然后查看:

@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