Question

I have an image image.jpg that I want to place in a footer at the bottom left of every page. I am currently doing that, using Ruby 1.9.3 and the prawn gem to create a PDF, like so :

start_new_page
image "image.jpg", :at => [0,30]  

There are two issues with this :

1) I am creating pages dynamically, and I need the image placed on all pages. Since I never know how many pages will be created, I don't know how to place an image on each page.

2) My tables within my PDF are being written on top of the logo, causing the logo to not be visible.

I found this code snippet in a sample:

number_pages "<page> in a total of <total>", { :start_count_at => 0, :page_filter => :all, :at => [bounds.right - 50, 0], :align => :right, :size => 14 }

This will place text on each page, dynamically just the way I want it to; however, it doesn't work with images.

How can I place an image on the bottom left of each page within my document?

Was it helpful?

Solution 2

what exactly is the problem here?

this works fine for me:

require 'prawn'

Prawn::Document.generate('hello.pdf') do |pdf|
  pdf.text("Hello Prawn!")
  pdf.image "image.jpg", :at => [pdf.bounds.right - 100, 0], :align => :right
  pdf.number_pages "<page> in a total of <total>", :start_count_at => 0, :page_filter => :all, :at => [pdf.bounds.right - 100, 0], :align => :right, :size => 14
end

and for adding it to all pages you can do this:

require 'prawn'

Prawn::Document.generate('hello.pdf') do |pdf|
  pdf.text("Hello Prawn!")
  pdf.start_new_page
  pdf.text("Hello Prawn!")
  pdf.start_new_page
  pdf.text("Hello Prawn!")
  pdf.page_count.times do |i|
    pdf.go_to_page i
    pdf.image "image.jpg", :at => [pdf.bounds.right - 100, 0], :align => :right
  end
  pdf.number_pages "<page> in a total of <total>", :start_count_at => 0, :page_filter => :all, :at => [pdf.bounds.right - 100, 0], :align => :right, :size => 14
end

there might be better alternatives, but i never used prawn before.

OTHER TIPS

Why would you not use repeat?

repeat(:all) do
 image "image.jpg", :at => [pdf.bounds.right - 100, 0], :align => :right
end

You can find more information in the Prawn manual (page 98).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top