ktulasi
Joined: 21 Oct 2009 Posts: 55
|
Posted: Mon Feb 01, 2010 5:49 am Post subject: Pdf generation in rails |
|
|
Its very simple to generate pdf in rails,
First install the pdf writer gem as, gem install pdf-writer
then,
place the 'require pdf/writer' in controller by this we can get all the methods in that gem and
in the method create a PDF::Writer object
as pdf = PDF::Writer.new.
by the variable pdf we can implement methods where we requie.
place the outputted text as
pdf.text("example")
and place the two lines of code where to save the file and from where it should be outputted as
pdf.save_as("public/pdf/summary_report.pdf")
send_file "public/pdf/summary_report.pdf", :type => 'application/pdf', :disposition => 'attachment'.
That's it.
example as,
In method, create a new object, as
pdf = PDF::Writer.new.
here we have created an object and assigned to a variable pdf.We can apply colours,font styles,lines.
pdf.select_font "Times-Roman"
font style can be applied in this way.
pdf.text "Hello World",:font-size => 72,:justification => :center
A text with a font size and aligning it to center.
x=pdf.absolute_left_margin
y=pdf.absolute.bottom_margin
pdf.add_text(x,y,"Hello World",70,45)
alligns text at our required position.
pdf.start_page_numbering(pdf.absolute_right_margin, pdf.absolute_bottom_margin, 10, nil, nil, 1)
Numbering a page at absolute right and at absolute bottom, with 10 as font size and numbering starts from 1.
pdf.fill_color Color::Green
assigns colours to the text.
pdf.start_columns 4
pdf.text("Totalcalls",:font_size => 10,:left => 1)
pdf.start_new_page
pdf.text(@calls.size.to_s,:font_size => 10, :right =>1)
pdf.stop_columns
alligns output, here Total calls and its size in columns..Here start_new_page aligns the size in another column.
pdf.text ""
Which leaves an space between lines.
pdf.line(pdf.absolute_left_margin, pdf.y + 8, pdf.absolute_right_margin, pdf.y + .stroke
alligns a line at a given postition.
pdf.save_as("public/pdf/summary_report.pdf")
send_file "public/pdf/summary_report.pdf", :type => 'application/pdf', :disposition => 'attachment'
at last the file can be saved at a desired position as .pdf file.
Thank you. |
|