Generating an arabic pdf file using Django and xhtml2pdf

I want to generate an Arabic PDF file using Django and xhtml2pdf library.

My code is this:

In the views.py

class ViewPDF(View):
    def get(self, request, *args, **kwargs):
        name=أفراد.objects.get(رقم_شخصي="100056").الإسم
        data={"name":name}
        pdf = render_to_pdf('pdf_template.html', data)
        return HttpResponse(pdf, content_type='application/pdf')
def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return pdf

first, I followed xhtml2pdf API to generate a PDF file. First I created a render_to_pdf method to create the needed PDF files. Second, I inserted the desired data into the template.

The template i'm using :

{% load static%}
<html dir="rtl">
    <pdf:language name="arabic"/>
<head>
    <title>ملف الشخصي</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Naskh+Arabic:400,700" rel="stylesheet">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <style>
        @page {
            size: a4 portrait;
            @frame header_frame {           /* Static Frame */
                -pdf-frame-content: header_content;
                left: 50pt; width: 512pt; top: 50pt; height: 40pt;
            }
            @frame content_frame {          /* Content Frame */
                left: 50pt; width: 512pt; top: 90pt; height: 632pt;
            }
            @frame footer_frame {           /* Another static Frame */
                -pdf-frame-content: footer_content;
                left: 50pt; width: 512pt; top: 772pt; height: 20pt;
            }
            body {
                direction:rtl;

            }
        }
    </style>
    </head>
    
    <body>

        <!-- Content for Static Frame 'header_frame' -->
        <div id="header_content">
            <h3>ملف الشخصي</h3>
        </div>
        For arabic it's not working
        <!-- Content for Static Frame 'footer_frame' -->
        <div id="footer_content">(c) - page <pdf:pagenumber>
            of <pdf:pagecount>
        </div>
        <h3>ملف الشخصي</h3>
        {{name}}
    </body>
    </html>

I did add a few font settings, but they didn't change nor add anything worth mentioning.

The result i'm having:

a pdf file with dark squares instead of arabic letters

I tried adding a various font and adding styling tags. Yet nothing changed.

Вернуться на верх