ChartKick charts not showing when rendering to PDF using Wicked_PDF

I use Rails 4, Wicked_PDF and Chartkick Gem's For Google Charts, I use:

<% = javascript_include_tag "//www.google.com/jsapi", "chartkik"%>

The html view displays graphs and everything as expected. When I add .pdf to the URL, the pdf document is displayed in the browser, but the ChartKick charts are not displayed.
The following error message will appear:

Chart loading error: adapter not found

I found the following online in the PDFKit documentation.

Resources are not included in PDF: images, CSS or JavaScript do not seem to load in PDF. This is because the fact that wkhtmltopdf does not know where to find these files. To make sure that you use absolute paths (start with a slash) to your Resources. If you use PDFKit to create PDF files from raw HTML, make sure you use the full paths (either file paths or URLs including the domain). In restrictive root_url server environments, the configuration may be what you are looking to modify your resource.

I assume wkhtmltopdf does not find a link to the diagrams, but I'm not sure how to fix it. Anyone have a suggestion?

I found this link:
Render jQuery in wicked_pdf

Where Unixmonkey helps FattRyan solve this problem for Highcharts.

Can anyone help setting this wicked_pdf_javascript_include_tag so that Wicket_PDF accepts charts from Chartkick using google charts?

+3
source share
5 answers

You must specify the http or https protocol when referencing the CDN inside the pdf layout.

In addition, chartkick is used through the asset pipeline, so use wicked_pdf_javascript_include_tag .

Replace this line:

 <%= javascript_include_tag "//www.google.com/jsapi", "chartkik" %> 

Wherein:

 <%= javascript_include_tag "https://www.google.com/jsapi" %> <%= wicked_pdf_javascript_include_tag "chartkick" %> 

What I do is in my project.

Greetings.

+5
source

I struggled a bit with this, and the other answers were only partially useful to me. I would like to provide more details for everyone in the future:

The 4 main things that I did to fix this for us were:

(1) Do not use the middleware approach and instead use one of the built-in PDF files with rubies, based on the PDF layout that 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) with the help of wicked_pdf helpers, upload ONLY the javascript that we need on these pages for the assets that we store in the application (all CDN-based resources can be loaded using regular 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 saw only the axis and the data for the time diagrams (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 
+3
source

Add your attempt to convert to pdf at the top of the view:

  <%= wicked_pdf_javascript_include_tag "application", "chartkick" %> 
+2
source

I got it for working with Alex Villa's answer and answering a similar question , installing the latest version of wkhtmltopdf, 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 
+1
source

If anyone has the same problem with Rails 4 in 2019, try freezing the Chartkick version to 2.3.5 . Because they removed support for Rails <4.2 from version 3.0.0 . Watch Chartik CHANGELOG

 gem 'chartkick', '2.3.5' 

Add this at the beginning of the header in your PDF file:

 <%= javascript_include_tag "https://www.google.com/jsapi" %> <%= wicked_pdf_javascript_include_tag "chartkick" %> 

And replace wkhtmltopdf-binary- gem with wkhtmltopdf-binary-edge . I used version 0.12.4.0.

 gem 'wkhtmltopdf-binary-edge', '0.12.4.0' 

That is all I did, and it worked.

0
source

Source: https://habr.com/ru/post/1496398/


All Articles