PDF generation freezes on rails 4 using PDFkit camcorder

I can upload a pdf file using

curl google.com | wkhtmltopdf - test.pdf 

therefore, this means that the installation of wkhtmlpdf was successful.

But, when I try to create a pdf file by contacting http://localhost:3000/contacts/1.pdf , it freezes. The status bar displays: 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 to create pdf files by simply 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, helping me understand that I should indicate an absolute path for assets instead of relative ones. When I deleted the following lines, I was able to create a PDF file:

 <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 

Now I can’t figure out how to replace this with absolute paths . This message seems to be what I need, but I still can’t figure out how it will look for my case.

+8
ruby-on-rails pdf ruby-on-rails-4 pdf-generation wkhtmltopdf
source share
3 answers

The problem is with stylesheet_link_tag and javascript_include_tag using relative URLs, which often causes wkhtmltopdf to freeze when loading assets from the same server that wkhtmltopdf running wkhtmltopdf .

Using absolute URLs for assets solved the problem.

Set asset_host to the Rails configuration, which also affects stylesheet_link_tag and javascript_include_tag :

 # Modify asset host config setting in `config/application.rb` # Or create a new initializer: `config/initializers/wkhtmltopdf.rb` config.action_controller.asset_host = "http://mysite.com" # Or you can have different hosts for development (local) and production (CDN): # In `config/environments/development.rb` config.action_controller.asset_host = "http://localhost" # In `config/environments/production.rb` config.action_controller.asset_host = "http://d111111abcdef8.cloudfront.net" 
+8
source share

Setting config.action_controller.asset_host = "http://localhost" in development.rb didn't really work for me. That is, PDF generation will work, but then the tools will not occur when rendering HTML.

I followed the method here: http://jguimont.com/post/2627758108/pdfkit-and-its-middleware-on-heroku

and it worked like a charm to me. Hope this helps someone. Just throw assets.rb into config / intializers and you're good to go.

+3
source share

I had the same problem in which my log showed that the page was displayed, however a pdf file was not created and the browser freezes. In the end, it had nothing to do with OS compatibility, missing librarians, gems, and dependencies, but instead I needed to increase the maximum number of threads for my Puma server (which was set to 1) to 2 or more. Then he generated the PDF as usual.

0
source share

All Articles