Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Aug 21, 2009

_why?

It really doesn't matter what his name is, what I know is I read his book, use his projects and admired him.

Be good - Why The Lucky Stiff.

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.

Sep 18, 2008

[RUBY] Formating numbers with commas

I want to format the numbers with commas, there maybe a better way to do it, but this one works for me:

  # Return String formated as "123,456.78"
def format_number(number)
  s = "%0.2f" % number
  s =~ /([^\.]*)(\..*)?/
 int, dec = $1, $2
 int.gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,') + dec
end