首页 > 解决方案 > Django PDFTemplateView (easy_pdf) tidying up a report with page breaks

问题描述

I saw where I can Have a page break in a easy pdf html template saying

<p class="pb"></p> 

That is great, but I am trying to run a report where it page breaks and puts a table header every so many lines, with special consideration for the first page. Is there anyway to do this?

This is the view I am calling my html from:

class InvoiceReportIDPDF(PDFTemplateView):
  #template_name = 'ipaswdb/provider/report/provider_profile.html'
  template_name = 'ipaswdb/invoice/report/invoice_report_pdf.html'


  def get_context_data(self, **kwargs):
    context = super(InvoiceReportIDPDF, self).get_context_data(pagesize='LetterSize',title='Invoice',**kwargs)
    #print "provider id: ", kwargs['pk']
    self.download_filename = 'invoice-report-' + kwargs['pk'] + '.pdf'

    res = build_invoice_printout_models(kwargs['pk'])
    #res = build_invoice_printout_models('all')

    return context

So I can print this invoice but the line items just run from one page to the next with no nice header at the top of the next page (for when same invoice)...

I can't use variables in the html page to count using the template and I thought Id get slick and when I build the items I send to the webpage to put in a field for a page break.

So when I build my object I send to the view context I have like:

invoiceItem['pagebreak'] = None

if lineCount > xmany:
  invoiceItem['pagebreak'] = 'YES'

Then in the html:

  {% if invoice.items %}
    {% for item in invoice.items %}
          <tr>
        <td> {{ item.name}} - {{ item.provspecialty }} </td>
        <td> Current Dues </td>
        <td> </td>
        <td> ${{ item.dues }} </td>
      </tr>


      <tr>
        <td></td>
        <td> {{ item.desc }} </td>
        <td> ${{ item.adjustments}}</td>
        <td></td>
      </tr> 
      {% if item.pagebreak %}   
         <p class="pb"> </p> <!-- this didn't work right at all -->
        <!-- just sent a bunch of blank lines then 5 pages down tried to work--> 
      {% endif %}
    {% endfor %}
  {% endif %}

I wish it just 'knew' where the bottom of the page was to make the break and add a new header...

标签: djangopdfhtml2pdf

解决方案


推荐阅读