2014-02-17 36 views
1

应用主版面:如何获得jQuery风格pdf与Rails 4中的wicked_pdf生成的?

这应该用于HTML。

<head> 
    <%= stylesheet_link_tag "application" %> 
    <%= stylesheet_link_tag "print", media: 'print' %> 
    <%= javascript_include_tag "application" %> 
    <%= csrf_meta_tags %> 
</head> 

在PDF布局:

这应该用于PDF文件。

<head> 
    <title>Cassa</title> 
    <%= stylesheet_link_tag "application" %> 
    <%= stylesheet_link_tag "print", media: 'print' %> 
    <%= wicked_pdf_javascript_include_tag "application-pdf" %> 
</head> 

控制器:

respond_to do |format| 
    format.html 
    format.pdf do 
     render pdf: "your_assessment_printout.pdf", 
     disposition: "inline", 
     template: "assessments/show.pdf.erb", 
     layout: "application-pdf.html.erb" 
    end 
    format.html 
    end 

show.pdf.erb是具有HTML构建一个表中的模板,被用在<%= variable_name %>时尚必要的对象填充。

我可以得到PDF生成,但我可以风格的唯一方法是内联。 EX)

<tr style=" 
     background-color: #cdcdcf; 
    padding: 5em 0;"> 
    <td>...</td> 
</tr> 

jQuery的应用pdf.js脚本,我想使用:

$(document).ready(function() { 
    $('#summary-half table tr:odd').css('background-color', '#cdcdcf'); 
    $('#summary-half table tr:even').css('background-color', '#fff'); 

    $('#summary-half table li:odd').css('background-color', '#cdcdf'); 
    $('#summary-half table li:even').css('background-color', '#fff'); 
}); 

我不能够动态风格<tr></tr><li></li>元素。我已经注释掉了application.html.erb样式表和javascript标签,并且只启用了pdf特定的并且仍然没有改变。

有没有人使用wicked_pdf动态通过JavaScript/jQuery有任何运气样式元素?

回答

3

虽然没有引用外部文件,但我想通过让rails更新服务器端的类名来解决这个问题。

然后我问自己,如果将CSS放在show.pdf.erb和IT WORKS的样式标签之间会发生什么。所以我在每个元素中都使用了内联样式。唷!

然后下一个自然的问题是,如果我将jQuery放在show.pdf.erb顶部,它会起作用。不要忘记在你的脚本之前添加:

<%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %> 
<%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %> 

所以浏览器理解jQuery。我现在有TR行和LI行交替的颜色。

工作show.pdf.erb部分:

<%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %> 
<%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %> 
<script> 
    $(document).ready(function() { 
     $('table tr:odd').css('background-color', '#cdcdcf'); 
     $('table tr:even').css('background-color', '#fff'); 

     $('table li:odd').css('background-color', '#cdcdcf'); 
     $('table li:even').css('background-color', '#fff'); 
    }); 
</script> 

<table> 
    <tr> 
     <td> 
     <ul> 
      <li></li> 
     </ul> 
     </td> 
    </tr> 
</table>