2012-07-04 57 views
3

wickedpdf渲染javascript图吗?Ruby/Rails Javascript(flot)render pdf with Wickedpdf

的Ruby 1.8.7

导轨 - 2.3.8

我的代码工作正常,视图出来罚款 - 海军报不出来的情节图表的方式我想它......但是,当我看着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 
+0

http://rubydoc.info/gems/wicked_pdf/0.6.0/WickedPdfHelper#wicked_pdf_image_tag-instance_method – shunchan

回答

0

它可能没有一个完整的JS解释。尝试渲染一个JPG。粗鲁会做到这一点,但它看起来不是很好,特别是对于时间地块

+0

你有没有试过画布? – shunchan