2

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.

Stanislas
  • 21
  • 2
  • The library was not designed to handle characters outside of the BMP (i.e. first 65536 characters of the Unicode). Besides, even if it was, emoji would be printed in black, like any other characters. – Olivier May 08 '20 at 07:41

1 Answers1

1

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.

AKX
  • 152,115
  • 15
  • 115
  • 172