How encode this string to export it to PDF

323 views Asked by At

I'm trying to export to PDF a string containing UTF-8 characters. I have reduced my code to the following code to be able to reproduce it:

source = """<html>
            <style>
                @font-face {
                font-family: Preeti;
                src: url("preeti.ttf");
                }

                body {
                font-family: Preeti;
                }
            </style>
            <head>
                <meta http-equiv="content-type" content="text/html; charset=utf-8">
            </head>
            <body>
                This is a test <br/>
                       सरल
            </body>
        </html>"""

from xhtml2pdf import pisa
from io import StringIO, BytesIO

bytes_result = BytesIO()
pisa.CreatePDF(source, bytes_result, encoding='UTF-8')
result = StringIO(bytes_result.getvalue().decode('UTF-8', 'ignore'))

f = open('test.pdf', 'wt', encoding='utf-8')
f.write(result.getvalue())

Which produces the following PDF:

enter image description here

I have tried a ton of solutions from the internet but I cannot get it right. Any idea what am I missing?

0

There are 0 answers