Page Number on print template

@Dany_Carvalheiro this is not an easy problem. Some developers have used work-arounds.

We are hoping to merge this soon: Enchancement - provide header/footer HTML code and adjust margins on the fly in custom print formats by gabtzi · Pull Request #1528 · frappe/frappe · GitHub

You can try and use it.

1 Like

I’m using variable on my custom print format.
First you have to set how many items to show in single page (in my case it’s 10 items)

On top of html page, put this:

{%- set n = 10 -%} <!-- number of items in one page -->
{%- set page = 0 -%} <!-- this is page counter -->

Then put this on the item table loop:

 {%- if (doc.items|length <= n) -%} 
        {%- set page_size = 1 -%}
        
        {%- elif ((doc.items|length > n) and (doc.items|length <= (n*2))) -%} 
        {%- set page_size = 2 -%}
        {%- elif ((doc.items|length % n == 0)) -%} 
        {%- set page_size = doc.items|length // n -%}
        {% else %}
         {%- set page_size = (doc.items|length // n)+1 -%}
        {%- endif -%} 
   
         {%- for i in range(0,page_size * n) -%}
                    
        {%- set row = doc.items[i] -%}
        
        {% if row != null -%}
        
        <tr>

            <td style="width: 5%;">{{ row.idx }}</td>
            <td style="width: 70%;">{{ row.description or '' }}       
             </td>
            <td style="width: 10%;text-align: right;">{{ row.get_formatted("qty") or '' }} </td>
             <td style="width: 15%;">{{ row.stock_uom }}  </td>
            
        </tr>
        {% else %}
		<tr>
                        <td> &nbsp; </td>
                        <td> &nbsp; </td>
                        <td> {{ _(" ") }} </td>
                        <td> {{ _(" ") }} </td>
                       
                </tr>
                {%- endif %}

<!-- page-break here -->
       {% if ((i+1) >= n) and ((i+1) % n == 0) and ((i+1) < doc.items|length) -%}
       {%- set page = page + 1 -%} 
       </table>
<div>Page {{ page }} of {{ page_size }}</div>

<div class="page-break"></div>
1 Like

Thank very much, trying it

ok thank you

sorry, but there are missing end’s, where do i put the {%endfor%}, and the last {%endif%}?

on the bottom

having dificulties, do i put it inside of the table or inside of the table but on the loop, because not seem working

down here is my table code:

    {% for row in doc.items -%}



        <tr height="0">              
        <td style="width: 5%;">{{ row.item_code }}</td>
        <td style="width: 40%;">
           {% if row.in_format_data("image") and row.get("image") -%}
             <div class="pull-left" style="max-width: 20%; margin-right: 10px;">
                <img src="{{ row.image }}" style="max-width: 100%">
             </div>
          {%- endif %}

             
              {{ row.description or '' }}  </td>
             <td style="width: 10%; text-align: right;">{{ row.qty }}</td>
             <td style="width: 10%; text-align: right;">{{ row.uom or row.stock_uom }}</td>
             <td style="width: 10%; text-align: right;">{{row.prazo}}</td>              


             <td style="width: 15%; text-align: right;">
           
                {{ row.get_formatted("rate", doc) or ''}}
             </td>
        
             {%- for row in doc.taxes -%}
              {%- if not row.included_in_print_rate -%}
               {%- if row.tax_amount -%}   
          <td style="width: 5%; text-align: right;">{{row.rate}}</td>
               {%- endif -%}
              {%- endif -%}
            {%- endfor -%}
          <td style="width: 20%; text-align: right;">{{
            row.get_formatted("amount", doc) or ''}}</td>
{%- endfor -%}
Artigo Descrição Quant. Un Prazo de entrega Pr. Unitario IVA Valor

having dificulties, do i put it inside of the table or inside of the table but on the loop, because not seem working

down here is my table code:

can you sent the code to my email, i think, that some code are being cut.
dany_cat5@hotmail.com

    {%- set n = 10 -%} 
    {%- set page = 0 -%}
         
    <!-- Items table header -->
    <table>
            <tr>
                <th>No</th>
               .......
            </tr>
    
            {%- if (doc.items|length <= n) -%} 
            {%- set page_size = 1 -%}
            
            {%- elif ((doc.items|length > n) and (doc.items|length <= (n*2))) -%} 
            {%- set page_size = 2 -%}
            {%- elif ((doc.items|length % n == 0)) -%} 
            {%- set page_size = doc.items|length // n -%}
            {% else %}
             {%- set page_size = (doc.items|length // n)+1 -%}
            {%- endif -%} 
       
             {%- for i in range(0,page_size * n) -%}
                        
            {%- set row = doc.items[i] -%}
            
            {% if row != null -%}
            
           <!-- item row -->
            <tr>
                <td>......</td>
               ..............
            </tr>
            {% else %}
    		<tr>
                            <td> &nbsp; </td>
                            .......
                    </tr>
            {%- endif %}
    
<!-- page-break here -->
{% if ((i+1) >= n) and ((i+1) % n == 0) and ((i+1) < doc.items|length) -%}
     {%- set page = page + 1 -%}    
</table>
<div class="page-break"></div>     
    
<!-- Items table header -->
<table>
    	<tr>
                <th>No</th>
               .......
        </tr>
    
{%- endif %}
    
{%- endfor -%}
</table>
1 Like

Thank you, working

Hi,

I added the script into my print format although it did make page numbers. Also loved the part where is automatically breaks the page after a number of items, although i couldn’t get it working properly

I couldn’t get the whole code to display here so I used pastebin, http://pastebin.com/rT2XpACp

this is my output could you tell me what I have done wrong?

Thank you
Bibin

@bibinqcs what is your requirement for print format?

In latest version of erpnext we can add page number and footer, you can use new code to add page-break instead of dividing 10 item per page.

1 Like

@kolate_sambhaji I just tried using 10 Items as the custom script had 10, I thought I will get the code working first and modify it. I know about the footer option and also in PDF the page number comes in the footer, although it is missing in print.

The print format I need is for a Dot matrix printer, to print on a 8.5x 5.5in page so I thought if it is possible automatically get a page break after 5 items. That would be the better than manually having to select page break after 5 items in the document.

Also for some reason Page number doesn’t not come at all now, Before it used to come in Standard Print Format and Print Format Builder. I am guessing some work is going on so I thought if this script actually works two birds one stone.
I did try the formats that you had on your GitHub Repository and you had helped me earlier with script but even that didn’t give me results. I am pretty sure that I did something wrong.
Here is the link of the thread you helped me before

2 Likes

@bibinqcs The code you are referring to previous post is old and not complete.
We have better option available today.

To add header footer in our custom print format with page number

{%- from "templates/print_formats/standard_macros.html" import add_header,
     render_field -%}
    <div id="header-html" class="hidden-pdf">
                 {{ add_header(0,1,doc,letter_head, no_letterhead) }}
      </div>
    <div id="footer-html" class="visible-pdf">
        {% if not no_letterhead and footer %}
        <div class="letter-head-footer">
            {{ footer }}
        </div>
        {% endif %}
        <p class="text-center small page-number visible-pdf">
            {{ _("Page {0} of {1}").format('<span class="page"></span>', '<span class="topage"></span>') }}
        </p>
    </div>

For page size in print format

<style>
    .print-format table, .print-format tr, 
    .print-format td, .print-format div, .print-format p {
        font-family: Monospace;
        line-height: 100%;
        vertical-align: middle;
        padding: 2px !important;
    }
    .print-preview {
        max-width: 8.5in;
    }
    @media screen, print {
        .print-format {
            width: 8.5in;
            padding: 0.25in;
            min-height: 5.5in;
        }
    }
</style>
2 Likes

Awesome, Thanks Let me try this out and will post the results.

Thanks once again :smiley:

1 Like

@kolate_sambhaji
Sadly no page numbers still, Although it did help with another problem I had with padding.
Here is the results from using the code full code here http://pastebin.com/isZRBHvq

Uploading Screen Shot 2016-03-30 at 2.46.36 PM.png…

@bibinqcs which version of ERPNext you are using?

@kolate_sambhaji

ERPNext: v6.27.4

Frappe Framework: v6.27.6

running on a Centos server

Can you update screen shot again?