How to export charts.js dynamic chart to PDF in Rails

๐Ÿ‘:0

In case someone comes across this question, I managed to improve the quality of the exported chart on pdf with some tweaking.
Iโ€™m using chartjs 2.0 but this should apply to older versions. wicked_pdf is 1.0.4

First I thought about getting the chart image from the canvas when it finished drawing and then put it on an image tag, but then realised it was a js timing problem, so I ended up fixing this just by letting rails wait more time for the page to render before generating the pdf.

In your controller when you do the render you can:

def show
  respond_to do |format|
    format.html
    format.pdf do
      render  :pdf => 'report',
              :template => 'reports/report_test.html.erb',
              :layout => "report",
              :page_size => "A4",
              javascript_delay: 1000 # The relevant line is this one 
    end
  end
end

Leave a comment