Ruby / Rails Javascript(flot)使用Wicked_pdf渲染pdf

时间:2012-07-04 00:59:15

标签: ruby-on-rails ruby pdf flot wicked-pdf

wickedpdf会渲染javascript图吗?

Ruby 1.8.7

Rails - 2.3.8

我的代码工作正常,视图很好 - flot确实以我想要的方式绘制图形...但是当我查看pdf文件时,它全部都是空白的。

还有其他人遇到过类似的问题吗?

控制器:

 def graph
 @now=Time.now

 @graph = [0,0] / some sort of dataset /

 pdf = render_to_string :pdf => "graphs", :orientation => 'Landscape', :font_size => 11
 save_path = Rails.root.join('pdfs', @now.to_date.to_s+'_graphs.pdf')

 File.open(save_path, 'wb') do |file|

 file << pdf
 end

查看:

<script language="javascript" type="text/javascript" src="/javascripts/jquery.js"></script>
<script language="javascript" type="text/javascript" src="/javascripts/jquery.flot.js"></script>
<script language="javascript" type="text/javascript" src="/javascripts/jquery.flot.navigate.js"></script>

<%= wicked_pdf_stylesheet_link_tag "pdf" -%>
<%= wicked_pdf_javascript_src_tag "jquery" %>
<%= wicked_pdf_javascript_src_tag "jquery.flot" %>
<%= wicked_pdf_javascript_src_tag "jquery.flot.navigate" %>
<script type="text/javascript">

$(function () {

var options = {
    lines: { show: true },
    points: { show: true },
    xaxis: { mode: "time",  timeformat: "%m/%y",   minTickSize: [1, "day"], tickSize: [1, "month"]}
    };

    var graph = <%= @graph.to_json %>;
    $.plot(placeholder,graph.data,options);
    }

  <div id="placeholder" style="width:1000;height:500px"></div>  

助手:

    module WickedPdfHelper
  def wicked_pdf_stylesheet_link_tag(*sources)
    css_dir = Rails.root.join('public','stylesheets')
    sources.collect { |source|
      "<style type='text/css'>#{File.read(css_dir.join(source+'.css'))}</style>"
    }.join("\n").html_safe
  end

  def wicked_pdf_image_tag(img, options={})
    image_tag "file:///#{Rails.root.join('public', 'images', img)}", options
  end

  def wicked_pdf_javascript_src_tag(jsfile, options={})
    javascript_src_tag jsfile, options
  end

  def wicked_pdf_javascript_include_tag(*sources)
    sources.collect{ |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n").html_safe
  end

  module Assets
    def wicked_pdf_stylesheet_link_tag(*sources)
      sources.collect { |source|
        "<style type='text/css'>#{read_asset(source+".css")}</style>"
      }.join("\n").html_safe
    end

    def wicked_pdf_image_tag(img, options={})
      image_tag "file://#{asset_pathname(img).to_s}", options
    end

    def wicked_pdf_javascript_src_tag(jsfile, options={})
      javascript_include_tag jsfile, options
    end

    def wicked_pdf_javascript_include_tag(*sources)
      sources.collect { |source|
        "<script type='text/javascript'>#{read_asset(source+".js")}</script>"
      }.join("\n").html_safe
    end

    private

    def asset_pathname(source)
      if Rails.configuration.assets.compile == false
        File.join(Rails.public_path, asset_path(source))
      else
        Rails.application.assets.find_asset(source).pathname
      end
    end

    def read_asset(source)
      if Rails.configuration.assets.compile == false
        IO.read(asset_pathname(source))
      else
        Rails.application.assets.find_asset(source).to_s
      end
    end
  end
  end

1 个答案:

答案 0 :(得分:0)

它可能没有完整的js解释器。尝试渲染jpg。 Gruff会做到这一点,但它看起来不是特别适合时间图