[FIX] PAYROLL: Indian currency display issue in pdf
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
from datetime import date, datetime, time
|
||||
|
||||
import pandas as pd
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles import finders
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db import models
|
||||
from django.db.models import F, ForeignKey, ManyToManyField, OneToOneField
|
||||
from django.db.models.functions import Lower
|
||||
from django.forms.models import ModelChoiceField
|
||||
from django.http import HttpResponse
|
||||
from django.template.loader import render_to_string
|
||||
from django.template.loader import get_template, render_to_string
|
||||
from django.utils.translation import gettext as _
|
||||
from xhtml2pdf import pisa
|
||||
|
||||
@@ -607,24 +610,65 @@ def check_owner(employee, instance):
|
||||
return False
|
||||
|
||||
|
||||
def link_callback(uri, rel):
|
||||
"""
|
||||
Convert HTML URIs to absolute system paths so xhtml2pdf can access those
|
||||
resources
|
||||
"""
|
||||
|
||||
uri = "payroll/fonts/Poppins_Regular.ttf"
|
||||
result = finders.find(uri)
|
||||
if result:
|
||||
if not isinstance(result, (list, tuple)):
|
||||
result = [result]
|
||||
|
||||
result = list(os.path.realpath(path) for path in result)
|
||||
path = result[0]
|
||||
|
||||
else:
|
||||
sUrl = settings.STATIC_URL
|
||||
sRoot = settings.STATIC_ROOT
|
||||
mUrl = settings.MEDIA_URL
|
||||
mRoot = settings.MEDIA_ROOT
|
||||
|
||||
if uri.startswith(sUrl):
|
||||
path = os.path.join(sRoot, uri.replace(sUrl, ""))
|
||||
else:
|
||||
return uri
|
||||
|
||||
if os.name == "nt":
|
||||
return uri
|
||||
|
||||
if not os.path.isfile(path):
|
||||
raise RuntimeError("media URI must start with %s or %s" % (sUrl, mUrl))
|
||||
return path
|
||||
|
||||
|
||||
def generate_pdf(template_path, context, path=True, title=None):
|
||||
html = template_path
|
||||
template_path = template_path
|
||||
context_data = context
|
||||
title = (
|
||||
f"""{context.get("employee")}'s payslip for {context.get("range")}.pdf"""
|
||||
f"""{context_data.get("employee")}'s payslip for {context_data.get("range")}.pdf"""
|
||||
if not title
|
||||
else title
|
||||
)
|
||||
if path:
|
||||
html = render_to_string(template_path, context)
|
||||
|
||||
result = io.BytesIO()
|
||||
pdf = pisa.pisaDocument(io.BytesIO(html.encode("utf-8")), result)
|
||||
response = HttpResponse(content_type="application/pdf")
|
||||
response["Content-Disposition"] = f"attachment; filename={title}"
|
||||
|
||||
if not pdf.err:
|
||||
response = HttpResponse(result.getvalue(), content_type="application/pdf")
|
||||
response["Content-Disposition"] = f'''attachment;filename="{title}"'''
|
||||
return response
|
||||
return None
|
||||
template = get_template(template_path)
|
||||
html = template.render(context_data)
|
||||
|
||||
pisa_status = pisa.CreatePDF(
|
||||
html.encode("utf-8"),
|
||||
dest=response,
|
||||
link_callback=link_callback,
|
||||
)
|
||||
|
||||
if pisa_status.err:
|
||||
return HttpResponse("We had some errors <pre>" + html + "</pre>")
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def filter_conditional_leave_request(request):
|
||||
|
||||
BIN
payroll/static/payroll/fonts/Poppins_Regular.ttf
Normal file
BIN
payroll/static/payroll/fonts/Poppins_Regular.ttf
Normal file
Binary file not shown.
@@ -14,9 +14,19 @@
|
||||
</head>
|
||||
<body>
|
||||
<style>
|
||||
@font-face {
|
||||
font-family: 'Poppins';
|
||||
src: url('{% static "payroll/fonts/Poppins_Regular.ttf" %}') format('truetype');
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
}
|
||||
body {
|
||||
font-family: "Poppins", sans-serif;
|
||||
}
|
||||
|
||||
li {
|
||||
font-size: 12px;
|
||||
font-family: "poppins";
|
||||
font-family: "Poppins";
|
||||
}
|
||||
.oh-payslip__table-th,
|
||||
.oh-payslip__table-tf {
|
||||
|
||||
Reference in New Issue
Block a user