Jan 25, 2009

PDF generation with Prawn in Merb

In my company, the customer service representatives asked me to generate a PDF file format of the price letter for our customers, because those are relative sensitive files, which was used to be in M$ word format.

I started using HTMLDOC at first, which is fine if you've already had the HTML version. Really, I like it. It is pretty simple to set up and running. You do not need to care about too much about the layout and format of the file, because it is relatively easy to do that in a html.erb file. But there is a catch on production servers, if you have, like me, several different types of environment, Red Hat, Ubuntu, OS X, you really have to be care of the PATH to the binary htmldoc command. Otherwise the Phusion Passenger will throw out very strange error message.

After I came back from Ruby Conf 2008(if you were there, you problaly knew Gregory Brown, he held a hack session with Prawn.), so I decide to give Prawn a try.

Here are the steps( of cause within Merb):

1. Add mime type in your config file

2. In the view file, the form for the PDF request:

Please notice the :format => 'pdf'
3. In controller, provides the pdf format:


From here, you have two ways to send the PDF file:
In the controller/action

Or, in a pdf.erb file

Now, you should be able to get a PDF file.

UPDATE(Feb-23-2009):
Someone asked what if you using 'GET' instead of post?
Well, it is pretty simple, you can just add the format like:
resource(:ordering, @price_letter, :print, :format => :pdf)
I am hoping this is helpful.

Jan 17, 2009

jQuery: unobtrusive javascript form submit with Ajax in Merb.

I was working on a project this week. There are several forms on one page, I want to submit any form without leaving the page. At first, this requirement really bothered me for a few minutes, after reading some documents from jQuery and Merb, it turns out pretty easy to implement:

1. View - file which have the forms you want to submit
In your form.html.erb file, you can create them like the normal forms.

2. Javascript(jQuery)
Here is the javascript to do the unobtrusive form submit.

3. Controller
In your controller's action, you have to specify provides :js

4. View - file which does the render
This is the good part I like Merb, you can mix Ruby code and javascript together to operate the DOM.


It is pretty easy and handy.

Jan 10, 2009

Create PDF file from image files

Recently, I had to create a PDF file from a lot of image files. Usually, I have to open the image in Preview, then save it to a pdf file. Once I get all the pdf files, combine them together to create one file. It is OK if you are only dealing with a few, but I am facing thousands of them. Finally, I find the Automator( Come with OS X). It is super helpful. Although this is the first time I am working with it, I love it.
Step 1: Select finder items


Step 2: Convert and combine


Step 3: Save as a finder plug in

Jan 4, 2009

Elaine is 6 month now!

Here is the video(HD) I edited during the new year holiday.
I'm 6-month now! from Elaine Zuo on Vimeo."