Question

I'm building a PDF export function in Rails and Prawn where I want to export a number of "Companies" in one single PDF. A company usually flows over 2-3 pages. Right now when exporting a single Company this works with a pdf.repeat(:all). I want the logo to change in the header based on the Company though. A simple code example is:

@companies.each do |c|
 pdf.repeat(:all) do
  pdf.image company.logo.url(:thumb), :at => [0,520]
 end
end

Is there a way of doing this? I have looked at related topics, like header and footer in Prawn PDF but it won't help me since I can't see what Company belongs to which page after it's generated.

Was it helpful?

Solution

The awesome self-documenting manual (http://cloud.github.com/downloads/sandal/prawn/manual.pdf) contains this code (page 105), which may be of help:

repeat(lambda { |pg| pg % 3 == 0 }) do
  draw_text "Every third page", :at => [250, 20]
end
repeat(:all, :dynamic => true) do
  draw_text page_number, :at => [500, 0]
end

Either lambda or the dynamic should do the trick for you, provided that you know what company starts at what page.

In case you do not know about the number of pages per company, make a pdf for each of them and merge them. Manual, page 109:

filename = "#{Prawn::BASEDIR}/data/pdfs/multipage_template.pdf"
Prawn::Example.generate("full_template.pdf", :template => filename) do
  go_to_page(page_count)
  start_new_page
  text "Previous pages and content imported.", :align => :center
  text "This page and content is brand new.", :align => :center
end

In the worst case you will end up with merging two at a time.

You might also check if pdf.number_pages with :filter option works with images (if you have not tried it yet). I have absolutely no idea if it works and I have no chance to check it right now.

OTHER TIPS

Not sure if this'll help but WickedPDF is a good alternative to Prawn.

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