ChartKick charts do not show when rendering to PDF using Wicked_PDF - ruby-on-rails

I am using Rails 4, Wicked_PDF and Chartkick Gem's
For Google Charts I use:
<%= javascript_include_tag
"//www.google.com/jsapi", "chartkik" %>
The html view comes up with charts and everything as expected.
When I append .pdf to the url the pdf document shows in the browser but the ChartKick charts do not show.
The following error appears where the chart should be:
Error Loading Chart: No adapter found
I have found the following online in the PDFKit documentation.
Resources aren't included in the PDF: Images, CSS, or JavaScript does
not seem to be downloading correctly in the PDF. This is due to the
fact that wkhtmltopdf does not know where to find those files. Make
sure you are using absolute paths (start with forward slash) to your
resources. If you are using PDFKit to generate PDFs from a raw HTML
source make sure you use complete paths (either file paths or urls
including the domain). In restrictive server environments the root_url
configuration may be what you are looking for change your asset host.
I am assuming that wkhtmltopdf is not finding the link to the charts, but I am not sure how to fix this.
Does anyone have a suggestion?
I found this link:
Render jQuery in wicked_pdf
Where Unixmonkey helps FattRyan to solve this for Highcharts.
Can anyone help how to set this wicked_pdf_javascript_include_tag so that Wicket_PDF will accept charts from Chartkick using Google charts?

You have to specify a protocol http or https when referencing to a CDN inside the pdf layout.
Also chartkick is served via the assets pipeline, so use wicked_pdf_javascript_include_tag instead.
Replace this line:
<%= javascript_include_tag "//www.google.com/jsapi", "chartkik" %>
With this:
<%= javascript_include_tag "https://www.google.com/jsapi" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
That's how I do it in a project of mine.
Cheers.

I struggled with this for a bit and the other answers were only partially useful for me. I wanted to provide more detail for anyone in the future:
The 4 major things I did to fix this for us were:
(1) Not using the middleware approach and instead using one off ruby embedded pdfs based off of a PDF layout you create
#Example layout file
#app/views/layout/pdf.pdf.rb
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%= wicked_pdf_stylesheet_link_tag "print" %> #print specific stylesheet
<%= yield :head %>
</head>
<body>
<h1>PDF Report</h1>
<%= yield %>
</body>
</html>
(2) using wicked_pdf's asset helpers to load ONLY the javascript we needed on those pages for assets we store in the app (all CDN based assets can be loaded with a normal javascript_include_tag)
#Example page template for a PDF you're downloading
#app/views/users/profile.pdf.erb
<% content_for :head do %>
<%= javascript_include_tag "some_cdn.com" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
<% end %>
<p>All your pages content</p>
(3) using the javascript_delay option
#Use it universally from the initializer or on the specific PDF rendering in the controller
#config/initializers/wicked_pdf.rb
WickedPdf.config = {
javascript_delay: 3000,
other_options...
}
(4) pass the "discrete" axis option otherwise we only saw the axis and no data for time based charts (line/area/etc.)
#In the above template, wherever you render your chart
#app/views/users/profile.pdf.erb
<% content_for :head do %>
<%= javascript_include_tag "some_cdn.com" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
<% end %>
<%= area_chart #data_retriever.time_based_data, discrete: true %>
<%= pie_chart #data_retriever.other_data %> # the default is discrete: false so no need for another option

Add the following to the top of the view your trying to convert to a pdf:
<%= wicked_pdf_javascript_include_tag "application", "chartkick" %>

I got it to work with Alex Villa's answer and from the answer to a similar question by installing the latest wkhtmltopdf version then specifying the javascript_delay option in the controller in step (3):
respond_to do |format|
format.html
format.pdf do
render pdf: "filename",
javascript_delay: 3000,
template: 'template_path.pdf.erb',
layout: 'pdf.html'
end
end

If someone gets the same issue with Rails 4 in 2019, then try to freeze chartkick version on 2.3.5. Because from the 3.0.0 version they removed support for Rails < 4.2. See
chartkick CHANGELOG
gem 'chartkick', '2.3.5'
Add this at the beginning of head in your pdf view file:
<%= javascript_include_tag "https://www.google.com/jsapi" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
And replace the wkhtmltopdf-binary gem with wkhtmltopdf-binary-edge. I used 0.12.4.0 version.
gem 'wkhtmltopdf-binary-edge', '0.12.4.0'
That's all I did and it worked.

Related

how to manually include remotipart js?

I need to conditionally use the remotipart gem. The [docs][1] say just add it to application.js:
//= require jquery.remotipart
But I don't want it to be included with every single view, instead I want to conditionally include it, however when I try:
<%= javascript_include_tag "jquery.remotipart" %>
I get an error. How do i reference a js included as part of a gem generically, and remotipart js specifically?
Thanks,
Kevin
But I don't want it to be included with every single view, instead I want to conditionally include it, however when I try:
What means conditionally in this context? The most simplest way would be
<%= javascript_include_tag "jquery.remotipart" if condition %>
Also you could use content_for like this on the views where you want to include it:
# in your view
<% content_for :js do %>
<%= javascript_include_tag "jquery.remotipart" %>
<% end %>
# in your layout.html.erb
<%= yield :script %>
https://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
If you meant that you get an error that the JS file can't be found, please update your question with what library (webpacker, sprockets) and Rails version you use.

Rendering a Navbar React Component in Rails App

I have a simple Rails 5 app with the react-rails gem installed. I want to create a Navbar that will appear at the top of every page in my app. I'm new to React, so normally what I would do is include an erb tag in application.html.erb that would render a layout helper called _navbar.html.erb, and would appear above the yield block. How do I accomplish this same thing using the react-rails gem and a navbar component? Rendering a react component from application.html.erb like this does not seem to be working:
<body>
<div id='app'>
<%= react_component 'Navbar' %>
<%= yield %>
</div>
</body>
Maybe it has something to do with <%= javascript_include_tag 'application' %> in application.html.erb being inside <head> and the page not being fully rendered yet. Any help would be appreciated (bonus points for including the react-bootstrap navbar component!) Thanks!
Edit: I can now get something to display, but it's not formatted like a navbar. I'm using the react-bootstrap docs example but no luck yet.

css and javascript in just one page (Rails 4) [duplicate]

This question already has answers here:
Using Rails 3.1, where do you put your "page specific" JavaScript code?
(29 answers)
Closed 9 years ago.
I'm starting with Rails 4 and I have a couple of questions about assets.
How can I specify that a CSS just must be included in some view?
I have the same question about a JS file. I have added a JS file to app/assets/javascripts/viewname.js
however it is included in every page. I would like it to be seen just in viewname view.
Help me please!
Thanks
Save the file as /vendor/assets/javascripts/viewname.js and include it in your view with:
<%= javascript_include_tag 'viewname' %>
To include the JS file in the HTML header
Use content_for in a view file to collect content that will be rendered later.
<% content_for :head %>
<%= javascript_include_tag 'viewname' %>
<% end %>
Call yield in the layout template to render the collected content:
<head>
...
<%= yield :head %>
</head>
To automatically include a javascript file named after the current controller, you could also put this in your layout template file:
<head>
...
<%= javascript_include_tag params[:controller] %>
<%= javascript_include_tag "#{params[:controller]}_#{params[:action]}" # variant with action name %>
</head>
But that will give 404 errors for a controllers/actions without a javascript file.

Dynamically loaded assets in Rails 4 and Turbolinks

I am working on a Rails 4 app in application.html.erb I have the following code
....
<head>
<title>...</title>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= yield :head %>
<%= csrf_meta_tags %>
</head>
What this does is allows me to load certain assets depending on the page I am on. So maybe in the show template of a certain action I could do something like
<% content_for :head do %>
<%= stylesheet_link_tag "show" %>
<% end %>
This works great and helps me keep the page structured clean and simple. The issue I am having now is that turbolinks is completely ignoring these assets and not even loading them when I click on a next link. I have looked around but cant seem to find an answer to this problem. Any help will be appreciated.
I had the same problem, try to add data-turbolinks-track into your stylesheet tag
This is most likely because turbolinks does not replace the <head> element:
Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body and the title in the head. Think CGI vs persistent process.
As such, any changes to the <head> element will be ignored.

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