2017-08-10 147 views
-1

我的要求是,下載的docx文件,並應在Microsoft Word中打開:內聯CSS不與寶石「htmltopdf」工作

我使用下面的寶石:

gem 'responders' 
gem 'htmltoword 

控制器代碼:

require 'htmltoword' 

class Admin::VisibilitiesController < ApplicationController 
respond_to :docx, :html, :css,:js 

def preview 
    @project = Project.find_by(id: params[:id]) 
    @feeder = Project.find_by(id: params[:id]).form2.last.feeder11s.first 
     respond_to do |format| 
     format.docx do 
     render :docx => "report1_docx",:template => 'admin/visibilities/preview.html.docx.erb', :page_height => 600, :page_width =>345 
     end 
    end 
    end 

查看文件代碼:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<title></title> 
</head> 
<body> 
<style type="text/css"> 
    div.alwaysbreak { page-break-before: always; } 
div.nobreak:before { clear:both; } 
div.nobreak { page-break-inside: avoid; } 
td{padding: 2px 5px;} 

</style> 
<div style="padding-top:20px;"> 



<table style="width:800px;margin:0px auto;border:1px solid grey; background: #fff;margin: 0 auto;margin-bottom:30px;padding:10px 20px; ">  
     <tbody> 
     <tr> 
      <td style="padding: 15px 0 50px;"> 
      <table style="padding:0px;overflow:hidden;display:table;"> 
       <tbody> 
       <tr> 
        <td style="font-size: 16px;width:100%;font-weight:600"> 
        Report No......./...../......./20116-17..... 
        </td>   
       </tr> 
       <tr> 

        <td style="font-size: 16px;width:100%;font-weight:600"> 
        Dated: ...... 

        </td> 

       </tr> 
       </tbody> 
      </table> 
      </td> 
     </tr> 



     <tr> 
     <td style="font-size:28px;font-weight:bold;text-align:center;padding-bottom:20px; font-style: italic;">THIRD PARTY VILLAGE INSPECTION REPORT</td> 
     </tr> 
      <tr> 
     <td style="font-size:28px;font-weight: bold;text-align:center;padding-bottom:25px; font-style: italic;">OF</td> 
     </tr> 
      <tr> 
     <td style="font-size:24px;font-weight:400;text-align:center;padding-bottom:25px;">RURAL ELECTRIFICATION WORKS UNDER DEEN DAYAL UPADHYAYA GRAM JYOTI YOJANA (erstwhile RGGVY 12TH PLAN)</td> 
     </tr> 
     <tr> 
     <td style="font-size:24px;font-weight:400;text-align:center;padding-bottom:20px;">IN</td> 
     </tr> 
     <tr> 
     <td style="font-size:24px;font-weight:400;text-align:center;padding-bottom:25px;">..............DISTRICT</td> 
     </tr> 
     <tr> 
     <td style="font-size:24px;font-weight:400;text-align:center;padding-bottom:20px;">SUBMITTED TO</td> 
     </tr>  
     <tr> 
     <td style="font-size:24px;font-weight:400;text-align:center;padding-bottom:25px;">........................</td> 
     </tr>   
     <tr> 
      <td style="font-size:24px;font-weight:400;text-align:center;padding-bottom:20px;">SUBMITTED BY </td> 
     </tr> 
       <tr> 
     <td style="font-size:24px;font-weight:400;text-align:center;padding-bottom:30px;">........................</td> 
     </tr> 
     <tr> 
      <td style="font-style: italic; padding-bottom: 30px; padding-top: 100px;"> 
      <table style="width: 100%; margin-top: 50px;"> 
       <tr> 
       <td style="font-size:12px;padding-bottom:10px;font-weight:600">Report No (admin)/District(survey)/1st/2016-17/070</td> 
       <td style="font-size:12px;padding-bottom:10px;font-weight:600; text-align: right;">Dated: from survey (form1)</td> 
       </tr> 
      </table> 
      </td> 
     </tr>   
     <tr> 
      <td style="padding-bottom: 50px;"> 
      <table style="width: 100%;"> 
       <tr> 
       <td style="font-size:18px;font-weight:600">Location:</td> 
       </tr> 
       <tr> 
       <td style="font-size:16px;font-weight:600">Name of Village  : <%= @project.form1.try(:village_name) %></td> 
       <tr> 
       <td style="font-size:16px;font-weight:600">Census Code No  :      <%= @project.form1.try(:census_code_no) %> 
</td> 
       </tr> 

       <tr> 
       <td style="font-size:16px;font-weight:600">Name of Block  : <%= @project.form1.try(:block_name) %></td> 
       </tr> 
      </table> 
      </td> 
     </tr> 


      </tbody> 

    </table> 
</body> 

</html> 

輸出

enter image description here

+0

你有沒有檢查,這是否支持? CSS非常豐富,可能根本不支持CSS。 –

回答

0

htmltoword創業板目前提供的只是一些基本樣式的支持主要是在調整和表。
這裏是關於這個問題的GitHub問題https://github.com/karnov/htmltoword/issues/45
here是描述支持的樣式和類的文檔。
嘗試在可能的地方使用一些基本的HTML標籤而不是CSS樣式,它似乎是目前唯一可以讓您的單詞文檔樣式化的方法。