Changing the background color of a pdf with Prawn?
Asked Answered
M

1

6

I can't for the life of me find any documentation on how to change the background color of a pdf to something besides white?

Mercorr answered 30/4, 2015 at 3:19 Comment(0)
G
8

You could have the first element on a page be a filled rectangle that takes up the entire page. canvas will let you work with the bounds of the entire page.

canvas do
  fill_color "FFFFCC"
  fill_rectangle [bounds.left, bounds.top], bounds.right, bounds.top
end

The following script would create this PDF.

require "prawn"

def background_color(color)
  tmp_color = fill_color
  canvas do
    fill_color color
    fill_rectangle [bounds.left, bounds.top], bounds.right, bounds.top
  end
  fill_color tmp_color
end

Prawn::Document.generate("colored-pages.pdf") do
  fill_color "FF0000"

  background_color "FFFFCC"
  text "Text on page 1"

  start_new_page
  background_color "FFCCFF"
  text "Text on page 2"

  start_new_page
  background_color "CCFFFF"
  text "Text on page 3"

  start_new_page
  background_color "CCFFCC"
  text "Text on page 4"
end
Goldston answered 24/9, 2015 at 12:44 Comment(1)
Thanks for the help. Sorry about not noticing it in the manual at first. It covers quite a bit, and unless you read it cover-to-cover, it's difficult to know exactly what to look for.Trackless

© 2022 - 2024 — McMap. All rights reserved.