使用 Rails 得到错误的数字参数错误

getting wrong number argument error using Rails

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

error:

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后link出现以下错误。

error-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

users/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.Please 帮助我解决此错误并成功获取 PDF 文件。

假设您正在尝试下载 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'

请检查是否有效。