开发者

How do I generate pdf file using Prawn in Rails?

I have used the following syntax to generate a pdf file:

Prawn::Document.generate("#{RAILS_ROOT}/public/pdf/generate.pdf") do          开发者_运维知识库                                           
  pdf.text "hello"
end

But when I look for the file within the /public/pdf/ folder, I dont find any file.

Well my controller code for this is

def generate
  respond_to do |format|                                                                                               
    format.pdf{ render :file => "#{RAILS_ROOT}/public/pdf/generate.pdf"}
  end
end


It could be something else, but the code supplied doesn't seem to work:

Prawn::Document.generate("x.pdf") do
  pdf.text "Hello"
end

gives me

NameError: undefined local variable or method `pdf' for #<Prawn::Document:0x352b6e4>
        from c:/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.8.4/lib/prawn/graphics/color.rb:72:in `method_missing'
        from (irb):3

You should be able to see if you're getting something similar in your log file.

I think the pdf. is not needed:

Prawn::Document.generate("x.pdf") do
  text "Hello"
end


ruby  - 1.9.3
rails - 3.2

way 1

create new project in rails

rails new prawn_sample

you can find gem file inside project folder, add prawn gem.

gem 'prawn'
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

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


Not sure what version of Prawn you're using, but this worked for me:

    pdf = Prawn::Document.new(background: img, :page_size => "LETTER", :page_layout => :landscape)
    pdf.render_file File.join(Rails.root, "app/pdfs", "x.pdf")

I think you're missing the pdf.render_file line that actually creates the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜