3

wickedpdf 是否呈现 javascript 图形?

红宝石 1.8.7

导轨 - 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
4

1 回答 1

0

它可能没有完整的 js 解释器。尝试渲染 jpg。Gruff 会这样做,但它看起来不太好,尤其是对于时间图

于 2012-07-10T07:17:35.740 回答