使用Pisa打印页码

2 投票
2 回答
4019 浏览
提问于 2025-04-17 05:34

我在我的Django项目中使用pisa,目的是把HTML生成PDF,用于报告功能。我的一些报告变得相当大,我注意到上面没有页码。有没有人知道我该怎么做才能在打印的PDF报告上显示页码?

补充说明

这是我生成PDF的方式

if request.POST.has_key('print_report_submit'):
            context['show_report'] = True
            context['mb_logo'] = os.path.join(os.path.dirname(__file__), "../../../media/images/mb_logo.jpg")
            html = render_to_string('reports/fam_maturity_print.html', RequestContext(request,context))
            result = StringIO.StringIO()
            pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
            response = HttpResponse(result.getvalue(), mimetype='application/pdf')
            response['Content-Disposition'] = 'attachment; filename=family_maturity-statement.pdf'
            return response

HTML代码

{% if show_report %}

<table width="100%">

    <tr>
        <td colspan="2" align="center" width="400px">
            <h1 class="title">Family Maturities by Year</h1>
            <br/><br/>
        </td>
    </tr>
    <tr>
        <td width="500px">
            <img src="{{ mb_logo }}"/>
        <td>                            <p>
              <img style="max-height: 60px; max-width:60px;" src="{{ check_image }}"/>
          </p>
    </tr>
</table>
    <br/>
    <br/>



   <hr class="report_hr1"/>
   <h2 class="state_head" style="font-size: 16px;">Statement Information As of {{ statement_date|date:"F d, Y" }}</h2>
<hr class="report_hr1"/>

    <table>

    {% for plan_dict in plan_list %}
        <hr class="report_hr2"/>
        <table>
        <tr>
            <td><strong>Plan Holder: </strong></td><td>{{ plan_dict.plan.get_primary_owner.member.client}}</td>
            <td><strong>Joint Owner(s): </strong></td>
            <td>
                {% for member in plan_dict.plan.planmember_set.all %}
                    {% ifnotequal member.ownership_type.code "primary" %}
                        {{ member }}
                    {% endifnotequal %}
                {% endfor %}
            </td>
        </tr>
        <tr>
            <td><strong>Plan ID: </strong></td><td>{{ plan_dict.plan.id }}</td>
        </tr>
        <tr>
            <td><strong>Plan type: </strong></td><td>{{ plan_dict.plan.plan_type }}</td>
        </tr>
        </table>
        <hr class="report_hr2"/>
        {#      This is the table for the current year's maturities      #}
        {% if plan_dict.investment %}
        <table>
            <td colspan="2"><h3>{{ cur_year }}</h3></td>
            <tr>
                <th>Financial Institution</th>
                <th>Type</th>
                <th>Principal</th>
                <th>Maturity</th>
            </tr>
            {% for i in plan_dict.investment %}
            <tr>
                <td>{{ i.financial_institution.abbr }}</td>
                <td>{{ i.product.code }}</td>
                <td>${{ i.amount|intcomma }}</td>
                <td>${{ i.maturity_amount|intcomma }}</td>
            </tr>
            {% endfor %}
            <tr>
                    <td><strong>Total for {{ cur_year }}</strong></td>
                    <td></td>
                    <td><strong>${{ plan_dict.total|intcomma }}</strong></td>
                    <td><strong>${{ plan_dict.total_m|intcomma }}</strong></td>
                </tr>
        </table>
        {% endif %}
        <div id="holding_table" align="center">
               <h3>Holding Totals with Each Company</h3>
               <table>
                    <tr>
                        <td style="font-weight: bold;">Financial Institution</td>
                        <td style="font-weight: bold;">Total</td>
                    </tr>
               {% for l in plan_dict.total_list %}
                   {% if l.maturity_amount__sum != None %}
                       <tr>
                        <td>{{ l.financial_institution__abbr }}</td>
                        <td>${{ l.maturity_amount__sum|intcomma }}</td>
                    </tr>
                   {% endif %}
               {% endfor %}
                    <tr>
                        <td style="font-weight: bold;">Total Non-Fund Holdings for this plan</td>
                        <td style="font-weight: bold;">${{ plan_dict.grand|intcomma }}</td>
                    </tr>
               </table>
                <span class="disclaimer_span">This report has been prepared from data believed to be reliable but no
                        representation is made as to accuracy or completeness.</span>
        </div>
        <br/>
        <br/>

    {% endfor %}
    </table>
{% endif %}

任何帮助都将非常感谢。

2 个回答

0

这个对我来说是有效的,你需要使用这个,它是针对A3大小的。

@page{
        size: A3;
        margin: 1cm;

      @frame footer_frame {           /* Another static Frame */
        -pdf-frame-content: footer_content;
        left: 400pt; top: 1170pt; height: 20pt;
        
    }
    }

你需要把它放在上面的表格里。

<div id="footer_content"> Page <pdf:pagenumber>
of <pdf:pagecount> </div>
1

在这个链接中提到的问题是:

http://www.arnebrodowski.de/blog/501-Pisa-and-Reportlab-pitfalls.html

我遇到的一个问题是,想在我的页面上添加一个静态的页脚,这个页脚里有一个 <pdf:pagenumber /> 标签,应该在生成的PDF每一页的底部显示当前的页码。问题是,每一页都只显示数字0。解决方法其实很简单,但我只有在研究了pisa的源代码后才明白:你只能在段落中使用这个标签,而不能像我那样放在表格里面。即使是表格里的段落也不行。它必须是一个顶层的段落。

编辑

试试这个:

<div>
   <pdf:pagenumber />
</div>

撰写回答