Thursday, November 29, 2007

Calculating Time difference in Ruby

require 'date'
now = DateTime.new
date = Date.new
p now.strftime("%Y-%m-%d %I:%M:%S").to_s
p Time.now.strftime("%Y-%m-%d %I:%M:%S").to_s
dif = DateTime.parse(Time.now.strftime("%Y-%m-%d %I:%M:%S").to_s)-DateTime.parse("2007-9-12 14:07:00")
hours, mins, secs, ignore_fractions = Date::day_fraction_to_time(dif)
p hours * 60 * 60 + mins * 60 + secs

Tuesday, November 20, 2007

Pdf-writer -- Images in simple table

When using pdf-writer using ruby programming language, simple table always gives option of using text in the rows. I did not have an option to use images inside simple table as it uses data as

attr_accessor :data
# An array of Hash entries. Each row is a Hash where the keys are the
# names of the columns as specified in #column_order and the values are
# the values of the cell.

The following is the code change in simpletable.rb file is required to use images in respected cells.

line : 647
if(line.match("::Image"))
pic = line.gsub("::Image","")
line = pdf.add_image_from_file(pic,pos[name], pdf.y-5, @image_width)
line =""
else
line = pdf.add_text_wrap(pos[name], pdf.y,
max_width[name], line,
@font_size, just)
end


Note: Make sure that text should have "::Image" and remaing text is the path of the image .

like

data[i]["Q1Image"] ="::Image"+Dir.getwd+"/public/images/status_#{p.status_q1_image}.jpg"

For more information please post your comments.