ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

Add page break to print format while generating PDF


#21

Dear @lasalesi
thanks for your support
it’s a very good idea to add total for every item group but actually, I need to add something like page break after every item group may be like page break option in item table page

Thanks in advance


#22

In that case, simply close the table, add the page break and then on the new page start a new table…

     {% for n in doc.items %}
       <table  style="width: 100%; float: left;" border="1">
         <tr>
           <th style="width: 30%;">Product</th>
           <th style="width: 20%;">Module</th>
           <th style="width: 20%;">QTY</th>
           <th style="width: 20%;">VIS.</th>
           <th style="width: 10%;">Brand</th>
        </tr>
        <!-- add group items -->
        {% if n.item_group == key %}
          <tr>
          (...)
            <td>{{ value }}</td>
          </tr>	 
       </table>
       <div class="page-break"></div>
       {% endfor %}

#23

Dear @lasalesi
i try your suggestion but not working fine please find my code below

<!-- positions -->
<p></p>
 <table  style="width: 100%; float: left;" border="1">
 <tr>
    <th style="width: 30%;">Product</th>
    <th style="width: 20%;">Module</th>
    <th style="width: 20%;">QTY</th>
    <th style="width: 20%;">VIS.</th>
    <th style="width: 10%;">Brand</th>
 </tr>
 <!-- prepare groups -->
 {% set group_total = {} %} 
 {% for n in doc.items %}
   {% if group_total[n.item_group] is defined %}
      {% if group_total.update({n.item_group: group_total[n.item_group] + n.amount}) %}{% endif %}
   {% else %}
      {% if group_total.update({n.item_group: n.amount}) %}{% endif %}
   {% endif %}
 {% endfor %}
 {% for key, value in group_total.iteritems() %}
     {% for n in doc.items %}
	    <!-- add group items -->
        {% if n.item_group == key %}
          <tr>
            <td>{{ n.item_name}}</td>
            <td>{{ n.get_formatted('item_group') }}</td>
            <td>{{ n.qty }} {{ n.stock_uom }}</td>
            <td><img src="{{ n.image }}" style="width: 60%"></td>
            <td>{{ n.brand }}</td>
          </tr>
</table>
<div class="page-break"></div>
            {% endif %}
     {% endfor %}
	 <!-- total per group -->
     <tr>
        <td>Total</td>
        <td>{{ key }}</td>
        <td></td>
        <td></td>
        <td>{{ value }}</td>
     </tr>	 
 {% endfor %}	 
 </table>

<!-- summary and taxes -->
<p></p>
<table style="width: 100%; ">
<tr>
    <td style="width: 60%;"></td>
    <td style="width: 20%;">
	  Net Total<br />
	  {% for t in doc.taxes %}
	    {{ t.description }}<br />
	  {% endfor %}
	  {% if doc.discount_amount != 0 %}
	    Rabatt<br />
	  {% endif %}
	  <strong>Grand Total</strong>
    </td>
    <td style="width: 20%;">
	  {{ doc.get_formatted('total') }}<br />
	  {% for t in doc.taxes %}
	    {{ t.get_formatted('tax_amount') }}<br />
	  {% endfor %}	  
	  {% if doc.discount_amount != 0 %}
	    -{{ doc.get_formatted('discount_amount') }}<br />
	  {% endif %}
	  <strong>{{ doc.get_formatted('base_grand_total') }}</strong>
     </td>
</tr>
</table>

Controlling page-break
#24

Hi @Alaa_Badri,

your table definition has an extra </table> and the structure does not reflect what you want (I guess). Try this:

<!-- positions -->
<p></p>
 <!-- prepare groups -->
 {% set group_total = {} %} 
 {% for n in doc.items %}
   {% if group_total[n.item_group] is defined %}
      {% if group_total.update({n.item_group: group_total[n.item_group] + n.amount}) %}{% endif %}
   {% else %}
      {% if group_total.update({n.item_group: n.amount}) %}{% endif %}
   {% endif %}
 {% endfor %}
 {% for key, value in group_total.iteritems() %}
    <table  style="width: 100%; float: left;" border="1">
      <tr>
        <th style="width: 30%;">Product</th>
        <th style="width: 20%;">Module</th>
        <th style="width: 20%;">QTY</th>
        <th style="width: 20%;">VIS.</th>
        <th style="width: 10%;">Brand</th>
      </tr>
      {% for n in doc.items %}
      <!-- add group items -->
        {% if n.item_group == key %}
          <tr>
            <td>{{ n.item_name}}</td>
            <td>{{ n.get_formatted('item_group') }}</td>
            <td>{{ n.qty }} {{ n.stock_uom }}</td>
            <td><img src="{{ n.image }}" style="width: 60%"></td>
            <td>{{ n.brand }}</td>
          </tr>
        {% endif %}
      {% endfor %}
      <!-- total per group -->
      <tr>
        <td>Total</td>
        <td>{{ key }}</td>
        <td></td>
        <td></td>
        <td>{{ value }}</td>
      </tr>
   </table>
   <div class="page-break"></div>
 {% endfor %}

#25

Dear @lasalesi
thanks for your support

the above code is generate extra table but wihout page break between them


#26

Hi @Alaa_Badri,

you are indeed right. It reproduced on my system. And while I cannot give you a reason, this snippet will work :slight_smile: (note only one additional line that adds a Jinja-induced line break; if you do it in plain html it will not work - really don’t know why)

<!-- positions -->
<p></p>
 <!-- prepare groups -->
 {% set group_total = {} %} 
 {% for n in doc.items %}
   {% if group_total[n.item_group] is defined %}
      {% if group_total.update({n.item_group: group_total[n.item_group] + n.amount}) %}{% endif %}
   {% else %}
      {% if group_total.update({n.item_group: n.amount}) %}{% endif %}
   {% endif %}
 {% endfor %}
 {% for key, value in group_total.iteritems() %}
    <table  style="width: 100%; float: left;" border="1">
      <tr>
        <th style="width: 30%;">Product</th>
        <th style="width: 20%;">Module</th>
        <th style="width: 20%;">QTY</th>
        <th style="width: 20%;">VIS.</th>
        <th style="width: 10%;">Brand</th>
      </tr>
      {% for n in doc.items %}
      <!-- add group items -->
        {% if n.item_group == key %}
          <tr>
            <td>{{ n.item_name}}</td>
            <td>{{ n.get_formatted('item_group') }}</td>
            <td>{{ n.qty }} {{ n.stock_uom }}</td>
            <td><img src="{{ n.image }}" style="width: 60%"></td>
            <td>{{ n.brand }}</td>
          </tr>
        {% endif %}
      {% endfor %}
      <!-- total per group -->
      <tr>
        <td>Total</td>
        <td>{{ key }}</td>
        <td></td>
        <td></td>
        <td>{{ value }}</td>
      </tr>
   </table>
   <p>{{ '<br>' }}</p>
   <div class="page-break"></div>
 {% endfor %}

#27

Dear @lasalesi
I very appreciate your effort and support your code above it working well :grinning::grinning:
now I try to add a paragraph after every table related to specific item group using below code but I think there is something wrong it’s not working

    {% if item_group == 'Security' %}
              <h3>| Security</h3>
    {% else %}
    <h3>| Not Security </h3>

            {% endif %}

#28

Note that the item group is an attribute of the loop instance n…