15

Im using prawn to generate a PDF output in a rails app. How do i change the color of the outputted text?

miku
  • 181,842
  • 47
  • 306
  • 310
Arcath
  • 4,331
  • 9
  • 39
  • 71

4 Answers4

34

Have you tried fill_color? Code below should work:

require "rubygems"
require "prawn" 

Prawn::Document.generate "hello.pdf" do 
  fill_color "0000ff" 
  text_box "Hello World (in blue)", :at => [200,720], :size => 32 
end
BenKoshy
  • 33,477
  • 14
  • 111
  • 80
miku
  • 181,842
  • 47
  • 306
  • 310
6

if you use any 1.x version (it's only a pre-release as of writing) you can also use:

Gem install:

$ gem install prawn --pre

Code:

require "rubygems"
require "prawn" 

Prawn::Document.generate "hello.pdf" do 
  text "Hello World (in blue)", :color => "0000ff", :size => 32 
end
SztupY
  • 10,291
  • 8
  • 64
  • 87
3

Note that you can also set a CMYK color (in this example 100% key black):

fill_color(0,0,0,100)
Gawin
  • 994
  • 10
  • 12
1

Use the color option

text "Red color here", color: 'FF0000'

And the result:

Red color here

With the utmost respect, there is no need to use fill_color as of 2022 because you have to manually set and then unset the color - IMO it's much better to set the color directly via the prawn DSL (domain specific language).

BenKoshy
  • 33,477
  • 14
  • 111
  • 80