Ruby on Rails & Prawn PDF - Create a customer list

I'm trying to create a pdf report using shrimp, I can get it to report on the show’s action quite easily by passing one identifier, but I want to create it with each entry. Like a standard flyover index page. Using rails, it will look like this:

<% @customer.each do |customer| %>
<%= customer.id %>
<%= customer.name %>
<%end%>

Easy!

But I'm not sure how to do this with shrimp ..

Sort of:

def index
 @customer = Customer.all
  respond_to do |format|
  format.html
   Prawn::Document.generate("customer_list.pdf") do |pdf|
   pdf.text "#{@customer.id} "
    pdf.text "#{@customer.name} "  
       end
    end
end

Which is clearly wrong.

Any ideas? Thank.

+5
source share
3 answers

This is easy to do with shrimp , Gemfile => gem 'prawn', bundle

will say that you have a Client :

customers_controller.rb

def show
   @customer = Customer.find(params[:id])
   respond_to do |format|
     format.html
     format.pdf do
        pdf = CustomerPdf.new(@customer)
        send_data pdf.render, filename: "customer_#{id}.pdf",
                              type: "application/pdf",
                              disposition: "inline"
     end
   end
end

pdfs apps customer_pdf.rb

class CustomerPdf< Prawn::Document

  def initialize(customer)
    super()
    @customer = customer
    text "Id\##{@customer.id}"
    text "Name\##{@customer.name}"
  end

end

show.html.erb

  <div class="pdf_link">
    <%= link_to "E-version", customer_path(@customer, :format => "pdf") %>
  </div>

EDIT:

pdf config/initializers/mime_types.rb

Mime::Type.register "application/pdf", :pdf
+5

, . (! Rails core developer) book. . , .

+2

How do i do this:

class CustomerPdf< Prawn::Document

 def initialize(customer)
  super(page_size: "A4", page_layout: :portrait)
  @customers = customer
  bullet_list
 end

 def bullet_list
  @customers.each do |customer|
      text "•#{customer.id}- #{customer.name} ", style: :bold
    move_down 5
  end
 end

end
+1
source

All Articles