I'm trying to convert HTML to PDF using xhtml2pdf but not able to properly render the emoji characters in the HTML they appear as black boxes. I have tried with different fonts but still the same issue.
# -*- coding: utf-8 -*-
import time
import os
from io import BytesIO
from xhtml2pdf import pisa
source = """<html>
<head>
<title>Doc title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
</head>
<style>
@media print {
@font-face {
font-family: Helvetica;
src: url("/absolute/path/to/Helvetica.ttf");
}
}
</style>
<body>
<p>🤓</p>
</body>
</html>"""
def convert_html_to_pdf(source):
pdf = BytesIO()
pisaStatus = pisa.CreatePDF(BytesIO(source), pdf, encoding='utf-8')
return pdf
if __name__ == "__main__":
pdf = convertHtmlToPdf(source)
fd = open(os.path.join('/home/userX/', 'test-%s.pdf' % time.time()), "w+b")
fd.write(pdf.getvalue())
fd.close()
So I was able to get emoji on PDF using Symbola font as following:
# -*- coding: utf-8 -*-
import time
import os
from io import BytesIO
from xhtml2pdf import pisa
source = """<html>
<head>
<title>Doc title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
</head>
<style>
@media print {
@font-face {
font-family: Symbola;
src: url("/absolute/path/to/Symbola.ttf");
}
}
body {font-family: Symbola, sans-serif !important}
</style>
<body>
<p>🤓</p>
</body>
</html>"""
def convert_html_to_pdf(source):
pdf = BytesIO()
pisaStatus = pisa.CreatePDF(BytesIO(source), pdf, encoding='utf-8')
return pdf
if __name__ == "__main__":
pdf = convertHtmlToPdf(source)
fd = open(os.path.join('/home/userX/', 'test-%s.pdf' % time.time()), "w+b")
fd.write(pdf.getvalue())
fd.close()