Search code examples
pythondjangopython-3.xdjango-templatesweasyprint

Django/WeasyPrint: Bangla Font Rendering Issue


My Python Version 3.6+, Django version 1.11.6 and OS: Windows-10 64bit.

I am creating the PDF file with weasyprint within Django project. But the problem is Bengali font is not rendering. Please check this screenshot. enter image description here

Now this is my views.py file

def get_pdf_file(request, customer_id, sys_type):
    sys_type = customer_id
    area = "pdf"
    site_credit = site_credit1
    time_now = timezone.now()
    customers = get_object_or_404(CustomerInfo, pk=customer_id)

    due_taka_track = customers.duetaka_set.all()

    if due_taka_track == None:
        due_taka_track = 0

    unpaid_taka = int(customers.customer_price -
                      customers.customer_due_taka_info)
    due_taka_track = customers.duetaka_set.all()

    sum_cost_taka = due_taka_track.aggregate(
        sp=Sum('customer_due')).get('sp', 0)

    if sum_cost_taka == None:
        sum_cost_taka = 0
    total_paid_taka = sum_cost_taka + customers.customer_due_taka_info

    payment_status = 'complete'
    payment_message = ' '
    remain_taka='Full Paid '
    remain_msg=''


    if customers.customer_due_taka_info < customers.customer_price:
        payment_status = 'incomplete'
        payment_message = 'সম্পূর্ন টাকা পরিষোধ করা হয়নি'
        remain_msg='টাকা বাকী আছে'
        baki_ase="পাওনা আছে "

        remain_taka = customers.customer_price - customers.customer_due_taka_info



    context = {'customers': customers,
    'sys_type': sys_type,
    'area': area,
    'site_credit': site_credit,
    'site_name': 'Moon Telecom',
    'sys_type': sys_type,
    'due_taka_track': due_taka_track,
    'total_paid_taka': total_paid_taka,
    'payment_message': payment_message,
    'time_now': time_now,
    'unpaid_taka': unpaid_taka,
    'payment_message': payment_message,
    'remain_taka': remain_taka,
    'sum_cost_taka': sum_cost_taka,
    'remain_msg': remain_msg}

    html_string = render_to_string('shop/pdf_invoice.html', context).encode(encoding="UTF-8")
    response = HttpResponse(content_type='application/pdf')

    response['Content-Disposition'] = 'inline; filename=invoice' + \
        str(customers.id)+customers.customer_uid + customers.customer_name + '.pdf'

    HTML(string=html_string).write_pdf(response)
    return response

and this is my html file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-16">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">



    <title>invoice copy {% if customers.customer_name %}Customer Name:{{customers.customer_name}}{% else %}
    Name: দেওয়া হয়নি{% endif %} | {{customers.customer_uid}}</title>

<head>
    <style>
        @import url('https://fonts.maateen.me/siyam-rupali/font.css');
        table,
        th,
        td {
            border: 1px solid black;
        }

        th,
        td {
            padding: 5px;
            text-align: left;
        }
        p,th,td,th,tr{
            font-size:10px;line-height:12px
            font-family: 'SiyamRupali', sans-serif;
        }
        .border_sx {
            border-style: solid;
            border-width: 1px;
            padding: 5px;
            border-radius: 5px;
            display:block;
            width: 100%;
            margin-right:-100px;
            padding: 10px;

}


        #background{
    position:absolute;
    z-index:0;
    background:white;
    display:block;
    min-height:50%; 
    min-width:50%;
    color:yellow;
}

#content{
    position:absolute;
    z-index:0.1;
}

#bg-text
{
    color:lightgrey;
    font-size:70px;
    margin-top: 160px;

    margin-right: 40px;
    transform:rotate(320deg);
    -webkit-transform:rotate(300deg);
}

#bg-text-one
{
    color:lightgrey;
    font-size:20px;
    line-height: 25px;
    margin-right: 100px;
    margin-bottom: 800px;
    margin-top: -10px;
    transform:rotate(320deg);
    -webkit-transform:rotate(300deg);
}
    #fix_font
    {
        font-size: 13.5px;
    }

    table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {

    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {

}
    </style>
</head>

<body>
    <div id="background">
        <p id="bg-text">Moon Telecom</p>
        <p id="bg-text-one">Fulhata Bazar জাহাঙ্গীর সুপার মার্কেট, ব্রীজ রোড, ফুলহাতা বাজার, মোডেলগঞ্জ। 01717-051200(সুকান্ত বসু) 01828-163858(দোকান)</p>
    </div>

    <div id="content" class="border_sx">
        <div class="invoice_intro">
            <div class="site_title" style="text-align:center; margin:0 auto">
                <h1 style="font-size:10px;line-height:12px">ওঁ</h1>
                <p style="font-size:8px;line-height:10px">মা কালী সহায়</p>
                <h2 style="font-size:19px;line-height:13px;">মুন টেলিকম</h2>

                <div class="invoice_info_one" style="width:70%;margin:0 auto; text-align:center">
                    <p style=";">সকল প্রকার মোবাইল সেট, সীম কার্ড, মেমোরী কার্ড, MP-3, সোলার চার্জার, সোলার ফ্যান, মোবাইল ফোনের ব্যাটারী, চার্জার,
                        ক্যাচিং,কাভার,হেডফোন, রেবন, ডিসপ্লে এবং ইলেট্রিক মালামাল বিক্রেতা</p>
                </div>
                <div class="invoice_location">
                    <p>জাহাঙ্গীর সুপার মার্কেট, ব্রীজ রোড, ফুলহাতা বাজার, মোডেলগঞ্জ।</p>
                </div>
                <div class="invoice_contact">
                    <p>01717-051200(সুকান্ত বসু) 01828-163858(দোকান)
                        <b>Email:</b> moontelecom2008@gmail.com</p>
                </div>



                <hr>
            </div>
        </div>


        <div class="customer_part" style="width:40%;margin:0px left;display:block">

            <div class="customer_info">
                {% if customers.customer_name %}
                <p style="font-size:">Customer Name:
                    <b> {{customers.customer_name}}</b>

                </p>
                {% else %}
                <p> Name: দেওয়া হয়নি</p> {% endif %}

                <p style="font-size:x">Phone Number: {% if customers.customer_mobile_no %} {{customers.customer_mobile_no}} {% else %} No Mobile Number
                    {% endif%}
                </p>
                <p style="font-size:">Purchase Time: {{customers.customer_sell_date}}</p>
                <p style="font-size:">invoice id:
                    <b>{{customers.customer_uid}}</b>
                </p>
                <p>{{customers.product_warrenty}}</p>
            </div>
        </div>





        <div class="">
            <table style="border-style: solid;border-width: 0px;">

                <tr>
                    <th>Product Name</th>
                    <th colspan="">Price</th>
                    <th>Product Price (MRP)</th>
                    <th>ID or IME</th>
                    <th>Warrenty</th>
                    <th>QN</th>

                </tr>


                <tr>
                    <td>
                        <b>{{customers.customer_product_name}}</b>
                    </td>
                    <td>
                        <b>Customer Price: {{customers.customer_price}}</b> TK
                        <i>{% if customers.customer_discount_taka %} 
                        ( Discount added {{customers.customer_discount_taka}} TK )
                        {% else%}
                        {%endif%}</i>
                    </td>
                    <td>{{customers.customer_product_mrp}} Taka * {{customers.customer_product_quantity}} (QN) </td>
                    <td>{{customers.customer_product_id}}</td>


                    <td>
                        {% if customers.customer_product_warrenty %} {{customers.customer_product_warrenty}} Months {% else %} No {% endif %}
                    </td>
                    <td>{{customers.customer_product_quantity}}</td>
                </tr>


                <th>
                    First Time Payment
                </th>

                <th>

                    {{customers.customer_first_time_payment}} TK
                    <span style="color:red">{{customers.customer_first_due_info}}</span>
                </th>

                <td>
                    {{customers.customer_sell_date}}
                </td>


                {% if due_taka_track %}



                <tr>
                    <th>SL</th>
                    <th>Taka</th>
                    <th>Paid Date</th>
                    <th>Due Info</th>

                </tr>

                <hr> {% for track in due_taka_track %}
                <tr>
                    <td>{{forloop.counter}}</td>
                    <td>
                        <i>{{track.customer_due}}</i> TK</td>
                    <td>{{track.customer_due_date}}</td>
                    <td>{{track.customer_due_info}}</td>
                </tr>
                {% endfor %} {% else %} {% endif %} {% if sum_cost_taka %}
                <tr>

                    <td>Total Due Complete </td>

                    <th>{{sum_cost_taka}} TK</th>

                </tr>
                {% else %} {% endif %}





                <tr>
                    <td>
                        <b>Total Paid</b>
                    </td>

                    <th>


                        {% if payment_message %} 
                        {{customers.customer_due_taka_info}} TK

                        <span style="color:red">{{payment_message}}</span>
                        <br>



                        <button type="button" class="btn btn-danger">{{remain_msg}} {{remain_taka}}TK</button>
                        {%else %} {{customers.customer_due_taka_info}} TK
                        <button type="button" class="btn btn-success">Payment Completed</button>

                        {% endif %}
                    </th>
                </tr>
            </table>
            <table border="none!important" style="margin-top:10px;border:none!important">
                <tr>
                    <th style="text-align:left">Customer signature</th>
                    <th  style="text-align:right">Authorized signature</th>
                </tr>
            </table>
        </div>


        <div class="customer_notifications">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">


                        {% if customers.customer_conditions %}
                        <div class="warning">
                            <p>
                                <i>{{customers.customer_product_name}} {{customers.customer_conditions}}</i>
                            </p>
                        </div>

                        {% else %} {% endif %}

                        <div class="col-lg-12">
                            <div class="footer_info">
                                <p id="fix_font"><b>বিঃদ্রঃ ডেলিভারির সময় মাল বুঝিয়া নিবেন। পরে কোন আপত্তী গ্রহনযোগ্য নয়। (বিক্রিত মাল ফেরত হয় না) </h6>
                                <p>Print date: {{time_now}}</b></p>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>








        <div class="footer" style="display:block">
            <div class="footer_info">
                <div class="container">
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="footer_info">
                                <div class="copy_right">
                                    {% if site_credit %}
                                    <p>{{site_credit}}</p>
                                    {% else %} {% endif %}

                                </div>

                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


</body>

</html>

I changed UTF-8/UTF but this issue did not fix.


Solution

  • I installed(Kalpurush) font on my PC. Then I added that font write_pdf variable.

    HTML(string=html_string).write_pdf(response,stylesheets=[CSS(string='body,p,h1,h2,h3,h4,h5,tr,td { font-family: Kalpurush !important }')])