在 wicked_pdf 中渲染 jQuery

发布于 2024-12-18 19:06:24 字数 1793 浏览 2 评论 0原文

我在我的应用程序中使用 Highcharts 来渲染图表。一切工作正常,除了当我想将包含图表的页面转换为 PDF 时。我正在使用 wicked_pdf。这是我的控制器的显示方法:

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

我的 /quarters/report.pdf.erb 文件看起来就像在我的 show.html.erb 中的 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>

在我的显示页面中,这是下载 PDF 文件的链接:

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

问题是图表不会呈现,它只是空白。我从这里知道你应该调用“wicked_pdf_javascript_include_tag”,但它给了我一个“未定义”的错误方法‘javascript_src_tag’”。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2024-12-25 19:06:24

看起来 javascript_src_tag 在 Rails 3.1 中消失了:
http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/javascript_src_tag

您可以将您对 wicked_pdf_javascript_include_tag 的调用替换为如下内容:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文