1

I'm using rails and prawn pdf to generate report in pdf format

I would like to ask is it possible to generate 2 or more pdf documents from one controller (I'm using respond_to to generate pdf)

below is currently my controller that can generate only one pdf, I need to create pdf with different format for owner and end users

thank you

  def show
    @family = Family.find(params[:id])

    
    respond_to do |format|
      format.html
      format.pdf do
        pdf = familyPdf.new(@family) 
        send_data pdf.render, filename: "family_#{@family.father_name}.pdf",
                              type: "application/pdf",
                              disposition: "inline"
      end
    end
  end
widjajayd
  • 6,090
  • 4
  • 30
  • 41

2 Answers2

0

You can download one file at a time. To generate and send more then one file in an action, You have to merge them in zip folder. To generate zip file, you may use rubyzip gem https://github.com/rubyzip/rubyzip

Here is also a nice article to do so http://thinkingeek.com/2013/11/15/create-temporary-zip-file-send-response-rails/

Shahzad Tariq
  • 2,767
  • 1
  • 22
  • 31
-1

You could use an if/else statement

def show
  @family = Family.find(params[:id])


  respond_to do |format|
    format.html
    format.pdf do
      if current_user.is_owner?
        pdf = familyPdf.new(@family) 
        send_data pdf.render, filename: "family_#{@family.father_name}.pdf",
                              type: "application/pdf",
                              disposition: "inline"
        return
      else
        ...
        code for formatting pdf for end user goes here
        ...
      end
    end
  end
end
  • thank you, for answer , but is there possible respond to from one controller can generate pdf_number_one.pdf and pdf_number_two.pdf , if this possible, how can I create link for the first pdf and second pdf, the one that I understand currently is family_path(family, format: "pdf") for the first pdf – widjajayd May 07 '15 at 04:18