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

Monday, May 16, 2011

Reading PDF and converting to CSV using RubyCode & pdf-reader plugin

I wanted to convert a PDF document into a XLS table and after a couple of searches I could easily able to write the code in ruby and converted a Citibank PDF Statement to CSV file. This gave me enough relief that I learnt how to read a PDF file if its not password protected.

require 'rubygems'
require 'pdf/reader'
class PageTextReceiver
  attr_accessor :content

  def initialize
    @content=[]
    @kk = "false"
    @i = 0
    @ptr_str = ""
  end

  def begin_page(arg=nil)
    puts ""
  end

  def show_text(string, *params)
    if string.strip=="Previous Balance"
      @kk="false"
    end
    if @i==4
      puts @ptr_str
      @ptr_str = ""
      @i=0
    end
    if string.strip=="Sale Date" or @kk == "true"
      @kk="true"
      if @i==0
        @ptr_str << string + "/2009,"
      else
        @ptr_str << string + ","
      end
      if (string.reverse.index(".")==2 or string=="Amount (in Rs)")
        @i=4
      else
        @i=@i+1
      end
    end
  end

  def move_to_next_line_and_show_text
    @i=0
    show_text
  end

  alias :super_show_text :show_text
  alias :set_spacing_next_line_show_text :show_text

  def show_text_with_positioning(*params)
    params=params.first
    params.each { |str| show_text(str) if str.kind_of?(String) }
  end
end

receiver = PageTextReceiver.new
(1..45).each do | x |
  pdf = PDF::Reader.file("#{x}.pdf", receiver)
  puts receiver.content.inspect
end

The above code use to read 45 pdf files. Say the above code is saved in read_pdf.rb

Below is the command to execute the file and store in a file (which I hope the easiest way)
ruby read_pdf.rb >> a.csv
hope it helps you ? or If you know how to read a PDF which is password protected thru code, where I can input the password of the file, please let me know.

Thursday, August 12, 2010

undefined method `stringify_keys!' for + attachment_fu


Today me & my friend were working on a simple attachment_fu plugin with the Rails. We had some trouble which we solved it too.

We followed http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu to make the attachment_fu work with our application. Basically we have a company table & attachment table too which has a relation in between. Company table contains the list of companies & Attachment table contains the attachments for the individual companies.

The model looks like this.
class Company < ActiveRecord::Base
  has_one :attachment
  has_many :books
end


class Attachment < ActiveRecord::Base
  belongs_to :company
  has_attachment :content_type => ['application/zip']
  has_attachment :storage => :file_system, :path_prefix => 'public/files'
  validates_as_attachment
end

the view file looks like this

<% form_for(:mugshot, :url => "/companies", 
                  :html => { :multipart => true }) do |f| -%>
<!-- Form -->
<div class="form">
   <label>Company Title <span>(Required Field)</span></label>
   <input type="text" name="company_title" class="field size1" />
   <label>Upload the zip file <span>(Required Field)</span></label>
   <%= f.file_field :uploaded_data %>
</div>
<!-- End Form -->
<div class="buttons">
 <input type="button" class="button" value="[cancel]" />
 <input type="submit" class="button" value="[upload now]" />
</div>
<!-- End Form Buttons -->
<% end %>

The company_controller.rb looks like this.

def create
  company = Company.create(:title => params[:company_title], :from => params[:from_date], :to => params[:to_date], :comment => params[:comment])
  @zipfile = Attachment.new(params[:uploaded_data])
  @zipfile.company_id = company.id
  if @zipfile.save
     flash[:notice] = 'Company successfully created.'
     redirect_to :controller => "books", :action => "new"
   else
     flash[:error] = 'You must select a file to upload first!'
     company.destroy
     render :action => :new
   end
end

But strangely we got the error saying..
undefined method `stringify_keys!' for
which got rectified based on the changing one line in the view file, from
   <%= f.file_field :uploaded_data %>
to
   <%= file_field :attachment, :uploaded_data %>
and also update the line in company_controller.rb from
  @zipfile = Attachment.new(params[:uploaded_data])
to
  @zipfile = Attachment.new(params[:attachment])


This made it work properly. Hope this helps you too, else let me know and will try to help you out.

Monday, August 2, 2010

Existing PHP / Ruby not working with MySQL installed with 'sudo port install'

By Default OSX 10.x contains PHP & Ruby installed already. I wanted to make work few applications which used MySQL, PHP & Ruby. So to do this, I installed Xcode & MacPorts too. Then installed mysql using 'sudo port install' and installed successfully. But when I executed the applications, I found that neither PHP nor Ruby could know which mysql.sock can be used to make the existing application work. After a deep search found that port install uses different location and stores the mysql socket. I tried to edit the php.ini file to make sure that it gets connected to the existing socket, but still it didn't worked.

When executed the PHP code I got this warning "Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /Users" but without this solved I can't connect to DB and get the code working.

When I tried with Ruby also it gave me some weird error "ruby `initialize`: wrong number of arguments (4 for 0) (ArgumentError)" and I got frustrated little bit.

So instead of debugging more I thought I can remove the existing PHP & Ruby and install them using the port install command. So I followed the few steps like mentioned below:

sudo mv /System/Library/Frameworks/Ruby.framework/Versions/1.8 /System/Library/Frameworks/Ruby.framework/Versions/1.8.dead

sudo port install ruby
sudo port install rb-rubygems

sudo mv /usr/bin/ruby /usr/bin/old_ruby
sudo mv /usr/bin/gem /usr/bin/old_gem
sudo ln -s /opt/local/bin/ruby /usr/bin/ruby
sudo ln -s /opt/local/bin/gem /usr/bin/gem

sudo gem update --system

sudo gem install xml-simple
sudo gem install mysql

sudo port install php5
sudo port install php5-mysql
sudo port install php5-mbstring

sudo cp /opt/local/etc/php5/php.ini-production /opt/local/etc/php5/php.ini

locate mysqld.sock
==> /opt/local/var/run/mysql5/mysqld.sock (mostly this will be)

sudo vim /opt/local/etc/php5/php.ini

and change all mysql & mysqli default_socket = /opt/local/var/run/mysql5/mysqld.sock

sudo mv /usr/bin/php /usr/bin/old_php

The above commands will rename the existing PHP & Ruby and uses the new Ruby & PHP installed using the port install commands. After updating the php.ini my existing code worked perfectly as though there were no issues. Even the Ruby also didn't gave me any error, even that too worked fantastically.

I hope this will help you to resolve your problem easily rather than debugging the issue which might eat up your time.

Friday, July 30, 2010

Uninstall Ruby / PHP in existing Mac OS

Hi Guys, posting this after a long period as was little busy ;-). Anyways lets get into the subject.

Whenever OSX 10.x version installed, by default PHP / Ruby will also be installed. This is good but sometimes we don't want the particular version installed so to remove this we need to do the following steps.

For Ruby
sudo mv /System/Library/Frameworks/Ruby.framework/Versions/1.8 /System/Library/Frameworks/Ruby.framework/Versions/1.8.dead
sudo mv /usr/bin/ruby /usr/bin/old_ruby
sudo mv /usr/bin/gem /usr/bin/old_gem

For PHP
sudo mv /usr/bin/php /usr/bin/old_php