使用Rails获取错误的数字参数错误

时间:2015-05-27 08:04:55

标签: ruby ruby-on-rails-3.2 pdfkit

您好我在Rails3中使用wicked_pdf gem时遇到以下错误。

  

错误:

ArgumentError in UsersController#download_pdf

wrong number of arguments (0 for 1)
Rails.root: C:/Site/generate4

Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:6:in `download_pdf'

点击下载pdf链接后会出现以下错误。

  

错误-2:

RuntimeError in UsersController#download_pdf

Error: Failed to execute:
["C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe", "--encoding", "UTF-8", "file://C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf20150527-1192-1qf0ac.html", "C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf_generated_file20150527-1192-fijfxt.pdf"]
Error: PDF could not be generated!
 Command Error: Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
Error: Failed loading page file://c/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf20150527-1192-1qf0ac.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: ContentNotFoundError

请检查下面的代码。

  

users_controller.rb:

class UsersController < ApplicationController
    def index

    end
    def download_pdf
        pdf=WickedPdf.new.pdf_from_string(
         render_to_string pdf: "test.pdf", template: "users/test.html.erb", encoding: "UTF-8")

        @save_path = 'C:\Site\download_pdf.pdf'
    end
end
  

用户/ test.html.erb:

<h1>Hello rails</h1>
  

wicked_pdf.rb:

WickedPdf.config = {
  #:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf',
  #:layout => "pdf.html",
  :exe_path => 'C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe'
}
  

的Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.19'

gem 'sqlite3'
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

gem 'wicked_pdf'

我的要求是使用Rails将HTML转换为PDF 3.请帮我解决此错误并成功获取PDF文件。

1 个答案:

答案 0 :(得分:2)

假设您正在尝试下载pdf文件。

请参阅以下代码:

#users_controller.rb:
class UsersController < ApplicationController
  def download_pdf
    pdf = render_to_string(pdf: "test.pdf", template:  "users/test.html.erb", encoding: "UTF-8")
    send_data pdf
  end
end

# Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.19'

gem 'sqlite3'
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

gem 'wicked_pdf', :github => 'mileszs/wicked_pdf', :branch => 'master'

请检查它是否有效。