Getting error PDF could not generated in Rails 3

走远了吗. 提交于 2019-12-25 03:34:33

问题


I am getting the following error while converting the HTML file to PDF using wicked_pdf gem in Rails 3.

error:

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

Please check my below code.

users/index.html.erb:

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

users/download_pdf.pdf.erb:

<h1>Hello pdf</h1>

controller/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'

I installed wkhtmltopdf by downloading from its site and set the path in wicked_pdf.rb file.But i am getting this type of error.I am using Rails version 3.2.19 and win-xp.Please help me to resolve this issue.


回答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}"
}

Add this code to config/initializers/wicked_pdf.rb

and then try

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



回答2:


Find wicked_pdf.rb in path/to/gem/wkhtmltopdf/bin, on line 64 replace file:// with file:/// (you can read why at https://github.com/mileszs/wicked_pdf/issues/157). If it still didn't work out rename the file download_pdf.pdf.erb to download.erb. It works fine for me :)



来源:https://stackoverflow.com/questions/30450268/getting-error-pdf-could-not-generated-in-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!