34

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.

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
Goro
  • 9,919
  • 22
  • 74
  • 108

4 Answers4

55

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()
Nicholas TJ
  • 1,629
  • 18
  • 30
  • 24
    From what I can tell, using the 256 color space won't work. The manual states that using 1 is 'all lights on full'. So, to create 256,256,256 is actually done by using (1,1,1). Thus, to get something in between, you'll have to use decimals. For me, I wanted the RGB: (75,116,156) so I had to write: `setFillColorRGB(0.29296875,0.453125,0.609375)`. That's the equivelant to: `75/256, 116/256, 156/256`. A bit ridiculous IMO, but it came out perfect. – Garfonzo Feb 05 '13 at 19:36
  • This indeed work! But when I set some differente color the text seems to be transparent... – gbrennon Oct 17 '16 at 20:02
  • @gbrennon, it's just all white. You have to pass RGB as triple floats in range [0, 1]. They won't raise any error when you pass numbers over 1, they just use 1 (`min(1, passed_color_value)`). – Ctrl-C May 11 '17 at 16:58
  • 1
    Note: [`registerFont`](https://stackoverflow.com/a/4900031) is required to make this answer work, at least for me with Windows 7 + Python 2.7 – Basj Apr 30 '19 at 10:18
22
from reportlab.lib.colors import HexColor
...

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

...
SergO
  • 2,703
  • 1
  • 30
  • 23
0

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.

0

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).

Gordon Seidoh Worley
  • 7,839
  • 6
  • 45
  • 82
  • setFillColor() doesn't do anything for the text, either. – Goro Mar 25 '12 at 19:29
  • Then I would guess that the example is incorrect. Looking in the docs, in the User Guide section 2.11 gives examples of setting text color in this way. Are you able to set the color of things other than text? If not, maybe it's a more general problem without outputting color. – Gordon Seidoh Worley Mar 27 '12 at 15:07