0

How do I download multiple pdf using Wicked-pdf

Below is the line of code i am using here,

@awb_orders = Spree::Order.where('created_at >= ? AND created_at <= ?', DateTime.now-1.month, DateTime.now).where(:state => 'complete').order("created_at DESC")
@awb_orders.each do |order|
  @order = order
  respond_to do |format|
    format.html
    format.pdf do
      render pdf: "file_name_#{DateTime.now}",
        save_to_file: Rails.root.join('public', "invoice_#{DateTime.now}.pdf")
     end
   end
 end

I am getting the below result

Render and/or redirect were called multiple times in this action...... "redirect_to(...) and return".

Can anyone please help me where I am going wrong?

Unixmonkey
  • 18,485
  • 7
  • 55
  • 78

1 Answers1

0

In a normal Rails controller, you cannot call any render method more than once, and here you are doing it for as many orders you have.

Instead, I think you'll want to either reorganize your data so that all invoices are generated in a single pdf template, or you generate the separate pdfs outside of the respond_to/render loop, then merge them together into a either a single PDF or a zip file containing them all.

Here's a rough outline of how that might look:

@awb_orders = Spree::Order.where('created_at >= ? AND created_at <= ?', DateTime.now-1.month, DateTime.now).where(:state => 'complete').order("created_at DESC")
respond_to do |format|
  format.html
  format.pdf do
    cpdf = CombinePDF.new
    @awb_orders.each do |order|
      pdfdata = render_to_string(pdf: "order_#{order.id}"
      cpdf << CombinePDF.parse(pdfdata)
    end
    send_data cpdf.to_pdf, filename: "combined.pdf", type: "application/pdf"
  end
end
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78