Below are some steps to troubleshoot & fix your python project so that your PDF’s contain emojis 🤓
For me, specifying encoding fixed the issue, as well as ensuring that the font file was loading properly.
Summary
- Use UTF-8 encoding:
- Ensure <meta charset=”UTF-8″> is in the HTML’s <head> tag
 - encoding=”utf-8″ when using with open
 
 - Make sure your using the correct path to your font file
 
My code that works
Below, I bolded the parts that made my PDF display emojis & other characters properly. *note: I’m using an f-doc-string so the brackets work a little differently.
from weasyprint import HTML
import os
# HTML sample that worked
html_sample = f"""<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Social Media Marketing Report</title>
        <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
        <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Color+Emoji&display=swap" rel="stylesheet">
        <style>
            @page {{
                size: A4 landscape;
                margin: 1cm;
            }}
            @font-face {{
            font-family: 'Noto Color Emoji';
            src: url('apps/app/dir/NotoColorEmoji-Regular.ttf') format('truetype');}}
            body {{font-family: 'Montserrat', 'Helvetica', sans-serif;}}
        </style>
    </head>
    <body>
        <header style="text-align: center;">
            <img src="{appaloosa_logo}" alt="" style="max-width: 200px;">
            <h1>{report_title}</h1>
            <p>Instagram & Facebook Metrics. 🇺🇸🙂</p>
            <hr style="width:80%; border: none; border-top: 3px solid #F0E2B6;">
        </header>"""
# Specify encoding when creating HTML
with open("temp/sample.html", "w", encoding="utf-8") as file:
    file.write(html_sample)
# Use weasyprint as you normally would   
HTML('temp/report.html').write_pdf('output/sample.pdf')
Check if Font File exists
Here’s a way you can check if your font file is working. If it is working, then it might be an encoding issue.
# Check if the font file exists
if os.path.exists('path-to-dir/NotoColorEmoji-Regular.ttf'):
    print("Font file exists.")
else:
    print("Font file does not exist. Please check the path.")
That’s pretty much it! (for me)
If your emoji file is being referenced correctly, and you’re using utf-8 encoding in your HTML & with python’s os library, then it should work. At least, it did for me. If not, you could check the weasyprint’s github or reach out.


Leave a Reply