Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Proper working pagination

Tomasz Szymanskie3e9baef912b
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index f5446155cd..b49a6abbd5 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -353,6 +353,13 @@ def generate_invoice_pdf_for_order(invoice):
limit = i + 13
rest_of_products.append(all_products[_product_limit:][i:limit])
+ try:
+ page_count = (
+ 1 + len(rest_of_products) + (1 if len(rest_of_products[-1]) > 11 else 0)
+ )
+ except IndexError:
+ page_count = 1
+
rendered_template = get_template("invoice.html").render(
{
"invoice": invoice,
@@ -360,6 +367,7 @@ def generate_invoice_pdf_for_order(invoice):
"logo_path": f"file://{logo_path}",
"products_first_page": products_first_page,
"rest_of_products": rest_of_products,
+ "page_count": page_count,
}
)
content_file = ContentFile(HTML(string=rendered_template).write_pdf())
diff --git a/templates/invoice.html b/templates/invoice.html
index 9f0700b419..932b407fb1 100644
--- a/templates/invoice.html
+++ b/templates/invoice.html
@@ -8,9 +8,6 @@
@page {
background-color: #FAFAFA;
margin: 0.5cm;
- @bottom-right {
- content: counter(page) " / " counter(pages);
- }
}
body {
@@ -282,28 +279,36 @@
<span class="header-title">{% trans "ITEMS ORDERED" context "Invoice PDF table header" %}</span>
<br />
{% include "invoice_products_table.html" with products=products_first_page %}
+ <div class="page-footer">
+ <span class="page-counter header-category">
+ 1 / {{ page_count }}
+ </span>
+ </div>
{% if not rest_of_products|length %}
{% if products_first_page|length == 4 %}
<div class="page-break"></div>
+ <div class="page-footer">
+ <span class="page-counter header-category">2 / 2</span>
+ </div>
{% endif %}
{% include "invoice_summary_table.html" with order=order %}
- <div class="page-footer">
- <span class="page-counter header-category">1 / 9</span>
- </div>
{% else %}
{% for product_set in rest_of_products %}
<div class="page-break"></div>
+ <div class="page-footer">
+ <span class="page-counter header-category">{{ forloop.counter|add:1 }} / {{ page_count }}</span>
+ </div>
{% include "invoice_products_table.html" with products=product_set %}
{% if forloop.last %}
{% if product_set|length > 11 %}
<div class="page-break"></div>
+ <div class="page-footer">
+ <span class="page-counter header-category">{{ forloop.counter|add:2 }} / {{ page_count }}</span>
+ </div>
{% endif %}
{% include "invoice_summary_table.html" with order=order %}
{% endif %}
{% endfor %}
- <div class="page-footer">
- <span class="page-counter header-category">1 / 9</span>
- </div>
{% endif %}
</div>
</body>