Stanislas
Stanislas

Reputation: 21

Adding emoji's to a PDF using PyFPDF

I am writing a program that is supposed to turn a paragraph into a PDF. A lot of said paragraphs have emoji's in them and I cannot figure out how I am supposed to make them show up on the PDF.

Whenever there is an emoji in a paragraph I get the following error

File "C:\Python38\lib\site-packages\fpdf\fpdf.py", line 1449, in _putTTfontwidths
    if (font['cw'][cid] == 0):
IndexError: list index out of range

Now from my understanding this basically says that an emoji was not found at this uncode location in the font. But I have looked at the font in detail and it does indeed contain emojis.

getting the length of font['cw'] reveals that it goes up to 65536 when the emoji in question is located at position 128522 which is almost twice as far.

Now if I edit the fpdf code from this

if (font['cw'][cid] == 0):
        continue

to this

try:
    if (font['cw'][cid] == 0):
        continue
except:
    continue

It prints 2 boxes instead of emojis but if I copy paste the boxes into a web browser they are displayed correctly.

I am assuming this is an encoding problem. But I haven't really meddled with encoding so i am unsure how to proceed.

Upvotes: 2

Views: 1869

Answers (1)

AKX
AKX

Reputation: 169268

Seems to be a known bug: https://github.com/reingart/pyfpdf/issues/131

It looks like Fpdf hasn't been updated in a while. There's apparently a fork called fpdf2: https://pypi.org/project/fpdf2/

If that fails too, you could see if the ReportLab or WeasyPrint libraries work better for you.

Upvotes: 1

Related Questions