0

So I'm creating a streamlit web app where people provide a paragraph and I split it up into sentences and allow them to download it as a PDF after applying color-blind friendly color coding. To do this, I create HTML and add the color coding as a style attribute. I then try and download it using fpdf2. However, when I open the downloaded PDF, there is no color.

class PDF(FPDF, HTMLMixin):
    pass

def create_html(text, hex_codes):
    result = nltk.sent_tokenize(text)
    hex1 = hex_codes[0]
    hex2  = hex_codes[1]
    flag = True
    res = []
    for sentence in result:
        if flag:
            hex = hex1
        else:
            hex = hex2
        flag = not flag
        res.append(f"<p style ='color:{hex}'>{sentence}</p>")
    return ''.join(res)

def create_pdf(html):
    pdf = PDF()
    pdf.add_page()
    pdf.write_html(html)
    return BytesIO(pdf.output())

Is there any reason this happens?

martineau
  • 119,623
  • 25
  • 170
  • 301
Nakul Upadhya
  • 494
  • 4
  • 16

1 Answers1

0

Reading the docs, it looks like coloring works with the <font> tag:

res.append(f"<p><font color='{hex}'>{sentence}</font></p>")
Jonathan Ciapetti
  • 1,261
  • 3
  • 11
  • 16