如何使用ruby on rails上的prawn基于搜索生成PDF?

时间:2015-08-10 00:30:55

标签: ruby-on-rails prawn ransack prawnto

我想要实现的是,以PDF格式生成的文档应该基于搜索引擎。我使用ransack作为工具来执行搜索条件。我可以使用下面的代码生成PDF格式的文档,但它会生成整个记录,尽管搜索引擎工作正常。如何根据特定标准生成PDF?非常感谢

这是我的控制器

    def index
           @search = Student.search(params[:q])
           @students = @search.result.paginate(:page => params[:page], :per_page => 5)
           @search.build_condition if @search.conditions.empty?
           @search.build_sort if @search.sorts.empty?
           @total_students = Student.count
           @numMale = Student.where(:gender => "Male").count
           @numFemale = Student.where(:gender => "Female").count

      respond_to do |format|
        format.html 
        format.json { render json: @students }
        format.pdf {@search.result}
      end
  end

这是apps / views / students / index.pdf.prawn

header = ['First Name', 'Last name', 'Date Of Birth', 'Gender', 'District','Sub distrcit','Village','Hamlet']
    @search = Student.search(params[:q])
    @students = @search.result.paginate(:page => params[:page], :per_page => 5)
    students = @students.search.result.map do |student| 
       [  
        student.fname,
        student.familyName,
        student.dob,
        student.gender,
        student.district.name,
        student.subdistrict.name,
        student.suco.name,
        student.village.name

       ]
    end

    pdf.table [header] + students, :header => true, :width => pdf.bounds.width do
          row(-5..-1).column(2).align = :right
          row(0).style :font_style => :bold
          row(-1).style :font_style => :bold
        end 

这是我的apps / views / students / index.html.erb

<%- model_class = Student -%>
<div class="page-header">
  <h1>List of Learners</h1>
</div>
<fieldset class="scheduler-border">
    <legend class="scheduler-border">Total Students</legend>
<div class="container">
<p>Number of Students:<%= @total_students%></p>
<p>Number of Males:<%= @numMale%></p>
<p>Number of Females:<%= @numFemale%></p>
</div>
</fieldset>
<fieldset class="scheduler-border">
    <legend class="scheduler-border">Search Students</legend>
    <div id="advancesearch" style='display:none'>
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">Advance Search</legend>
        <%= search_form_for @search, url: search_students_path, method: :post do |f| %>
          <%= f.condition_fields do |c| %>
            <%= render "condition_fields", f: c %>
          <% end %>
          <p><%= link_to_add_fields "Add Conditions", f, :condition %></p>
          <div class="col-sm-4">
            Sort:
            <%= f.sort_fields do |s| %>
              <%= s.sort_select Hash.new, class: "form-control" %>
            <% end %>
          </div>
          <br>
          <div class="actions"><%= f.submit "Filter", class: "btn btn-primary" %></div>
        <% end %>
        </fieldset>
        </div>
          <%= search_form_for @search, url: search_students_path, method: :post do |f| %>
          <div class="col-sm-4">
          <%= f.text_field :fname_cont, class: "form-control", :placeholder =>"Type first name" %>
          </div>
          <div class="col-sm-4">
          <%= f.text_field :familyName_cont, class: "form-control" , :placeholder =>"Type Family name"%>
          </div>
          <div class="col-sm-4">
          <%= f.submit "Search", class: "btn btn-primary" %>
          </div>
          <% end %>
          <div class="col-sm-4">
          <br>
          <input type="button" name="answer" class="btn btn-primary" value="Advance Search" onclick="showDiv()" />
          </div>
         </div>
          <br>

</fieldset>

<div class="pagination">
  <%= will_paginate @students, renderer: BootstrapPagination::Rails %>
</div>
<div class="pdf_link">
    <%= link_to "Generate PDF", students_path(@students, :format => "pdf"), class: "btn btn-primary" %>
</div>
<br>
<table class="table table-striped">
  <thead>
    <tr>
      <th><%= sort_link @search, :id, "Student Number" %></th>
      <th><%= sort_link @search, :fname, "First name" %></th>
      <th><%= sort_link @search, :tname, "Middle Name" %></th>
      <th><%= sort_link @search, :familyName, "Surname" %></th>
      <th><%= sort_link @search, :gender, "Gender" %></th>
      <th><%=t '.actions', :default => t("helpers.actions") %></th>
    </tr>
  </thead>
  <tbody>
    <% @students.each do |student| %>
      <tr>
        <td><%= link_to student.student_number, student_path(student) %></td>
        <td><%= student.fname %></td>
        <td><%= student.tname %></td>
        <td><%= student.familyName %></td>
        <td><%= student.gender %></td>
        <td>
          <%= link_to t('.edit', :default => t("helpers.links.edit")),
                      edit_student_path(student), :class => 'btn btn-default btn-xs' %>
          <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                      student_path(student),
                      :method => :delete,
                      :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
                      :class => 'btn btn-xs btn-danger' %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

<%= link_to t('.new', :default => t("helpers.links.new")),
            new_student_path,
            :class => 'btn btn-primary' %>

1 个答案:

答案 0 :(得分:-1)

我只是环顾四周,我在这个链接中找到了这个有用的答案:

Ransack export results to CSV

只需添加以下代码即可。

<div class="pdf_link">
    <%= link_to "Generate PDF", students_path(params.merge(:format => "pdf")), class: "btn btn-primary" %>
</div>