3

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.

Community
  • 1
  • 1
miccet
  • 1,870
  • 3
  • 19
  • 26
  • One idea I had when thinking a bit about it was to generate a bunch of PDF objects. One for each Company like I do now when doing a single at a time and then mash them all together and export that. Will try that now. – miccet May 24 '11 at 12:07

2 Answers2

3

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.

Miki
  • 7,052
  • 2
  • 29
  • 39
  • I don't know how many pages a company will take up each. Usually it's 2, but can also be 3, it's not known and a big part of this problem. – miccet May 24 '11 at 12:06
  • I will accept this as the answer since it will eventually solve my issue. Right now I'm on version 0.8.4 which seems to ignore the :template option completely while the latest 0.11.1 can handle it. Problem is there are a lot of changes to the code so I will have to recode a lot of the old working stuff. Cheers! – miccet May 24 '11 at 20:07
  • Thanks! I had gone through 8.4 -> 11.1 change last weekend, however I needed only to change creation of tables. Everything else stayed unchanged, but I was only generating one page of text (an invoice), so there was very little code to modify anyway ;) – Miki May 24 '11 at 20:11
  • Yes, same experience here after updating the code tonight, with some minor changes to positioning. It was easy to correct some numbers on the move up and down functions though. – miccet May 24 '11 at 21:41
-1

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

Yule
  • 9,668
  • 3
  • 51
  • 72
  • I started off with that solution, but due to server crashes using it we dumped it. – miccet May 24 '11 at 12:04
  • Provided that you want to install all the stuff that it depends on. Prawn is (sometimes very) far from being perfect, but it is pure Ruby. – Miki May 24 '11 at 12:05
  • I've had great success with it though and it keeps amazing me with the feature set. This is just a hard nut to crack. – miccet May 24 '11 at 12:13