safalmj
safalmj

Reputation: 692

PDF Prawn : Problem with image overriding the text

I want to put an image as a background on every page. This is my code :

Prawn::Document.generate("#{Rails.root.to_s}/public/#{filename}.pdf", :page_size => [576,576], :left_margin => 50,:right_margin => 50, :page_layout => :portrait, :skip_page_creation => true, :skip_encoding => true ) do |pdf|

  pdf.repeat :all do
    pdf.image open(APP_CONFIG['site_url']+"/images/pdf/bg_blank_high.jpg"), :at => [-pdf.bounds.absolute_left,  pdf.bounds.absolute_top], :width => 576, :height => 576
  end

  pdf.start_new_page(:margin => [75,50,50,50]) 
  pdf.fill_color "8c8464"
  pdf.text story.contents, :size => 12, :align => :left, :leading => 3 
end 

The problem is - image overrides the text in all pages.

Is there any way to put text over the image or make image 'send to back' or any other ideas?

Upvotes: 0

Views: 1917

Answers (2)

A5308Y
A5308Y

Reputation: 447

It seems that the code in the repeat-block is called after the text is rendered, thus putting it 'above' the text. The code snippet provided works because it just concerns a single image.

prawn_document( filename:       "filename.pdf",
                background:     path_background_image) do |pdf|
...

Might work for you. But it only provides low_res (72 dpi) images.

There is a pull request for allowing the resolution to be set for background images: https://github.com/sandal/prawn/pull/292

Upvotes: 1

Devin M
Devin M

Reputation: 9752

In the Prawn manual they give this as an example of an image covered by text:

# images/absolute_position.rb
# One of the options that the image method accepts is :at. If you've read some of the graphics
# examples you are probably already familiar with it. Just provide it the upper-left corner where you
# want the image placed.
# While sometimes useful this option won't be practical. Notice that the cursor won't be moved after the
# image is rendered and there is nothing forbidding the text to overlap with the image.

y_position = cursor
text "The image won't go below this line of text."
image "#{Prawn::BASEDIR}/data/images/fractal.jpg", :at => [200, y_position]
text "And this line of text will go just below the previous one."

So from what I can tell this might be a problem with how you are constructing the document or perhaps your cursor placing.

Upvotes: 1

Related Questions