I’ve had the opportunity to spend the last few days working with the amazing Prawn Ruby gem and the equally stellar Prawnto Rails plugin., which allow you to dynamically create PDF files from a Ruby on Rails application. It’s elegant, fun, and stupid simple, and since I’ve never written a tutorial before, these libraries inspired me to crank one out.
In this tutorial we’re going to build an app together that takes user submissions for a cover letter, and their previous job title and description to print out for job candidates.
So let’s create a new Rails app.
Open up terminal and let’s do this thing. I’m calling my app “resume.”
rails resume
Great. Now let’s use Rails scaffolding to create the foundation for our app.
Change into your app’s directory:
cd resume
and let’s get our scaffold on.
For our purposes, we’re going to have a form asking the user to paste in their cover letter, type the name of their previous employer, and a description of the work they did there.
./script/generate scaffold Submission coverletter:text previousjob:string description:text
Remember to migrate your database and we’ll run the development server to be sure everything’s working.
rake db:migrate
ruby script/server
Now if you navigate to http://localhost:3000/submissions you should see a very basic looking page to start working with.
Awesome, now let’s open up our IDE and do some configuring. The first thing to note is that the 1-2 punch of Prawn and Prawnto will not currently work with Rails 2.2. I’m sure support is just around the corner, but just for now to be safe, let’s open our environment.rb file and roll back to Rails 2.1.
Let’s hop back into terminal and install Prawn and Prawnto.
sudo gem install prawn
./script/plugin install git://github.com/thorny-sun/prawnto.git
And open resume/config/environment.rb again and configure the Prawn gem dependency:
And here I always migrate my database once more to make sure things are working.
Awesome. Now prawn and Prawnto are installed and we can start kicking ass.
Let’s open up the Submissions controller in app/controllers/submissions_controller.rb.
For this tutorial, we want to have PDF views for both the list of all submitted resumes as well as a PDF for each individual one. We’ll do this by editing both the index and show methods in the controller to render PDF. Luckily with Prawn and Prawnto this is as simple as rendering any other format.
We simply type:
format.pdf { render :layout => false }
In the respond_to block so the index method looks like this:
And the show method looks like this:
Now let’s move on to creating our PDF views. For the index view, create a file in app/views/submissions called “index.pdf.prawn,” and for the show view a file called “show.pdf.prawn” in the same directory.
First, let’s just make sure it works. In index.pdf.prawn, simply type:
pdf.text "Hello World!"
and save the file.
Now, if you navigate your browser to http://localhost:3000/submissions.pdf, you should get the familiar PDF browser action:
And the resulting PDF should read as you might expect:
Awesome. So now let’s customize it to our app.
In app/views/submissions/index.pdf.prawn:
And in app/views/submissions/show.pdf.prawn:
Sick. Now fill in some dummy information to your app and navigate to: http://localhost:3000/submissions.pdf which should print a PDF of all submissions, and http://localhost:3000/submissions/1.pdf which will print out a PDF of the submission with the ID 1.
Congratulations!
From here there’s a million directions you can go:
Exercises:
Beginner: Trick your users into submitting false resumes. Use their submitted data to print out Madlibs-style resumes and cover letters.
Intermediate: Use a Rails plugin like Paperclip to allow user photo uploads and print the images to a PDF.
Advanced: Work with the Prawnto team to patch the amazing plugin to Rails 2.2!











