Making PDFs with Prawn

In my class, we recently spent a few hours working on a SOA (service-oriented architecture) assignment. Our task was to create a resume app that would allow a user to log in and view their resume. The data for the resume is pulled from an API. We were divided up into 5 teams and given different assignments so we could all make different apps to make the whole app working.

My team was in charge of making the pdf.

To make a pdf in Rails, there is a gem called Prawn. Installing the gem was easy enough (just follow the instructions on the page), but implementing it was a little harder.

A lot of the online blogs and tutorials on the gem were outdated, as a lot of them told you to use a prawnto gem that actually does not work with the latest update of Prawn. The most useful tutorial I found was this video:

[youtube https://www.youtube.com/watch?v=e-A3zBeWDdE]

It worked for the most part, but we still had trouble implementing it completely. So the following is my tutorial for it.

Warning: this is an incomplete tutorial, as it only works by putting all of your text in your controller. If I return to this gem, I will post an update.

Step 1: Add the gem to your Gemfile and bundle install.

Step 2: Update your controller in your rails app. Here is what mine ended up looking like:

Screen Shot 2015-12-22 at 5.22.32 PM

In this app, I am making my show page show a pdf. I am pulling in data from an API that my classmates made (the API is hosted in localhost:3000 and my app is hosted in localhost:3001). I set a variable for the data I am getting from the json file.

Now, this controller acts as both the controller for any HTML show pages you make, and any PDF pages you make. In order for your controller to be able to distinguish between the html and the pdf page. That is why you use the

respond_to do |format|

line.

As you can see from the above example, if it is an html page, there are no extra steps. This is because it will then show the show page you should have made in your controller.

If the file type is a pdf, you have to do some extra steps. (In a perfect world, this is where you would reroute it to a separate pdf page, but I could not get mine to work. If you want to see a working example, see the above video).

First, set a variable to equal

Prawn::Document.new

This is the code that creates a new document with the prawn gem. I called my variable pdf since we’re making a pdf, but you could call it whatever you wanted (i.e. purple hippo).

The following lines are where you add the text. You add a “.text” to the end of your variable, followed by whatever text you want to add.

The last line is where you send the data to be rendered. You add the type of file you want it to render to by (so in this case, you want it to be a pdf). You add disposition: “inline” because this will have your pdf open in the browser. If you do not add a disposition, it will download the pdf by default.

That’s a basic tutorial of Prawn. If I continue to learn how to use this gem, I will update this post. If you have any questions, feel free to leave a comment!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.