ChartKick charts do not show when rendering to PDF using Wicked_PDF

天大地大妈咪最大 提交于 2019-12-01 20:55:25

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" %>
jhoanna

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!