Skip to content

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

Template and utils func to create an invoice PDF

Tomasz Szymanski1fa1054cfbae
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index c050cce68e..f0476b967e 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -1,7 +1,9 @@
from functools import wraps
from django.conf import settings
+from django.contrib.staticfiles import finders as static_finders
from django.db import transaction
+from django.template.loader import get_template
from django.utils import timezone
from prices import Money, TaxedMoney
@@ -338,3 +340,20 @@ def get_voucher_discount_for_order(order: Order) -> Money:
def match_orders_with_new_user(user: User) -> None:
Order.objects.confirmed().filter(user_email=user.email, user=None).update(user=user)
+
+
+def _create_pdf(rendered_template, path):
+ from weasyprint import HTML
+
+ pdf_file = HTML(string=rendered_template).write_pdf(path)
+ return pdf_file
+
+
+def generate_invoice_pdf_for_order(invoice):
+ logo_path = static_finders.find("images/logo-light.svg")
+ rendered_template = get_template("invoice.html").render(
+ {"invoice": invoice, "order": invoice.order, "logo_path": f"file://{logo_path}"}
+ )
+ pdf_path = f"/tmp/{invoice.number}.pdf"
+ pdf_file = _create_pdf(rendered_template, pdf_path)
+ return pdf_file
diff --git a/templates/invoice.html b/templates/invoice.html
new file mode 100644
index 0000000000..b0d193d65e
--- /dev/null
+++ b/templates/invoice.html
@@ -0,0 +1,341 @@
+{% load price from taxed_prices %}
+<!DOCTYPE html>
+<html>
+
+<head>
+ <style>
+ @page {
+ background-color: #FAFAFA;
+ margin: 0.5cm;
+ }
+
+ .section-header,
+ .section-invoice-info {
+ background-color: #EFF5F8;
+ height: 235px;
+ }
+
+ .section-left {
+ width: 50%;
+ float: left;
+ }
+
+ .section-right {
+ width: 50%;
+ float: right;
+ }
+
+ .header-category {
+ font-family: Inter;
+ font-style: normal;
+ font-weight: normal;
+ font-size: 14px;
+ letter-spacing: 0.02em;
+ color: rgba(40, 35, 74, 0.6);
+
+ }
+
+ .header-item {
+ display: block;
+ color: black;
+ font-weight: bold;
+
+ font-family: Inter;
+ font-style: normal;
+ font-weight: bold;
+ font-size: 14px;
+ letter-spacing: 0.02em;
+ color: #28234A;
+ }
+
+ .header-title {
+ display: block;
+ font-family: Inter;
+ font-style: normal;
+ font-weight: 600;
+ font-size: 19px;
+ letter-spacing: 0.02em;
+ padding-bottom: 5px;
+ color: #28234A;
+ }
+
+ .content-padded {
+ padding: 20px;
+ }
+
+ .normal-text {
+ color: #28234A;
+ }
+
+ .summary-row {
+ line-height: normal;
+ }
+
+ .saleor-logo {
+ height: 40px;
+ width: 117px;
+ padding-bottom: 20px;
+ }
+
+ .products-table {
+ width: 100%;
+ line-height: 1.8;
+ }
+
+ .summary-table {
+ width: 100%;
+ line-height: 1.8;
+ padding-top: 20px;
+ }
+
+ .row-category>td,
+ .row-product>td {
+ border-bottom: 2px solid #EFF5F8;
+ }
+
+ .cell-product {
+ width: 55%;
+ }
+
+ .cell-price {
+ width: 15%;
+ }
+
+ .cell-summary {
+ width: 70%;
+ text-align: right;
+ padding-right: 30px;
+ }
+
+ .cell-quantity {
+ width: 15%;
+ }
+
+ .cell-total-price {
+ width: 15%;
+ }
+
+ .content-separator {
+ display: inline-block;
+ width: 100%;
+ border-bottom: 2px solid #EFF5F8;
+ }
+ </style>
+</head>
+
+<body>
+ <div class="section-header section-left">
+ <div class="content-padded">
+ <img class="saleor-logo" src="{{ logo_path }}" />
+ <br />
+ <span class="normal-text">{{ order.billing_address.company_name }}</span>
+ <br />
+ <span class="normal-text">{{ order.billing_address.street_address_1 }}</span>
+ <br />
+ {% if order.billing_address.street_address_2 %}
+ <span class="normal-text">{{ order.billing_address.street_address_2 }}</span>
+ <br />
+ {% endif %}
+ <span class="normal-text">{{ order.billing_address.city }}</span>
+ <br />
+ <span class="normal-text">{{ order.billing_address.postal_code }}</span>
+ <br />
+ <span class="normal-text">{{ order.billing_address.phone }}</span>
+ <br />
+ <span class="normal-text">{{ order.get_customer_email }}</span>
+ <br />
+ </div>
+ </div>
+ <div class="section-invoice-info section-right">
+ <div class="content-padded">
+ <span class="header-title">INVOICE INFORMATION</span>
+ <br />
+ <span class="header-category">Order:</span>
+ <br />
+ <span class="header-item">{{ ordr.id }}</span>
+ <br />
+
+ <span class="header-category">Date:</span>
+ <br />
+ <span class="header-item">{{ invoice.created }}</span>
+ <br />
+
+ <span class="header-category">Amount:</span>
+ <br />
+ <span class="header-item">{% price order.total_gross %}</span>
+ <br />
+ </div>
+ </div>
+ <div class="section-left">
+ <div class="content-padded">
+ <span class="header-title">BILLING ADDRESS</span>
+ <br />
+ <span class="normal-text">
+ {{ order.billing_address.first_name }}
+ {{ order.billing_address.last_name }}
+ </span>
+ <br />
+ <span class="normal-text">{{ order.billing_address.street_address_1 }}</span>
+ <br />
+ {% if order.billing_address.street_address_2 %}
+ <span class="normal-text">{{ order.billing_address.street_address_2 }}</span>
+ <br />
+ {% endif %}
+ <span class="normal-text">{{ order.billing_address.country }}</span>
+ <br />
+ <span class="normal-text">{{ order.billing_address.city }}</span>
+ <br />
+ <span class="normal-text">{{ order.billing_address.postal_code }}</span>
+ <br />
+ <span class="normal-text">{{ order.billing_address.phone }}</span>
+ <br />
+ <span class="normal-text">{{ order.get_customer_email }}</span>
+ <br />
+ </div>
+ </div>
+ <div class="section-right">
+ <div class="content-padded">
+ <span class="header-title">SHIPMENT ADDRESS</span>
+ <br />
+ <span class="normal-text">
+ {{ order.shipping_address.first_name }}
+ {{ order.shipping_address.last_name }}
+ </span>
+ <br />
+ <span class="normal-text">{{ order.shipping_address.street_address_1 }}</span>
+ <br />
+ {% if order.shipping_address.street_address_2 %}
+ <span class="normal-text">{{ order.shipping_address.street_address_2 }}</span>
+ <br />
+ {% endif %}
+ <span class="normal-text">{{ order.shipping_address.country }}</span>
+ <br />
+ <span class="normal-text">{{ order.shipping_address.city }}</span>
+ <br />
+ <span class="normal-text">{{ order.shipping_address.postal_code }}</span>
+ <br />
+ <span class="normal-text">{{ order.shipping_address.phone }}</span>
+ <br />
+ <span class="normal-text">{{ order.user_email }}</span>
+ <br />
+ </div>
+ </div>
+ <div class="content-padded">
+ <div class="content-separator">&nbsp;</div>
+ </div>
+ <div class="section-left">
+ <div class="content-padded">
+ <span class="header-title">PAYMENT METHOD</span>
+ <br />
+ <span class="normal-text">Credit Card</span>
+ <br />
+ </div>
+ </div>
+ <div class="section-right">
+ <div class="content-padded">
+ <span class="header-title">SHIPMENT METHOD</span>
+ <br />
+ <span class="normal-text">{{ order.shipping_method_name }}</span>
+ <br />
+ <span class="normal-text">Tracking number: 21212112</span>
+ <br />
+ </div>
+ </div>
+ <div class="content-padded">
+ <div class="content-separator">&nbsp;</div>
+ </div>
+ <div class="content-padded">
+ <span class="header-title">ITEMS ORDERED</span>
+ <br />
+ <table class="products-table">
+ <tbody>
+ <tr class="row-category">
+ <td class="cell-product">
+ <span class="header-category">Products</span>
+ </td>
+ <td class="cell-price">
+ <span class="header-category">Price</span>
+ </td>
+ <td class="cell-quantity">
+ <span class="header-category">Quantity</span>
+ </td>
+ <td class="cell-total-price">
+ <span class="header-category">Total Price</span>
+ </td>
+ </tr>
+ {% for line in order %}
+ <tr class="row-product">
+ <td class="cell-product">
+ <span class="normal-text">
+ {{ line|truncatechars:30 }}
+ <br />
+ SKU: {{ line.product_sku }}
+ </span>
+ </td>
+ <td class="cell-price">{% price line.unit_price.gross %}</td>
+ <td class="cell-quantity">{{ line.quantity }}</td>
+ <td class="cell-total-price">{% price line.get_total.gross %}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ <table class="summary-table">
+ <tbody>
+ <tr class="summary-row">
+ <td class="cell-summary">
+ <span class="normal-text">
+ Subtotal
+ </span>
+ </td>
+ <td class="cell-quantity"></td>
+ <td class="cell-total-price">
+ <span class="header-item">
+ {% price order.get_subtotal.gross %}
+ </span>
+ </td>
+ </tr>
+ <tr class="summary-row">
+ <td class="cell-summary">
+ <span class="normal-text">
+ Delivery Cost
+ </span>
+ </td>
+ <td class="cell-quantity"></td>
+ <td class="cell-total-price">
+ <span class="header-item">
+ {% price order.shipping_price.gross %}
+ </span>
+ </td>
+ </tr>
+ <tr class="summary-row">
+ <td class="cell-summary">
+ <span class="normal-text">
+ Tax
+ </span>
+ </td>
+ <td class="cell-quantity"></td>
+ <td class="cell-total-price">
+ <span class="header-item">
+ {% price order.total.tax %}
+ </span>
+ </td>
+ </tr>
+ <tr class="summary-row">
+ <td class="cell-summary">
+ <span class="normal-text">
+ Grand Total
+ </span>
+ </td>
+ <td class="cell-quantity"></td>
+ <td class="cell-total-price">
+ <span class="header-item">
+ {% price order.total.gross %}
+ </span>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+</body>
+
+</html>