【问题标题】:Getting error PDF could not generated in Rails 3在 Rails 3 中无法生成错误 PDF
【发布时间】:2015-05-26 05:39:26
【问题描述】:

在 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
}

宝石文件:

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'

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

【问题讨论】:

  • 尝试在 Rails 控制台中运行“WickedPdf.new”。
  • @ user123 : 它给出了这个结果 #<0x309a668 files opdf.exe ::>

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


【解决方案1】:
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>') 

【讨论】:

  • @user123 : 我正在使用 win-xp
  • 在命令提示符下尝试“bundle show wkhtmltopdf”命令。如果它为您提供了路径,则复制该路径并将其粘贴到 config/initializers/wicked_pdf.rb
  • @ user123 :我在 wicked_pdf.rb 中添加了您的行。但是我为控制台运行 rails c 它作为未定义的局部变量或方法'arch'给出错误
  • @ user123 :它抛出错误为错误:无法执行:[“C:/Ruby193/lib/ruby/gems/1.9.1/gems/wkhtmltopdf-binary-0.9.9.3”, "file://C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf20150526-3672-1arfcgp.html", "C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf_generated_file20150526 -3672-1vuo536.pdf"] 错误:权限被拒绝 - C:/Ruby193/lib/ruby/gems/1.9.1/gems/wkhtmltopdf-binary-0.9.9.3
【解决方案2】:

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。它对我来说很好:)

【讨论】:

    猜你喜欢
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    相关资源
    最近更新 更多