2014-02-17 50 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>