Render jQuery in wicked_pdf

孤人 提交于 2019-12-11 00:08:55

问题


I'm using Highcharts in my application to render charts. Everything works fine, except when I want to convert a page that has charts to PDF. I'm using wicked_pdf. Here is the show method my controller:

 format.pdf do
          render :pdf => "report",
                 :template => "/quarters/report.pdf.erb",
  end

My /quarters/report.pdf.erb file looks like it does in my show.html.erb for highcharts:

    <div id="testcollections" style="width: 600px;"></div>

<!-- jQuery for testing collections charts -->
<script type="text/javascript" charset="utf-8">
$(function () {
  new Highcharts.Chart({
    chart: { renderTo: 'testcollections' },
    title: { text: 'Test Collections' },
    plotOptions: {
            series: {
                dataLabels: {
                    enabled: true,
                    formatter: function() {
                        return this.y;
                    }
                }
            }
        },

    series: [{
    name: 'Collections',
    type: 'area',
    color: '#5b81b4',
    data: <%= Quarter.where('quarters.client_id=?',      @quarter.client_id).map(&:collections) %> 
    },
    {
    name:'Average Collections',
    type: 'area',
    color: '#999',
    data: <%= Quarter.includes(:client).group('clients.specialty',      'quarters.year', 'quarters.quarter')
    .average('quarters.collections').values.map(&:to_f).to_json %>
    }]
  });
});
</script>

And in my show page this is link to download the PDF file:

<%= link_to 'PDF', { :action => "show", :format => :pdf } %> |

The problem is that the chart doesn't render, it's just blank. I know from here that you are supposed to call the "wicked_pdf_javascript_include_tag" but it gives me an error of "undefined method `javascript_src_tag'".

Any thoughts?


回答1:


Looks like javascript_src_tag went away in Rails 3.1: http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/javascript_src_tag

You can replace your call to wicked_pdf_javascript_include_tag with something like this:

<script type="text/javascript" src="file://#{Rails.root.join('public','javascripts','highcharts.js')}"></script>


来源:https://stackoverflow.com/questions/8318247/render-jquery-in-wicked-pdf

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