pythonpdffpdf

Python: Coloring cells in FPDF is not working?


I'm using the FPDF library to create a PDF, and I was looking to color the cells on my document. I looked through the API and found out that this is the way to do it:

fpdf.set_fill_color(r: int, g: int = -1, b: int = -1)

So I go ahead and do this in my script:

pdf = FPDF()

pdf.add_page()

pdf.set_font('Arial', 'B', 7)
pdf.set_fill_color(0, 0, 255)
pdf.cell(190, 6, 'Testing...', 1, 1, 'L')

pdf.output('Color.pdf', 'F')

And the color does not change. Everything else works fine, I just get a white cell instead of one that is colored blue. No errors are thrown either. Am I doing something wrong or is this PyFPDF malfunctioning?

EDIT: Added pdf.add_page() and pdf.output('Color.pdf', 'F') to this question (forgot to do it here, had it in my script).


Solution

  • According to the docs you must set the fill to True.

    [...]

    fill:

    Indicates if the cell background must be painted (True) or transparent (False). Default value: False.

    [...]


    from fpdf import FPDF
    
    pdf = FPDF()
    
    pdf.add_page()
    
    pdf.set_font('Arial', 'B', 7)
    pdf.set_fill_color(0, 0, 255)
    pdf.cell(190, 6, 'Testing...', 1, 1, 'L', fill=True)
    
    pdf.output('Color.pdf', 'F')