I'm able to download pdf file with:
curl google.com | wkhtmltopdf - test.pdf
so it means, wkhtmlpdf installation was successful.
But, when I try to generate pdf file by accessing http://localhost:3000/contacts/1.pdf
it hangs. In the status bar it shows: Waiting for localhost...
Rails server output:
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"
According to the PDFKit railscast this should be enough for generating pdf files just by adding .pdf
...
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 %>
layouts/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>
UPDATE 2:
Thanks to @Arman H for helping me to figure out that I have to specify absolute path for assets instead of a relative ones. When I removed the following lines I was able to generate PDF file:
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
Now, I can't get how to substitute this with an absolute paths. It seems this post is what I need, but I still can't figure out how this would look like for my case.
wkhtmltopdf
in Rails' console? Sometimes it appears to hang, but it's really just taking a long time to process the PDF. How long have you had it running for before cutting the request off? – Izanagiwkhtmltopdf
from terminal, outside of Rails, with the same parameters, and see if it gives you any errors. That is, run/usr/bin/wkhtmltopdf --page-size Letter --margin-top 0.75in --margin-right 0.75in --margin-bottom
... – Izanagiwkhtmltopdf
couldn't find/load them. Try removing all assets, and rendering bare HTML. If that's the issue, rewrite your paths with absolute values. – Izanagi