Rails, PDF + prawn jumpstart

Looking for a “to-PDF” solution for your rails application? Well, if your journey is (has been) anything like mine [which is pretty standard judging from what i’ve read] then you’ve probably decided prawn is the way forward. And you’re also probably convinced that prawnto is a good idea (it is also highly recommended).

So by now you’ve created a blank rails app, installed the gem, added the plugin, got a controller of sorts setup- all for the purposes of test-driving prawn (with prawnto). You’ve reread the documentation but it’s not going _that_ smoothly. There are one or two little things (not mentioned as explicitly as you’d might want) that you should be aware of. After trawling through some prawn discussions on google groups, i picked a bright penny-moment. Aha! Of course 🙂

Sometimes, while learning a new library/tool/tech/whatever, your brain seems to focus too hard on the problems you’re experiencing and forgets about the basics of the bigger context. When you step away, you (hopefully) realise that the issue you’re having is not the library/tool/tech/whatever, but with the fact that some fundamental got glossed over/ignored/forgotten about. This was one of those.

My controller code was fine. The actions were pretty much {empty}. My view filename was either action.pdf.prawn, action.pdf, action.prawn.pdf, action.prawn or actionpdf.prawn- all sorts of variations ‘cos i couldn’t consistently get the desired effect: an inline pdf render. But the documentation said quite clearly ‘.pdf.prawn’. What does it take to get a simple demo going to start experimenting with? Well… it turns out all i had wrong was the request. I completely ignored the basics (routing) and focused on the problem (pdf library). Not exactly a library issue.

http://localhost:3000/controller/action won’t work => renders .html
http://localhost:3000/controller/action.pdf won’t work either => no route

What i wanted to request was: http://localhost:3000/controller/action/:id.pdf
and :id is the identifier for your model (even if you’re not actually riding with one in our test drive).
And this is due to the default configuration of a default rails app. Check out your routes.rb. Back to basics. Evidently, if you want a different ‘route’ to get to your pdf, then, you guessed it: create a route 🙂