使用Rails 3在PDF文件中创建自定义表。

时间:2015-05-25 11:14:39

标签: ruby pdf ruby-on-rails-3.2

我想使用Rails 3生成应包含下表的PDF文件。我能够生成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文件中获取上述文件格式。请检查我的其他以下代码。

  

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,即gem 'prawn', '~> 1.3.0'gem 'prawn-table', '~> 0.2.1'来生成PDF文件。这里我的要求是我需要PDF文件,但是这个文件中的表格格式应该按照我的第一个代码显示(即pdftable.html.erb)。请帮我用Rails 3添加这种类型的布局。

1 个答案:

答案 0 :(得分:0)

  def download_pdf
        @product = Product.all
    end

创建名为download_pdf.pdf.prawn的文件 在该文件中,您可以使用prawn gem构建表 http://prawnpdf.org/manual.pdf