Im using prawn to generate a PDF output in a rails app. How do i change the color of the outputted text?
Asked
Active
Viewed 1.6k times
4 Answers
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
-
1":at is no longer a valid option with text.use draw_text or text_box instead" – BenKoshy Jan 03 '22 at 04:49
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:
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