Monday 23 December 2013

How To Generate PDFs in Rails With Prawn(ruby 1.9.3 and rails 3.2)

 way 1  
 For creating PDFs i refereed these links(http://www.railstips.org/blog/archives/2008/10/13/how-to-generate-pdfs-in-rails-with-prawn/, http://stackoverflow.com/questions/8641941/rails-plugin-install-git-github-com-thorny-sun-prawnto-git-is-not-working-in)  
 I found few updates have to do on it. So writing this blog. Found any correction comment down without fail.   
 ruby - 1.9.3  
 rails - 3.2  
 create new project in rails  
 rails new prawn_sample  
 you can find gem file inside project folder, add prawn gem.  
 gem 'prawn'  
 gem "prawnto_2", :require => "prawnto"  
 then bundle install  
 now, install prawnto plugin  
 rails plugin install git@github.com:forrest/prawnto.git  
 For sample data to display, let’s create a Book model with title,author and description.  
 rails generate scaffold book title:string author:string description:text  
 then  
 rake db:migrate  
 now start the server and enter sample data's to test   
 so,   
 rails s  
 localhost:3000/books  
 here enter the required amount of data's  
 next  
 Let’s get started with something simple and add a pdf version of the show action. Open up the books controller and add format.pdf  
 def show  
  @book = Book.find(params[:id])  
  respond_to do |format|  
   format.html # show.html.erb  
   format.xml { render :xml => @book }  
   format.pdf { render :layout => false }  
   end   
 end  
 create the show.pdf.prawn file inside app/views/books. For now, lets have hello world  
 pdf.text "Hello World!"  
 visit  
 http://localhost:3000/books/1  
 http://localhost:3000/books/1.pdf  
 you successfully generated PDF.  
 Let’s make the view specific to the books.  
 in show.pdf.prawn  
 pdf.font "Helvetica"  
 pdf.text "Book: #{@book.title}", :size => 16, :style => :bold, :spacing => 4  
 pdf.text "Author: #{@book.author}", :spacing => 16  
 pdf.text @book.description  
 now you see some text with format specified above . Browse more for format you required.  
 way 2  
 Reference (http://stackoverflow.com/questions/8658302/unable-to-render-pdf-to-browser-using-prawn-pdf-for-ruby-on-rails)  
 Gemfile  
 gem 'prawn'  
 /config/initializers/mime_types.rb  
 Mime::Type.register "application/pdf", :pdf  
 AuditsController  
 def show  
   @audit = Audit.find(params[:id])  
   respond_to do |format|  
      format.html  
      format.pdf do  
        pdf = Prawn::Document.new  
        pdf.text "This is an audit."  
        send_data pdf.render, type: "application/pdf", disposition: "inline"  
      end  
    end  
  end  

Friday 18 October 2013

Python code to crawl the web page

 import thread  
 from lxml import html  
 import requests  
 import thread  
 tocrawl = ["link","link"]  
 def print_def( threadName):  
   page = requests.get(crawling)  
   tree = html.fromstring(page.text)  
   text = tree.xpath('//item')  
   print text  
   f = open('myfile','a+')  
   f.write(str(text)+"/n")  
   f.close  
 try:  
    for crawling in tocrawl:  
    thread.start_new_thread( print_def, (crawling,) )  
 except:  
   print "Error: unable to start thread"  
 while 1:  
   pass  

simple html with javascript validation

 <html>  
 <script>  
 function validation()  
 {  
 //first name  
 var a = document.form.firstName.value;  
  if(a=="")  
     {  
     alert("Please Enter Your First Name");  
     document.form.firstName.focus();  
    return false;  
     }  
   if(!isNaN(a))  
 {  
 alert("Please Enter Only Characters");  
 document.form.firstName.select();  
 return false;  
 }  
   if ((a.length < 5) || (a.length > 15))  
 {  
 alert("Your Character must be 5 to 15 Character");  
 document.form.firstName.select();  
 return false;  
 }  
 //last name  
 var a = document.form.lastName.value;  
   if(a == "")  
 {  
 alert("Please Enter Your Last Name");  
 document.form.lastName.focus();  
 return false;  
 }  
   if(!isNaN(a))  
 {  
 alert("Please Enter Only Characters");  
 document.form.lastName.select();  
 return false;  
 }  
 //gender validation  
   if(document.form.gender.value == "-1")  
 {  
 alert( "Please provide your Gender!" );  
     return false;  
 }  
 //address  
 var c = document.form.address.value;  
   if(c == "")  
 {  
 alert("Please Enter Your Address");  
 document.form.lastName.focus();  
 return false;  
 }  
 //cell  
 var d = document.form.cell.value;  
   if(d == "")  
 {  
 alert("Please Enter Your cell num");  
 document.form.lastName.focus();  
 return false;  
 }  
   if(isNaN(d))  
 {  
 alert("Please Enter numbers not characters");  
 document.form.lastName.select();  
 return false;  
 }  
   if (d.length < 10)  
 {  
 alert("Plz enter correct cell number");  
 document.form.firstName.select();  
 return false;  
 }  
 //country  
   if(document.form.country.value == "-1")  
 {  
 alert( "Please select your Country!" );  
     return false;  
 }  
 }  
 </script>  
 <head>  
 </head>  
 <body>  
 <form name="form" method="post" onsubmit="return validation()">  
 <table width="100%" height="100%" bgcolor="#E0F8F7" align="center">  
   <tr>     
     <td colspan=3 ><font size=3 color=red><right><b>USER ADDITIONAL INFORMATION FORM</b></right>     
   </tr>  
   <tr>  
     <td width="30%">First Name:<br/></td>  
     <td><input type="text" size=25 maxlength=20 name="firstName"></td>  
   </tr>  
   <tr>  
     <td width="30%">Last Name:<br/></td>  
     <td><input type="text" size=25 maxlength=20 name="lastName"></td>  
   </tr>  
   <tr>  
     <td width="30" valign="middle">Gender:</td>  
      <td><select name="gender">  
        <option value="-1" selected>---Select---</option>  
        <option value="1">Male</option>  
        <option value="2">Female</option>  
        </select>  
      </td>  
   </tr>  
   <tr>  
     <td width="30%">Adress:<br/></td>  
     <td><textarea rows=3 colos=30 name="address"></textarea></td>  
   </tr>  
   <tr>  
     <td width="30%">Cell no:<br/></td>  
     <td><input type="text" size=25 maxlength=10 name="cell"></td>  
   </tr>  
   <tr>  
     <td width="30" valign="middle">Select Country :</td>  
      <td><select name="country">  
        <option value="-1"selected>---Select---</option>  
        <option value="1">India</option>  
        <option value="2">Country</option>  
        <option value="3">Country</option>  
        <option value="4">Country</option>  
        <option value="5">Country</option>  
        <option value="6">Country</option>       
       </select></td>  
   </tr>  
   <tr>  
     <td ><input type="Reset">  
     <td ><input type="submit" value="Submit">  
   </tr>  
 </table>  
 </form>  
 </body>  
 </html>