generating pdf hangs on rails 4 using PDFkit gem - ruby-on-rails

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.

The issue was due to stylesheet_link_tag and javascript_include_tag using relative URLs, which often causes wkhtmltopdf to hang when loading assets from the same server that wkhtmltopdf is running on.
Using absolute URLs for assets solved the problem.
Set asset_host in Rails' config, 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"

Setting config.action_controller.asset_host = "http://localhost"
in development.rb actually didn't work for me. That is, the PDF generation would work, but then assets wouldn't come through 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 for me. Hope this helps someone. Just throw assets.rb in config/intializers and you're good to go.

I had the same issue in which my log showed the page had rendered however no pdf was generated and the browser would hang. It ended up having nothing to do with OS compatability, missing librariers, gems nor dependencies but instead I needed to raise the max allowable thread count for my Puma server (which had been set to 1) upto 2 or more. This then generated pdf's as normal.

Related

Routing issues on ruby in rails 4.2.6?

When I am trying to perform destroy action n windows 7, I am receiving:
Started GET "/stylesheets/default.css" for ::1 at 2016-06-20 13:52:53 +0530
ActionController::RoutingError (No route matches [GET] "/stylesheets/default.css"):
and
Started GET "/javascripts/default.js" for ::1 at 2016-06-20 13:52:59 +0530
ActionController::RoutingError (No route matches [GET] "/javascripts/default.js"):
use "application" instead of "default" in
views/layouts/application.html.erb
<%= stylesheet_link_tag "application", media: "all" %>
The type of the request, i.e. delete, is irrelevant.
Edit:=================
Your error is due to the fact that your stylesheet_link_tag on the page views/layouts/application.html.erb looks something like this:
<%= stylesheet_link_tag "default", media: "all" %>
but there is no file by the name of default.css in assets/stylesheets--presumably the default file application.css contains the manifest for the css files.
====================
The rails asset pipeline combines all the css files listed in the manifest file assets/stylesheets/application.css into one big file, minimizes it, then sends the single css file to the browser when a view is loaded. The same thing happens for all your js files. To properly link to your single, minimized css file and your single minimized js file, the views/layouts/application.html.erb file should look something like this:
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<%= stylesheet_link_tag "application", media: "all" %> #<****HERE
<%= javascript_include_tag "application" %> #<****HERE
<%= csrf_meta_tags %>
</head>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<div>
<%= yield %>
</div>
<%= debug params %>
<%= debug session[:allowed] %>
</body>
</html>
When the page loads in your browser, you will see requests in your server window like:
Started GET "/assets/cats.css?body=1" for 127.0.0.1 at 2016-06-20 04:06:19 -0600
Started GET "/assets/cats.js?body=1" for 127.0.0.1 at 2016-06-20 04:06:19 -0600
You can use the path:
/stylesheets/default.css
in your stylesheet_link_tag if the file:
public/stylesheets/default.css
exists. According to the rails asset pipeline guide, you also have to add:
config.serve_static_files = true
to config/application.rb--but I find that is not needed for development, so it's probably required for production.
The server window does not show a request for a css file located in the public/ directory, but you can open a web inspector and examine the network activity and see that your browser does send the request.
Have you given the method for the delete action to be performed, because i think if you don't provide the exact method , which in case of your's is DELETE it would redirect to some other paths. If you can provide the syntax you are using for this , it will be more helpful to resolve the issue. For eg:-
<%= link_to '', your_action_path(object.id),:class=>"btn btn-danger fa fa-trash-o fa-1x",:style=>"margin-top:4px",:confirm =>'Are you sure',:method => :delete %>

Why rails isn't displaying a web page

Each time I launch a rails application, the server displays an error message. For instance (this is from the command line):
Started GET "/" for 127.0.0.1 at 2016-01-10 00:12:31 -0600
Processing by WelcomeController#index as HTML
Rendered welcome/index.html.erb within layouts/application (3.0ms)
Completed 500 Internal Server Error in 2054ms
ActionView::Template::Error (TypeError: Object doesn't support this property or method
(in c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/turbolinks-2.5.3/lib/assets/javascripts/turbolinks.js.coffee)):
3: <head>
4: <title>AlphaBlog</title>
5: <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6: <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7: <%= csrf_meta_tags %>
8: </head>
9: <body>
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___602047428_59662224'
Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (64.8ms)
How do I fix this issue?
The first way to solve. Add to file application.js this line:
//= require turbolinks
The second method of solving. In Gemfile set version 1.8.0 for gem 'coffee-script-source' :
gem 'coffee-script-source', '1.8.0'
Try removing the javascript_include_tag (line indicated as broken) and see if the page loads. If the page does load, then it maybe something wrong with the assets files regarding javascript.

Error favicon.ico same as query in show page rails 4.2.0

I had a problem about favicon.ico in show page.
And i tried to search and apply these solutions but it still failure. Here are some result which i found but i didn't successfully.
Ruby on Rails Error. Processing Controller method as png
This is problem show in production.log
This is my log:
Started GET "/videos/favicon.ico" for 42.112.87.124 at 2015-07-12 19:26:54 +0700
Processing by VideosController#show as
Parameters: {"id"=>"favicon"}
Video Load (0.2ms) SELECT `videos`.* FROM `videos` WHERE `videos`.`id` = 0 LIMIT 1
Completed 500 Internal Server Error in 3ms
And this is code in my application.html.erb
<%= favicon_link_tag 'favicon.ico' %>
<%= stylesheet_link_tag 'application', media: 'all'%>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
And this is my routes.rb:
resources :videos, only: [:index, :show]
But everytime i called to action show, it always returned a params[:id]="favicon". And i didn't understand why it happened ?.
Hope everybody can explain for me. Thank you very much.
Change
<%= favicon_link_tag 'favicon.ico' %>
to
<%= favicon_link_tag '/favicon.ico' %>
to load the favicon from the server's root instead of the folder relative to the document.

Restructuring .js files and directories

When I recently started on a mobile version of my app I realized that I had no use of all javascript code written for desktop clients. So I put this in application.html.erb
<% if mobile_agent? %>
<%= javascript_include_tag "mobile" %>
<%= stylesheet_link_tag "mobile", :media => "all" %>
<% else %>
<%= javascript_include_tag "desktop" %>
<%= stylesheet_link_tag "desktop", :media => "all" %>
<% end %>
And in app > assets > javascripts I created this structure
- desktop.js
- mobile.js
- desktop/file1.js
- desktop/file2.js
- mobile/file1.js
- mobile/file2.js
- shared/file1.js
- shared/file2.js
And in mobile.js:
//= require_tree ./mobile
//= require_tree ./shared
And in desktop.js
//= require_tree ./desktop
//= require_tree ./shared
In development env that has worked fine, but when I have deployed to Heroku it gives me an error:
Completed 500 Internal Server Error in 100ms
ActionView::Template::Error (mobile.js isn't precompiled):
8: <meta name="format-detection" content="telephone=no">
11: <%= javascript_include_tag "mobile" %>
9:
12: <%= stylesheet_link_tag "mobile", :media => "all" %>
10: <% if mobile_agent? %>
Parameters: {"community_category"=>"swingdancing", "city"=>"stockholm"}
app/views/layouts/application.html.erb:11:in 78182492067426179_45617280'
13: <% else %>
Completed 500 Internal Server Error in 2ms
app/controllers/communities_controller.rb:20:in `show'
Processing by ErrorsController#show as HTML
14: <%= javascript_include_tag "desktop" %>
/app/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/sprockets/helpers/rails_helper.rb:142:in Error during failsafe response: mobile.js isn't precompiled
What is causing this error and how can I resolve it?
what to do ?
simply add
config.assets.precompile += %w(mobile.js)
to config/application.rb
why ?
when you deploy to production environment, by default rails uses the asset pipeline.
This means that instead of serving directly the files that are in assets/, rails assumes that you have run rake assets:precompile beforehand, and that there are concatenated, compressed and minified versions of your assets in an another folder.
This implies that each and every asset you intend to load from the browser should be precompiled. The benefit is that instead of serving many human-readable files, you only serve a few that have been minified - less connections, less volume = faster load time.
This feature is inactive in development mode because you would have to recompile the assets at each request, and moreover being able to read js and css is quite usefull for debugging.

Rails custom css links are not working - following tutorial in http://ruby.railstutorial.org

I'm new to rails and working my way into the tutorial here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
I've added the blueprint css package to the public/stylesheets directory and added a custom.css file however I cannot seem to get the stylesheet to load. From my understanding fo the setup the page should have a blue background and some other small changes.
In my app/views/layouts/application.html.erb file I've added the following line:
<%= stylesheet_link_tag 'stylesheets/custom', :media => 'screen' %>
Which I believe should cause the css from the /public/custom.css to load.
In the tutorial the content of the custom.css file is located in section 5.1.2 (sorry I'm only able to post 2 hyperlinks here)
.
And the content of the app/views/layouts/application.html.erb file is here:
http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec:adding_to_the_layout
When I view source on the page there is a link to the custom.css file here:
But clicking that brings up a "Routing Error - No route matches "/stylesheets/stylesheets/custom.css"" message.
As far as I can find I've followed the tutorial exactly so I'm not sure what is wrong or what I've missed. Any help on where to go from here would be appreciated.
complete text of the application.html.erb file:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
<%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
<!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
<%= stylesheet_link_tag 'custom', :media => 'screen' %>
</head>
<body>
<div class="container">
<header>
<%= image_tag("logo.png", :alt => "Sample App", :class => "round") %>
<nav class="round">
<ul>
<li><%= link_to "Home", '#' %></li>
<li><%= link_to "Help", '#' %></li>
<li><%= link_to "Sign in", '#' %></li>
</ul>
</nav>
</header>
<section class="round">
<%= yield %>
</section>
</div>
</body>
</html>
This still has the same error when clicking the custom.css link in source as before:
"Routing Error - No route matches "/stylesheets/stylesheets/custom.css""
is this happening on localhost or production environment? If it's production then open config/production.rb and change:
config.serve_static_assets = false
to
config.serve_static_assets = true
If this is happening in development environment then I'm wondering if you possibly have more than 1 layout file. Check to make sure you don't have application.html.erb and application.html.haml. It's also possible that you have a separate layout for your posts, which would be titled post.html.haml or something...
ALSO
you state the following:
Which I believe should cause the css from the /public/custom.css to load.
If your custom.css file is located in public/custom.css then it is in the wrong place, it should be in public/stylesheets/custom.css
EDIT:
below is a screenshot of your app as it appears on my machine, it looks like it works ok after moving stylesheet into the right folder. I also took the liberty of deleting public/index.html, but you can leave it in for now until the tutorial advises you to take it out
Change it to stylesheet_link_tag 'custom', :media => 'screen'. It will automatically look in the /public/stylesheets/
So I'm not sure if you ever figured this out (I'm sure you did), but I just had the same issue, and found a solution. It looks like rails is looking in /app/assets/stylesheets instead of /public/stylesheets. I'm not sure if this is the correct way it should be working, but I moved my custom.css file there and everything is now working fine.
I had a similar problem following the tutorial but it hit me in chapter 4 where we first include the helper for the blueprint css files. I am using Rails v. 3.1.3, and for me nnaficy's answer solved the problem. Heres the description of why this is happening from the rubyonrails guide :
(source: http://guides.rubyonrails.org/layouts_and_rendering.html)
"3.1.2 Linking to JavaScript Files with the javascript_include_tag
The javascript_include_tag helper returns an HTML script tag for each source provided.
If you are using Rails with the Asset Pipeline enabled, this helper will generate a link to /assets/javascripts/ rather than public/javascripts which was used in earlier versions of Rails. This link is then served by the Sprockets gem, which was introduced in Rails 3.1."
(i copied the above for the explanation, but specific to css is the following snippet:)
"3.1.3 Linking to CSS Files with the stylesheet_link_tag
The stylesheet_link_tag helper returns an HTML tag for each source provided.
If you are using Rails with the “Asset Pipeline” enabled, this helper will generate a link to /assets/stylesheets/. This link is then processed by the Sprockets gem. A stylesheet file can be stored in one of three locations: app/assets, lib/assets or vendor/assets."
so instead of copying my blueprint folder to public/stylesheets/, i copied it to app/assets/stylesheets/ and the css did its magic.
Change the line:
<%= stylesheet_link_tag '/stylesheets/application', :media => 'screen' %>
to
<%= stylesheet_link_tag '/stylesheets/custom', :media => 'screen' %>
It worked for me.
Also, for those who are still working on the problem, check that you are puting the CSS in the right project. I had a folder called "sample_app" in which I started the tutorial. Some days after, I started using Aptana/RadRails, which created a custom project folder (workspace). I didn't realized and put all my css in my initial "sample_app" folder. That's why Rails could not find the css/logo!
Same question with my current Rails 3.2.6 app. I've solved it with this include function:
<%= stylesheet_link_tag '/stylesheets/custom', :media => 'screen' %>
You have to use absolute path's instead of relative ones.
Ok so maybe someone out there is still struggling, I've been trying to figure these out for a while now and i have solved the problem. Like #shime is saying Rails looks in public/stylesheets.
application.html.erb
-----------------
`<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
<%= stylesheet_link_tag **'screen'**, :media => 'screen' %>
<%= stylesheet_link_tag **'print'**, :media => 'print' %>
</head>
<body>
<%= yield %>
</body>
</html>`
Move screen.css and print.css from stylesheet/blueprint to app/assets/stylesheets.

Resources