Page Number using Macro

Hi Everyone,

I used custom print format using jinja and CSS, I add the

{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead) -%}
	{% if letter_head and not no_letterhead %}
	<div class="letter-head">{{ letter_head }}</div>
	{% endif %}
    {% if doc.print_heading_template %}
        {{ frappe.render_template(doc.print_heading_template, {"doc":doc}) }}
    {% else %}
    <div class="print-heading">
		<h2>{{ doc.select_print_heading or (doc.print_heading if doc.print_heading != None
			else _(doc.doctype)) }}<br>
			<small>{{ doc.sub_heading if doc.sub_heading != None
				else doc.name }}</small>
        </h2>
    </div>
    {% endif %}
	{%- if doc.meta.is_submittable and doc.docstatus==0-%}
	<div class="text-center" document-status="draft">
		<h4 style="margin: 0px;">{{ _("DRAFT") }}</h4>
    </div>
	{%- endif -%}
	{%- if doc.meta.is_submittable and doc.docstatus==2-%}
	<div class="text-center" document-status="cancelled">
		<h4 style="margin: 0px;">{{ _("CANCELLED") }}</h4>
    </div>
	{%- endif -%}
	{% if max_pages > 1 %}
	<p class="text-right page-number">{{ _("Page #{0} of {1}").format(page_num, max_pages) }}</p>
	{% endif %}
{%- endmacro -%}

{{ add_header(0,1,doc,letter_head, no_letterhead) }}

But for some odd reason Page number does not seem to come,
I get the page header and Draft a the document is in the Draft Stage

Any tips, maybe I am doing something wrong?

Print max_pages and see if you get more than one.

I played around bit more it doesnt seem to detect “max_pages” tag

At present I have put in this

{% set total_pages = [1] %}
<div> Page No  {{ total_pages|length }} </div>

This will display it as

Page No 1

It shows the Page No. but I would like to display the total number of pages so that It easier understand the total number of pages that are int the print

anything I can add to get

“Page # 1 of 2”

Hi @kolate_sambhaji
First off, Thanks for the code you shared thru Github
I used the script you posted there, It doesnt seem to detect the page info, as in it shows the title and every thing but no Page number

Some help would be nice

Thanks

1 Like

@bibinqcs
Its standard macro, so it contain page number,

{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead) -%}
.
.
.
{%- endmacro -%}

I am calling macro using below code, we are passing page number 0 and max_pages 1 (hard coded value), that’s why its not printing page number value.

{{ add_header(0,1,doc,letter_head, no_letterhead) }}

correct way is to add letter head in page break, if you ready to work and share, I will give you sample working code

@bibinqcs here it is sample code and sample output.

{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead) -%}
    {% if letter_head and not no_letterhead %}
    <div class="letter-head">{{ letter_head }}</div>
    {% endif %}
    {%- if doc.meta.is_submittable and doc.docstatus==0-%}
    <div class="alert alert-info text-center">
        <h4 style="margin: 0px;">{{ _("DRAFT") }}</h4></div>
    {%- endif -%}
    {%- if doc.meta.is_submittable and doc.docstatus==2-%}
    <div class="alert alert-danger text-center">
        <h4 style="margin: 0px;">{{ _("CANCELLED") }}</h4></div>
    {%- endif -%}
    {% if max_pages > 1 %}
    <p class="text-right">{{ _("Page #{0} of {1}").format(page_num, max_pages) }}</p>
    {% endif %}
{%- endmacro -%}

{%- from "templates/print_formats/standard_macros.html" import add_header2, add_header,
         render_field, add_foot -%}

{% for page in layout %}
Html code
<div class="page-break">
{{ add_header(loop.index,layout|len,doc,letter_head, no_letterhead) }}
</div>
{% endfor %}

Thanks for the script :smiley:

I am just trying the code.
I share the entire code here so you where I am going wrong

@kolate_sambhaji
I think I am doing something wrong, I am sharing my code
I am getting the header and page number now at the page break for both pages.

<style>
	.print-format table, .print-format tr, 
	.print-format td, .print-format div, .print-format p {
		font-family: Monospace;
                font-size: 6;
		vertical-align: top;
                }
	@media screen {
		.print-format {
			width: 8.5in;
			padding: 0.05in;

			min-height: 5.5in;
		}
	}
</style>
{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead) -%}
    {% if letter_head and not no_letterhead %}
    <div class="letter-head">{{ letter_head }}</div>
    {% endif %}
    {%- if doc.meta.is_submittable and doc.docstatus==0-%}
    <div class="alert alert-info text-center">
        <h4 style="margin: 0px;">{{ _("DRAFT") }}</h4></div>
    {%- endif -%}
    {%- if doc.meta.is_submittable and doc.docstatus==2-%}
    <div class="alert alert-danger text-center">
        <h4 style="margin: 0px;">{{ _("CANCELLED") }}</h4></div>
    {%- endif -%}
    {% if max_pages > 1 %}
    <p class="text-right">{{ _("Page #{0} of {1}").format(page_num, max_pages) }}</p>
    {% endif %}
{%- endmacro -%}
{%- from "templates/print_formats/standard_macros.html" import add_header2, add_header,
         render_field, add_foot -%}
{{ add_header(0,1,doc,letter_head, no_letterhead) }}
<br>
<br>
<br>
<div class="row">
        <div class="col-xs-12">
                <div><h3 style="text-align: center;"><b>DELIVERY NOTE</b></h3></div>
            
        </div>
        
    </div>
<div class="row">
        
        <div class="col-xs-9">
            
                <div><div>
<b>Customer:&nbsp;</b>{{ doc.customer_name }}
</div></div>
            
                <div>Phone:{{ doc.address_display.split("Phone:")[1] }}</div>
            
        </div>
        
        <div class="col-xs-3">
            
                <div><b>D/N No.:&nbsp;</b>{{doc.name}}</div>
                <div><b>D/N Date:&nbsp;</b>{{ doc.get_formatted("lr_date")}}</div>
            {%- if doc.po_no -%}
                <div><b>LPO No.:&nbsp;</b>{{ doc.po_no or '' }}</div>
            {%- endif -%}          
        </div>
    </div>
<table class="table table-bordered">
	<tbody>
		<tr>
			<th class="text-center">Sr</th>
			<th class="text-center">Description</th>
			<th class="text-center">Qty</th>
			<th class="text-center">Rate</th>
			<th class="text-center">Amount</th>
		</tr>
		{%- for row in doc.items -%}
		<tr>
			<td style="width: 3%; text-align: center; border-top-style: none;
    border-right-style: solid;
    border-bottom-style: none;
    border-left-style: solid;">{{ row.idx }}</td>
			<td style="width: 51%; border-top-style: none;
    border-right-style: solid;
    border-bottom-style: none;
    border-left-style: solid;">
				<div style="border: 0px;">{{ row.description }}</div></td>
			<td style="width: 12%; text-align: right; border-top-style: none;
    border-right-style: solid;
    border-bottom-style: none;
    border-left-style: solid;">{{ row.qty }} {{ row.uom or row.stock_uom }}</td>
			<td style="width: 15%; text-align: right; border-top-style: none;
    border-right-style: solid;
    border-bottom-style: none;
    border-left-style: solid;">{{
				row.get_formatted("rate", doc) }}</td>
			<td style="width: 19%; text-align: right; border-top-style: none;
    border-right-style: solid;
    border-bottom-style: none;
    border-left-style: solid;">{{
				row.get_formatted("amount", doc) }}</td>
		</tr>
                {% if row.page_break -%}
            </tbody>
            </table>
        {% for page in layout %}
        <div class="page-break">
        {{ add_header(loop.index,layout|len,doc,letter_head, no_letterhead) }}
        </div>
        {% endfor %}
             <br>
             <br>
             <br>
             <div class="row">
        
        <div class="col-xs-12">
            
                <div><h3 style="text-align: center;"><b>DELIVERY NOTE</b></h3></div>
            
        </div>
        
    </div>
<div class="row">
        
        <div class="col-xs-9">
            <div><b>D/N No.:&nbsp;</b>{{doc.name}}</div>
            </div>
        <div class="col-xs-3">
                <div><b>D/N Date:&nbsp;</b>{{ doc.get_formatted("lr_date")}}</div>
        </div>
    </div>
             <table class="table table-condensed table-hover table-bordered">
		<tr>
		    <th class="text-center">Sr</th>
		    <th class="text-center">Description</th>
		    <th class="text-center">Qty</th>
		    <th class="text-center">Rate</th>
		    <th class="text-center">Amount</th>
		</tr>
                {%- endif %}
                {%- endfor -%}
	</tbody>
</table>



<div class="row">
        <div class="col-xs-9">
            <div class="row">
			<div class="col-xs-2 text-left">
				<label>In Words</label>
			</div>
			<div class="col-xs-10 text-left">
		{{ doc.get_formatted("base_in_words") }}
	                </div>
		</div>
        </div>
        <div class="col-xs-3">
            <div class="row">
			<div class="col-xs-2 text-right">
				<label>Total</label>
			</div>
			<div class="col-xs-10 text-right">
		{{ doc.get_formatted("total") }}
	                </div>
		</div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
               <div class="row">
	<div class="col-xs-6"></div>
	<div class="col-xs-6"></div>
</div>
        </div>
    </div>
<div class="row">
        <div class="col-xs-6">
                <div><div>
<br>
<b>Received By:</b>
    <br>
    <br>(Name and Signature)
    <br>
</div></div>
        </div>
        <div class="col-xs-6">
                <div><br>
<div style="text-align: right;">For&nbsp;<b>The Company</b>
</div></div>
        </div>
    </div>
</div>
            </div>
	</div>

Is there anything else that I can do to get Page Numbers on the print. At present I am using custom HTML in the print format builder, This seems to generate page numbers, but I have limitations in customizing the print format. As I want to reduce the cell padding and other modifications to CSS governing the print format

Thanks for the help you guys have been giving

1 Like

Hi,
Have you got the solutions yet ? I’m looking for the similar things.

FROM Standard Macros, it will default showing the Doc Type and Doc Name.
Any way to remove it ?

You could try removing the doc.doctype and doc.name lines within the print-heading class

{% if doc.print_heading_template %}
{{ frappe.render_template(doc.print_heading_template, {“doc”:doc}) }}
{% else %}
<div class=“print-heading”>

</div>
{% endif %}

Hello,

Does it work for pdfs or print format?
i want page nos in footer in print not in pdf

can you help?