軌道4我能下載pdf文件有:生成PDF掛在使用PDFkit寶石
curl google.com | wkhtmltopdf - test.pdf
所以這意味着,wkhtmlpdf安裝成功。
但是,當我嘗試通過訪問http://localhost:3000/contacts/1.pdf
生成pdf文件時,它會掛起。在狀態欄顯示內容:Waiting for localhost...
Rails的服務器輸出:
Started GET "/contacts/1.pdf" for 127.0.0.1 at 2013-07-28 21:45:06 +0900
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ContactsController#show as HTML
Parameters: {"id"=>"1"}
Contact Load (0.3ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? LIMIT 1 [["id", "1"]]
Rendered contacts/show.html.erb within layouts/application (1.4ms)
Completed 200 OK in 99ms (Views: 57.0ms | ActiveRecord: 0.7ms)
的Gemfile:
gem 'pdfkit'
application.rb中:
config.middleware.use "PDFKit::Middleware"
按照PDFKit railscast這應該是隻需添加即可生成pdf文件0 ...
UPDATE:
show.html.erb:
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @contact.name %>
</p>
<p>
<strong>Age:</strong>
<%= @contact.age %>
</p>
<%= link_to 'Edit', edit_contact_path(@contact) %> |
<%= link_to 'Back', contacts_path %>
佈局/ application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Pdftest</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
更新2:
感謝@Arman H幫助我弄清楚,我必須爲資產指定絕對路徑而不是相對路徑。當我刪除了以下幾行,我能夠生成PDF文件:
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
現在,我不能得到如何與絕對路徑替代這一點。看來這post是我需要的,但我仍然無法弄清楚這對我的情況會如何。
它掛了多久? Rails控制檯中的'wkhtmltopdf'是否有錯誤?有時它似乎掛起,但它只是需要很長時間來處理PDF。在切斷請求之前,您已經運行了多久? –
上面的問題在Mac OS x上有過。現在我在Linux Mint上也一樣。它仍然是「等待本地主機......」20分鐘,看起來這將是無止境的。我只需要按Ctrl + c取消服務器。 – Askar
當我在40分鐘後關閉服務器時,它爲我生成了pdf文件。我在1分鐘後再次嘗試關閉服務器,在這種情況下,它顯示:命令失敗:/ usr/bin/wkhtmltopdf --page-size字母 - 邊距0.75in - 邊距 - 右邊0.75in - 邊緣底部0.75in -margin-left 0.75in --encoding UTF-8 --quiet - - – Askar