//DEVGURU

Author's entries:

Truncate string preserving only full words (Ruby, RoR)

Tuesday, December 15th, 2009

Easy method to cut string to a defined length without leaving words cut in half.

class String
  def truncate_full_words limit = 64, completion = "..."
    self.length > limit ? "#{self[0..(self.rindex(' ', limit) || limit)-1]}#{competion}" : self
  end
  def truncate_full_words! limit = 64, completion = "..."
    self.replace(self.truncate_full_words limit, completion)
  end
end

Usage:

>> some_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec enim nibh."
=> "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec enim nibh."
>> some_string.truncate_full_words
=> "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ..."

Have fun using it :)

How to convert doc/xls/ppt/… to pdf in Unix console

Wednesday, November 18th, 2009

Today we needed to make a tool to convert Microsoft Office (and preferably other formats) documents to PDF from a Rails application. We have come up with such solution – install OpenOffice.org, CUPS and CUPS-PDF (virtual PDF printer for CUPS) and run it like this:

soffice -headless -pt Cups-PDF file.doc

This command executes OpenOffice.org in so-called ‘headless’ mode – meaning that it will not run anything graphical. It reads the file.doc, and prints it using CUPS-PDF virtual printer. The resulting PDF can be found (by default) in ~/PDF directory.