如何使用 Rails 中的 Prawn 生成 pdf 文件?

发布于 2024-09-26 03:03:50 字数 508 浏览 2 评论 0原文

我使用以下语法生成 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惜醉颜 2024-10-03 03:03:51
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 [email protected]: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
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 [email protected]: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
糖果控 2024-10-03 03:03:51

不确定您使用的是什么版本的 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 行。

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.

自演自醉 2024-10-03 03:03:50

它可能是其他东西,但提供的代码似乎不起作用:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文