2015-05-25 52 views
0

我想生成PDF文件,其中應包含使用Rails 3的表格。我能夠生成PDF文件,但無法自定義此文件。使用Rails 3在PDF文件中創建自定義表格。

pdftable.html.erb

<div class="modal fade" id="payviewdetailsdiv4" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog modal-lg"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
       <h4 class="modal-title" id="myModalLabel">Payment Advice</h4> 
      </div> 
      <div class="modal-body"> 
       <div class="bgcolorwhite"> 
        <table class="table"> 
         <tr> 
         <th style="width: 50%; text-align:left;"> 
         <div class="pull-left"><img src="image/odishalogo.png" border="0" name="odishagovtlogo" style="width:60px; height:60px;"></div> 
         <div class="pull-left" style="padding:14px 0px 0px 10px;">Swargadwar, Puri Municipality <br /> 
        Government of Odisha</div> 
         <div class="clearfix"></div> 
         </th> 
         <th style="width:20%;"></th> 
         <th style="width: 30%; text-align:left; vertical-align: top;">Date: 14/03/2015<br /> 
        Bill No.: 123654789 </th>  
         </tr> 
         <tr> 
         <td colspan="3"> 
          <strong>Vendor Name</strong> : ABC<br> 
          <strong>Address</strong> : BBSR 
         </td> 
         </tr> 
         <tr> 
         <td colspan="3"> 
          <table style="border: 1px solid #000000; width:100%;"> 
            <tr style="border: 1px solid #000000; background-color:#7878C3; color:#FFF;"> 
             <td style="width:10%; border-right: 1px solid #000000;">Sl. No</td>  
             <td style="width:20%; border-right: 1px solid black;">Receipt No</td>  
             <td style="width:50%; border-right: 1px solid black;">Item</td> 
             <td style="width:20%; text-align:right; border-right: 1px solid black;">Amount</td>  
            </tr> 
            <tr style="border: 1px solid black;"> 
             <td style="border-right: 1px solid black;">1</td> 
             <td style="border-right: 1px solid black;">14/03/2015</td>  
             <td style="border-right: 1px solid black;">12121</td> 
             <td style="text-align:right; border-right: 1px solid black;"><i class="fa fa-rupee"></i> 1000</td>  
            </tr> 
            <tr> 
             <td colspan="2" style="vertical-align: top;"><strong>Note :</strong>Thanks for your business<br /> 
             <strong>Payment Mode :</strong> Cash/Cheque<br /> 
             <strong>Cheque No :</strong> 123456, Bank of India 
             </td> 
             <td style="text-align: right;">Total :<br /> Paid :<br /> Outstanding :<br /> Due Payment :<br /> Gross Total : </td>  
             <td style="text-align: right;"><i class="fa fa-rupee"></i> 1000<br /><i class="fa fa-rupee"></i> 200<br /><i class="fa fa-rupee"></i> 800<br /><i class="fa fa-rupee"></i> 120<br /><i class="fa fa-rupee"></i> 920</td>   
            </tr> 
          </table> 
         </td> 
         </tr> 
         <tr> 
         <td style="text-align:center;"> 
          <div style="height:70px;">&nbsp;</div><br /> 
          <span style="border-top:1px solid #000;">Puri Municipality</span> 
         </td> 
         <td></td> 
         <td style="text-align:center;"> 
          <div style="height:70px;">&nbsp;</div><br /> 
          <span style="border-top:1px solid #000;">Paid By</span> 
         </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

我想我生成的PDF裏面的上述文件格式file.Please檢查我的其他下列代碼。

table.html.erb

<table class="table table-bordered"> 
     <colgroup> 
      <col class="col-md-1 col-sm-1"> 
      <col class="col-md-3 col-sm-3"> 
      <col class="col-md-2 col-sm-2"> 
      <col class="col-md-2 col-sm-2"> 
      <col class="col-md-2 col-sm-2"> 
      <col class="col-md-2 col-sm-2"> 
     </colgroup> 
     <thead> 
      <tr> 
      <th class="text-center">Sl. No</th> 
      <th class="text-center">Date</th> 
      <th class="text-center">Total Claim</th> 
      <th class="text-center">Paid</th> 
      <th class="text-center">Outstanding</th> 
      <th class="text-center">Payment Advice</th> 
      </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td class="text-center">1</td> 
      <td class="text-center"><%= @add_payment.created_at %></td> 
      <td class="text-center"><%= @add_payment.total_claim %></td> 
      <td class="text-center"><i class="fa fa-rupee"></i><%= @add_payment.paid_amount %></td> 
      <td class="text-center"><i class="fa fa-rupee"></i><%= @outstanding %></td> 
      <td class="text-center"> 
       <div class="btn-group"> 
        <%= link_to download_pdf_path(:format => 'pdf'),:title => "" ,:class => "btn btn-xs btn-success","data-toggle" => "modal","data-target" => "#payviewdetailsdiv4" do %> 
        <!-- <a href="javascript:void(0)" title="" class="btn btn-xs btn-success" data-toggle="modal" data-target="#payviewdetailsdiv4"> 

        </a> --> 
        <i class="fa fa-eye"></i> 
        <% end %> 
       </div> 
       <div class="btn-group"> 
        <a href="javascript:void(0)" title="" class="btn btn-xs btn-danger"> 
         <i class="fa fa-download"></i> 
        </a> 
       </div> 
      </td> 
     </tr> 
     </tbody> 
     </table> 

控制器/ payment_controller.rb:

class PaymentsController < ApplicationController 
require "prawn/table" 
require "prawn" 
def download_pdf 
    @product = Product.all 
     respond_to do |format| 
      format.pdf do 
      pdf = Prawn::Document.new 
      table_data = Array.new 
      table_data << ["Sl no", "Receipt No","Item","Amount"] 
      @product.each do |p| 
       table_data << [p.id, p.Receipt_no,p.item,p.Amount] 
      end 
      pdf.table(table_data, :width => 500, :cell_style => { :inline_format => true }) 
      send_data pdf.render, filename: 'test.pdf', type: 'application/pdf',:disposition => 'inline' 
      end 
     end 
end 
end 

我使用兩個寶石即 - gem 'prawn', '~> 1.3.0'gem 'prawn-table', '~> 0.2.1'用於產生PDF文件。這裏我的要求是我需要PDF文件,但這個文件中的表格格式應該按照我的第一個代碼顯示(例如pdftable.html.erb)。請幫助我使用Rails 3添加此類型的佈局。

回答

0

您可以使用wicked_pdf gem將html轉換爲pdf。這對蝦是不可能的。

的Gemfile

gem 'wicked_pdf' 
gem 'wkhtmltopdf-binary' 

rails generate wicked_pdf 

payments_controller.rb

def download_pdf 
    @product = Product.all 
    respond_to do |format| 
    format.pdf{ render pdf: "file_name"} 
    end 
end 

https://github.com/mileszs/wicked_pdf

替換配置/初始化/ wicked_pdf.rb與路徑非常wkhtmltopdf安裝exe_path並且您可以通過獲取以下命令。

which wkhtmltopdf 

重命名視圖文件download_pdf.pdf.erb

+0

@ user123:好的,假設我添加了wicked_pdf gem.Now能否幫助我編輯download_pdf方法,以便在點擊該鏈接後將pdftable.html.erb轉換爲pdf文件。 –

+0

我已經這樣做了。看看 –

+0

@ user123:好的,讓我來檢查一下。 –

0
def download_pdf 
     @product = Product.all 
    end 

download_pdf.pdf.prawn 創建文件名的文件在該文件中,你可以使用寶石大蝦建表 http://prawnpdf.org/manual.pdf

相關問題