Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to generate PDF based on search using prawn in ruby on rails

Francisco Quinones asked in Gems / Libraries

So Chris im using prawn pdf in my app.
Now im adding some search form to filter by date range. All works fine and I get my filter table on my index. my problem is with prawn that wont hold the filter records

my form filter range

        <%= form_tag({controller: "accidents", action: "index"}, {:class => 'navbar-form navbar-left', method: "get"}) do %>
              <%= text_field_tag :from_date, params[:from_date], class: "form-control", placeholder: "Fecha Inicial" %>
              <%= text_field_tag :to_date, params[:to_date], class: "form-control", placeholder: "Fecha Final" %>
              <%= submit_tag 'Buscar', class: "btn btn-primary" %>
        <% end %>

my controller code

  def index
    if params[:from_date] && params[:to_date].present?
       from_date = Date.strptime(params[:from_date], "%d/%m/%Y")
       to_date = Date.strptime(params[:to_date], "%d/%m/%Y") + 1.day
       @accidents = @project.accidents.sorting.where(:date => from_date..to_date)
       .paginate(:page => params[:accidents], :per_page => 10)
    else
        @accidents = @project.accidents.sorting.paginate(:page => params[:accidents], :per_page => 10)
    end
    respond_to do |format|
        format.html
        # format.csv { send_data  @accidents.to_csv}
        format.pdf do
          pdf = AccidentsReportPdf.new(@accidents,@project)
          send_data pdf.render,filename: "accidente_reporte_#{@project.name}.pdf",
                              type: "application/pdf",
                              disposition: "inline"
      end
    end
  end

This my index filter with a range of date

http://companydemo.lvh.me:3000/projects/conroy-schamberger-and-crooks/accidents?utf8=%E2%9C%93&from_date=01%2F01%2F2016&to_date=31%2F01%2F2016&commit=Buscar

when I go to see the pdf version

<%= link_to Report, project_incidents_path(format: "pdf"), :class=> "btn" %>

I get

  http://companydemo.lvh.me:3000/projects/conroy-schamberger-and-crooks/accidents.pdf

maybe I need to past the dates to the link_to pdf. If so how is the right way to pass that data.

Reply

I found that this works

<%= link_to Report, project_incidents_path(params.merge(format: "pdf")), :class=> "btn" %>

I get this

http://companydemo.lvh.me:3000/projects/conroy-schamberger-and-crooks/incidents?action=index&commit=Buscar&controller=incidents&format=pdf&from_date=01%2F01%2F2016&project_id=conroy-schamberger-and-crooks&to_date=31%2F01%2F2016&utf8=%E2%9C%93

but I dont know if is the best way.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 76,990+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.