在Rails 3中无法生成错误PDF

时间:2015-05-26 05:39:27

标签: ruby pdf ruby-on-rails-3.2 wicked-pdf

使用Rails 3中的wicked_pdf gem将HTML文件转换为PDF时出现以下错误。

  

错误:

Error: Failed to execute:
["C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe", "file://C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf20150526-648-17uza61.html", "C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf_generated_file20150526-648-w6l9ye.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_pdf20150526-648-17uza61.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: ContentNotFoundError

请检查我的以下代码。

  

用户/ index.html.erb:

<p>
    <%= link_to "Download pdf",download_pdf_path(:format => 'pdf') %>
</p>
  

用户/ download_pdf.pdf.erb:

<h1>Hello pdf</h1>
  

控制器/ users_controller.rb:

class UsersController < ApplicationController
    def index

    end
    def download_pdf
    respond_to do |format|
        format.pdf{ render pdf: "/users/download_pdf.pdf.erb"}
    end 
    end
end
  

wicked_pdf.rb:

WickedPdf.config = {
  #:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf',
  #:layout => "pdf.html",
  :exe_path => 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
   #:exe_path => Rails.root.join('bin', 'wkhtmltopdf').to_s
}
  

的Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.19'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'

我通过从其网站下载并在wicked_pdf.rb文件中设置路径来安装wkhtmltopdf。但是我收到了这种类型的错误。我正在使用Rails版本3.2.19和win-xp.Please帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

require 'rbconfig'

if RbConfig::CONFIG['host_os'] =~ /linux/
  arch = RbConfig::CONFIG['host_cpu'] == 'x86_64' ? 'wkhtmltopdf_linux_x64' : 'wkhtmltopdf_linux_386'
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
  arch = 'wkhtmltopdf_darwin_386'
else
  raise "Invalid platform. Must be running Intel-based Linux or OSX."
end

WickedPdf.config = {
  exe_path: "#{ENV['GEM_HOME']}/gems/wkhtmltopdf-binary-#{Gem.loaded_specs['wkhtmltopdf-binary'].version}/bin/#{arch}"
}

将此代码添加到config / initializers / wicked_pdf.rb

然后尝试

WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>') 

答案 1 :(得分:0)

path/to/gem/wkhtmltopdf/bin中查找wicked_pdf.rb,在第64行将file://替换为file:///(您可以在https://github.com/mileszs/wicked_pdf/issues/157阅读原因)。如果仍然无效,请将文件download_pdf.pdf.erb重命名为download.erb。它适用于我:)