How to change text/font color in reportlab.pdfgen

Question:

I want to use a different color of text in my auto-generated PDF.

According to the reportlab docs all I need to do is:

self.canvas.setFillColorRGB(255,0,0)
self.canvas.drawCentredString(...)

But that doesn’t do anything. The text is black no matter what.

Asked By: Goro

||

Answers:

I can’t verify this at the moment, but if you look in the linked example whenever they set the color before calling drawCenteredString they always do it with setFillColor, never setFillColorRGB, only using the latter to set the color of rects. So try instead changing it to

self.canvas.setFillColor(red)
self.canvas.drawCenteredString(...)

I don’t know if it says this in that doc or not, but the variable red is defined as a constant in one of the ReportLab modules, so if you have any errors with that, just be sure to include the appropriate module (the exact name escapes me at the moment).

If you copy and paste the code in User Guide Section 2. You’ll get a fancy coloured rectangle with a coloured Text within it. Probably the approach is not that clear in the user guide, I’d spent some time playing with it and I finally know how it works.

You need to imagine yourself drawing a canvas. You need to do all the setup before you draw. Below is an example I prepared to show better how to style a text, draw a line, and draw a rectangle, all with the ability to put colour on them.

from reportlab.pdfgen import canvas

def hello(c):
    from reportlab.lib.units import inch

    #First Example
    c.setFillColorRGB(1,0,0) #choose your font colour
    c.setFont("Helvetica", 30) #choose your font type and font size
    c.drawString(100,100,"Hello World") # write your text

    #Second Example
    c.setStrokeColorRGB(0,1,0.3) #choose your line color
    c.line(2,2,2*inch,2*inch)

    #Third Example
    c.setFillColorRGB(1,1,0) #choose fill colour
    c.rect(4*inch,4*inch,2*inch,3*inch, fill=1) #draw rectangle

c = canvas.Canvas("hello.pdf")

hello(c)
c.showPage()
c.save()
Answered By: Nicholas TJ
from reportlab.lib.colors import HexColor
...

# sets fill color like orange
c.setFillColor(HexColor(0xff8100))
# or 
c.setFillColor(HexColor('#ff8100'))

...
Answered By: SergO

Although this question is a little old (!), I believe I have a positive contribution to anyone who, like me, has the same question.

In addition to the setFillColorRGB() function, you can also use setFillColor(), which takes color names as parameters (as Python variables, not strings).

I wanted to write a string in red. Then I did:

setFillColor(red)

and it worked.

The reportlab.lib.colors module has a huge list of color names that can be used. See lines 532 to 681. So, in this case, I put

from reportlab.lib.colors import red

in the beginning of my script.

Hope this helps.

This answer was machine translated from Portuguese, my mother tongue, into English.

Answered By: Gilberto Silva
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.