轨道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