2013-10-31 4 views
7

안녕하세요. PDF 영수증 다운로드를 구현하려고합니다. Prawn을 Rails 4 앱과 통합하는 방법을 잘 모르겠습니다.이 작업을 수행하는 방법에 대한 자습서를 찾을 수 없었습니다. 제가 한 일들을 아래에서보십시오. 누군가 제게 몇 가지 조언을 해주십시오.Prawn for Rails 4를 설치하는 방법

1 추가 새우 보석 및 번들 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을 렌더링하는

이 추가 된 컨트롤러 코드를 설치 않았다 뷰에서 LINK_TO 코드가 있습니다. 보기> App> PDF

<%= link_to "Download PDF", order_path(@order, format: pdf) %> 
+2

이동 : 누군가 당신을 돕기 위해 시간을 할애합니다. 나는 그가 약간의 의견 또는 받아 들여진 대답을 평가할 것이라고 확신한다. – Joris

답변

4

어떤 부분에 도움이 필요할지 모르겠지만 여기에 제가 한 방법입니다. 아래 코드에서 영수증의 pdf를 만들고 데이터베이스에 저장하고 있습니다. 출력은 다음과 같습니다. Sample Receipt

아마도 도움이 될 수 있습니다.

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 

다음은 첨부 파일을 업로드하고 다운로드하는 컨트롤러입니다.

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 

행운을 빈다. 좀 더 구체적인 것을 필요로한다면 알려주세요.

관련 문제