Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install Prawn for Rails 4

Hi I'm trying to implement downloading a PDF receipt. I'm not sure how to integrate Prawn with Rails 4 app and I couldn't find any tutorials on how to do this. Please see below for things that I have done. Could someone please suggest some articles to me or some tips.

1 Added Prawn gem and did bundle install

2 Added controller code to render PDF

respond_to do |format|
  format.html
  format.pdf do
    pdf = OrderPdf.new(@order, view_context)
    send_data pdf.render, filename: "order_#{@order.order_number}.pdf",
                          type: "application/pdf",
                          disposition: "inline"
  end
end

3 Have a link_to code in the view. The view is located App > PDF

<%= link_to "Download PDF", order_path(@order, format: pdf) %>
like image 211
Pan Wangperawong Avatar asked Oct 31 '13 21:10

Pan Wangperawong


People also ask

How to install Prawn?

Installing Prawn Prawn is distributed via RubyGems, and can be installed the usual way that you install gems: by simply typing gem install prawn on the command line.


1 Answers

I'm not sure which part you need help with, but here is how I've done it. In the code below, I am creating a pdf of a receipt and storing it in the database. The output looks like this - Sample Receipt

Perhaps it may be of some help.

class Omni::ReceiptWorksheet < Omni::Receipt

  def print(receipt)
    pdf = header receipt

    data = []
    data[0] = ["PO Nbr","Carton Nbr","Sku Nbr","Sku Description","S/U Open","S/U per Pack","Packs Open", "Packs Received"]

    receipt.receipt_details.each_with_index do |detail,i|
      selling_units = detail.purchase_detail.selling_units_approved - detail.purchase_detail.selling_units_received - detail.purchase_detail.selling_units_cancelled
      data[i+1] = [detail.purchase.purchase_nbr,' ', detail.sku.sku_nbr, detail.sku.display, selling_units, detail.receipt_pack_size, selling_units / detail.receipt_pack_size, ' ']
    end

    pdf.move_down 110

    pdf.table(data) do |t|
      t.style(t.row(0), :background_color => '0075C9')
      t.header = true
    end

    pdf.number_pages "page <page> of <total>", { :at => [pdf.bounds.right - 150, 0], width: 150, align: :right, page_filter: (1..50), start_count_at: 1, color: "002B82" }
    attach StringIO.new(pdf.render), "receiving_worksheet#{Date.today}.pdf", receipt
  end

  def header(receipt)
    pdf = Prawn::Document.new
    pdf.font_size = 12
    pdf.draw_text "Printed on: #{Date.today}", at: [0, 670]
    pdf.draw_text "Receiving Worksheet", at: [220, 670]
    pdf.draw_text "Page 1", at: [480, 670]

    pdf.draw_text "Receipt #: #{receipt.receipt_nbr}", at: [0, 650]
    pdf.draw_text "Receipt Date: #{Date.today}", at: [400, 650]

    pdf.draw_text "Receiving Location: #{receipt.location_display}", at: [0, 640]
    pdf.draw_text "Carrier Name: #{receipt.carrier_supplier.display}", at: [0, 620]
    pdf.draw_text "Bill of Lading: #{receipt.bill_of_lading_number}", at: [450, 620]
    pdf
  end

  def attach(file, file_name, receipt)
    attachment = Buildit::Attachment.create(
      attachable_type: "Omni::Receipt",
      attachable_id: receipt.receipt_id,
      file_name: file_name,
      mime_type: 'application/pdf',
      byte_size:  file.size,
      locale:   'en',
      is_enabled: true
    )

    Buildit::Content.create(
    contentable_type: "Buildit::Attachment",
    contentable_id:  attachment.attachment_id,
    data: file.read
    )
  end

end

Below is my controller for uploading and downloading the attachment.

class ContentController < ActionController::Base

  def download

    content     = Buildit::Content.find_by_content_id(params[:file_id])
    contentable = content.contentable
    file_name   = (contentable ? contentable.file_name : 'file')

    send_data content.data, :disposition => 'attachment', :filename => file_name

  end # def download

  def upload
    begin
      content = Buildit::Content.create(
        data: params[:file].read
      )

      result = {
        success:        true,
        content_id:     content.content_id,
        file_name:      params[:file].original_filename,
        mime_type:      params[:file].content_type,
        byte_size:      params[:file].size
      }
    rescue
      result = {success: false}
    end

    render text: result.to_json, status: 200
  end # def upload

end # class ContentController

Good luck! Let me know if you need something more specific.

like image 158
Aaron Henderson Avatar answered Sep 18 '22 14:09

Aaron Henderson