我使用以下语法生成pdf文件:
Prawn::Document.generate("#{RAILS_ROOT}/public/pdf/generate.pdf") do
pdf.text "hello"
end
但是当我在/ public / pdf /文件夹中查找文件时,我找不到任何文件。
我的控制器代码是
def generate
respond_to do |format|
format.pdf{ render :file => "#{RAILS_ROOT}/public/pdf/generate.pdf"}
end
end
答案 0 :(得分:3)
它可能是别的东西,但提供的代码似乎不起作用:
Prawn::Document.generate("x.pdf") do
pdf.text "Hello"
end
给了我
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
你应该能够看到你的日志文件中是否有类似内容。
我认为不需要pdf.
:
Prawn::Document.generate("x.pdf") do
text "Hello"
end
答案 1 :(得分:0)
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
答案 2 :(得分:0)
不确定您使用的是哪个版本的Prawn,但这对我有用:
pdf = Prawn::Document.new(background: img, :page_size => "LETTER", :page_layout => :landscape)
pdf.render_file File.join(Rails.root, "app/pdfs", "x.pdf")
我认为您错过了实际创建文件的pdf.render_file
行。