2012-06-27 80 views
0

我已經安裝了PDFKit和wkhtmltopdf。我用釀造安裝wkhtmltopdf,和PDFkit,我把它添加到我的寶石文件,並創建了以下:PDFkit Gem不能在導軌中工作

# config/initializers/pdfkit.rb 
PDFKit.configure do |config| 
    config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf' 
    config.default_options = { 
    # :page_size => 'Legal', 
    # :print_media_type => true 
    } 
    # config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server. 
end 

以及添加以下內容application.rb中

require 'pdfkit' 
config.middleware.use PDFKit::Middleware 

當我運行which wkhtmltopdf我得到:/usr/local/bin/wkhtmltopdf

我也可以運行wkhtmltopdf google.com google.pdf,它會正確下拉文件。

我甚至可以做一些像wkhtmltopdf http://localhost:3000/about google.pdf,它工作得很好。

但是,如果我嘗試去做:http://localhost:3000/about.pdf,並且只是在我的瀏覽器中去那裏,整個網站只會嘗試加載並且永遠不會結束。 不過,我得到以下幾點:

RuntimeError (command failed: "/usr/local/bin/wkhtmltopdf" "--quiet" "-" "-"): 
    pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:71:in `to_pdf' 
    pdfkit (0.5.2) lib/pdfkit/middleware.rb:21:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' 
    rack (1.4.1) lib/rack/etag.rb:23:in `call' 
    rack (1.4.1) lib/rack/conditionalget.rb:25:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/head.rb:14:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/flash.rb:242:in `call' 
    rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' 
    rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/cookies.rb:338:in `call' 
    activerecord (3.2.3) lib/active_record/query_cache.rb:64:in `call' 
    activerecord (3.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:467:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' 
    activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `_run__1600846312864942852__call__3683691454379641054__callbacks' 
    activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback' 
    activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' 
    activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks' 
    actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/reloader.rb:65:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
    railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app' 
    railties (3.2.3) lib/rails/rack/logger.rb:16:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
    rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.4.1) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.4.1) lib/rack/lock.rb:15:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call' 
    railties (3.2.3) lib/rails/engine.rb:479:in `call' 
    railties (3.2.3) lib/rails/application.rb:220:in `call' 
    rack (1.4.1) lib/rack/content_length.rb:14:in `call' 
    railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call' 
    rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' 
    /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    /usr/local/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 

,只是確認它是在中間件。

rake middleware 
use ActionDispatch::Static 
use Rack::Lock 
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fc11b9c4a30> 
use Rack::Runtime 
use Rack::MethodOverride 
use ActionDispatch::RequestId 
use Rails::Rack::Logger 
use ActionDispatch::ShowExceptions 
use ActionDispatch::DebugExceptions 
use ActionDispatch::RemoteIp 
use ActionDispatch::Reloader 
use ActionDispatch::Callbacks 
use ActiveRecord::ConnectionAdapters::ConnectionManagement 
use ActiveRecord::QueryCache 
use ActionDispatch::Cookies 
use ActionDispatch::Session::CookieStore 
use ActionDispatch::Flash 
use ActionDispatch::ParamsParser 
use ActionDispatch::Head 
use Rack::ConditionalGet 
use Rack::ETag 
use ActionDispatch::BestStandardsSupport 
use PDFKit::Middleware 
run Teacherjoy::Application.routes 

我錯過了什麼讓它工作?

+1

所以,構造的命令是:'/ usr/local/bin/wkhtmltopdf --quiet - '。也許你需要實際提供一些輸入/輸出信息? – Brian

回答

0

今天早上我嘗試了PDFKit,並有無限頁面加載相同的問題。我通過添加「config.threadsafe!」來修復它我的application.rb中

此外,我改變了我的代碼由this stackoverflow answer

0

爲pdfkit在/ usr/local/bin目錄的路徑解決方案引導,如果你在你的系統中使用RVM/wkhtmltopdfis不正確。 我的本地開發環境系統就是這種情況。

我檢查其wkhtmltopdf此命令將返回在我的應用程序目錄2路,如果我用我的寶石,然後我得到這樣

which wkhtmltopdf 
/home/re10/.rvm/gems/[email protected]/bin/wkhtmltopdf (path to the gemset in server) 

的路徑我試着在根目錄相同的命令,然後我我得到的路徑

which wkhtmltopdf 
/usr/local/bin/wkhtmltopdf 

所以嘗試進入到項目目錄後,並使用寶石的命令,它wkhtmltopdf路徑後,安裝軟件包應該是你的RVM內內,所有的寶石都存儲爲寶石。

請參考此stackoverflow爲例。這對我有用。 https://stackoverflow.com/a/5764290/1304966