2014-09-24 2 views
1

웹 애플리케이션에서 나는 새우와 함께 청구서를 생성합니다.우편 번호의 새우 파일에서 PDF 파일 다운로드

나는 지폐로 된 파일로 모든 청구서를 다운로드하고 싶습니다!

def download 
    bills = Bill.search(params[:q]).result(distinct: true).paginate(:page => params[:page], limit: 20, order: "paid_at DESC") 

    bills.each do |b| 
    Prawn::Document.generate("#{Rails.root}/public/pdfs/web_#{b.reference}.pdf") 
    end 


    require 'rubygems' 
    require 'zip' 
    zipfile_name = "#{Rails.root}/public/pdfs/factures.zip" 
    Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| 
    bills.each do |bill| 
     zipfile.add("web_#{bill.reference}.pdf", "#{Rails.root}/public/pdfs/web_#{bill.reference}.pdf") 
    end 
    end 

    path = File.join(Rails.root, "public", "pdfs") 
    send_file File.join(path, "factures.zip") 
end 

는하지만 PDF 파일 내가 PDF를 생성 할 수 있습니다은 "* .prawn"파일을 지정하지 않기 때문에 비어 :

내가 그랬어!

아무에게도 그렇게 할 수 있습니까?

감사

편집

내 show.pdf.prawn

prawn_document() do |pdf| 


pdf.image "#{Rails.root}/app/assets/images/logo.jpg" 
    pdf.move_down 30 

    pdf.move_down 10 
    pdf.text "<b>Email :</b> <a href='mailto:[email protected]'>[email protected]</a>", inline_format: true 
    pdf.text "<b>Site :</b> <a href='http://www.mtaville.fr'>www.mtaville.fr</a>", inline_format: true 

    pdf.move_up 230 
    pdf.table [["Facture N° #{@bill.reference}"],["Date : #{ldate(@bill.paid_at, format: :short2)}"]], position: :right do 
    cells.style do |c| 
     c.background_color = "2857BE" 
     c.text_color = "FFFFFF" 
     c.border_color = "2857BE" 
     c.align = :center 
    end 
    end 

    pdf.move_down 30 
    pdf.table [["CLIENT"]], position: :right do 
    cells.style do |c| 
     c.background_color = "2857BE" 
     c.text_color = "FFFFFF" 
     c.border_color = "2857BE" 
     c.width = 280 
    end 
    end 

    pdf.bounding_box([260, 600], :width => 280, :height => 80) do 
    pdf.text "#{@bill.billable.name}", style: :bold 
    pdf.text "#{@bill.billable.address_1} #{@bill.billable.address_2}" 
    pdf.text "#{@bill.billable.zipcode} - <b>#{@bill.billable.town}</b>", inline_format: true 
    end 

    pdf.move_down 100 
    pdf.text "<b>Objet :</b> Facture n° #{@bill.reference}", inline_format: true 

    if @bill.billable_type == 'Subscription' 
    pdf.move_down 10 
    data = [ 
     ["Produit", "Prix unitaire HT", "Prix total HT"], 
     ["Renouvellement de souscription à votre espace privé", "#{number_to_currency(@bill.total)}", "#{number_to_currency @bill.total}" ] 
    ] 
    pdf.table data, width: 540 do 
     cells.style do |c| 
     c.background_color = (c.row == 0)? "2857BE" : "ffffff" 
     c.text_color = (c.row == 0)? "ffffff" : "000000" 
     c.font_style = (c.row == 0)? :bold : :normal 
     end 
    end 
    end 
    if @bill.billable_type == 'Order' 
    pdf.move_down 10 
    data = [ 
     ["Produit", "Prix unitaire HT", "Prix total HT"], 
     ["Annonce n°#{@bill.billable.ad.id}", "#{number_to_currency(@bill.total)}", "#{number_to_currency @bill.total}" ] 
    ] 
    pdf.table data, width: 540 do 
     cells.style do |c| 
     c.background_color = (c.row == 0)? "2857BE" : "ffffff" 
     c.text_color = (c.row == 0)? "ffffff" : "000000" 
     c.font_style = (c.row == 0)? :bold : :normal 
     end 
    end 
    end 

    pdf.move_down 20 
    data = [ 
    ["Total HT", "#{number_to_currency(@bill.total)}" ], 
    ["Total TVA #{@bill.tva}%", "#{number_to_currency(@bill.amount_vat)}"], 
    ["Total TTC", "#{number_to_currency(@bill.amount_inclusive_of_tax)}"] 
    ] 
    pdf.table data, position: :right do 
    cells.style do |c| 
     c.font_style = (c.row % 2 == 0)? :bold : :normal 
     c.width = 140 
     c.align = (c.column == 1)? :right : :left 
    end 
    end 

    pdf.move_down 20 
    data = [["NET A PAYER", "#{number_to_currency(0)}"]] 
    pdf.table data, position: :right do 
    cells.style do |c| 
     c.font_style = :bold 
     c.background_color = "2857BE" 
     c.text_color = "ffffff" 
     c.width = 140 
     c.align = (c.column == 1)? :right : :left 
    end 
    end 

end 

** 답변 ** 여기

내 컨트롤러에서 fonction입니다 :

def download 
    bills = Bill.search(params[:q]).result(distinct: true).paginate(:page => params[:page], limit: 20, order: "paid_at DESC") 

    require 'rubygems' 
    require 'zip' 
    require 'bill' 
    zipfile_name = "#{Rails.root}/tmp/pdfs/factures.zip" 
    Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| 
     bills.each do |bill| 
     temp_pdf = Tempfile.new("web_#{bill.reference}.pdf") 
     temp_pdf.binmode 
     temp_prawn_pdf = BillPdf.new(bill) 
     temp_pdf.write temp_prawn_pdf.render 
     temp_pdf.rewind 
     zipfile.add("web_#{bill.reference}.pdf", "#{temp_pdf.path}") 
     temp_pdf.close 
     end 
    end 

    path = File.join(Rails.root, "tmp", "pdfs") 
    send_file File.join(path, "factures.zip") 
    end 

그리고 내 클래스 :

class BillPdf < Prawn::Document 
    include ActionView::Helpers::TranslationHelper 
    include ActionView::Helpers::NumberHelper 
    def initialize(bill) 
    super() 
    image "#{Rails.root}/app/assets/images/logo.jpg" 

    move_down 10 
    text "<b>N° TVA :</b> TVA", inline_format: true 
    text "<b>Siret :</b> 789 618 691 00016", inline_format: true 
    text "<b>Site :</b> <a href='http://www.mtaville.fr'>www.mtaville.fr</a>", inline_format: true 

    move_up 230 
    table [["Facture N° #{bill.reference}"],["Date : #{l(bill.paid_at, format: :short2)}"]], position: :right do 
     cells.style do |c| 
     c.background_color = "2857BE" 
     c.text_color = "FFFFFF" 
     c.border_color = "2857BE" 
     c.align = :center 
     end 
    end 

    move_down 30 
    table [["CLIENT"]], position: :right do 
     cells.style do |c| 
     c.background_color = "2857BE" 
     c.text_color = "FFFFFF" 
     c.border_color = "2857BE" 
     c.width = 280 
     end 
    end 

    bounding_box([260, 600], :width => 280, :height => 80) do 
     text "#{bill.billable.name}", style: :bold 
     text "#{bill.billable.address_1} #{bill.billable.address_2}" 
     text "#{bill.billable.zipcode} - <b>#{bill.billable.town}</b>", inline_format: true 
    end 

    move_down 100 
    text "<b>Objet :</b> Facture n° #{bill.reference}", inline_format: true 

    if bill.billable_type == 'Subscription' 
     move_down 10 
     data = [ 
     ["Produit", "Prix unitaire HT", "Prix total HT"], 
     ["Renouvellement de souscription à votre espace privé", "#{number_to_currency(bill.total)}", "#{number_to_currency bill.total}" ] 
     ] 
     table data, width: 540 do 
     cells.style do |c| 
      c.background_color = (c.row == 0)? "2857BE" : "ffffff" 
      c.text_color = (c.row == 0)? "ffffff" : "000000" 
      c.font_style = (c.row == 0)? :bold : :normal 
     end 
     end 
    end 
    if bill.billable_type == 'Order' 
     move_down 10 
     data = [ 
     ["Produit", "Prix unitaire HT", "Prix total HT"], 
     ["Annonce n°#{bill.billable.ad.id}", "#{number_to_currency(bill.total)}", "#{number_to_currency bill.total}" ] 
     ] 
     table data, width: 540 do 
     cells.style do |c| 
      c.background_color = (c.row == 0)? "2857BE" : "ffffff" 
      c.text_color = (c.row == 0)? "ffffff" : "000000" 
      c.font_style = (c.row == 0)? :bold : :normal 
     end 
     end 
    end 

    move_down 20 
    data = [ 
     ["Total HT", "#{number_to_currency(bill.total)}" ], 
     ["Total TVA #{bill.tva}%", "#{number_to_currency(bill.amount_vat)}"], 
     ["Total TTC", "#{number_to_currency(bill.amount_inclusive_of_tax)}"] 
    ] 
    table data, position: :right do 
     cells.style do |c| 
     c.font_style = (c.row % 2 == 0)? :bold : :normal 
     c.width = 140 
     c.align = (c.column == 1)? :right : :left 
     end 
    end 

    move_down 20 
    data = [["NET A PAYER", "#{number_to_currency(0)}"]] 
    table data, position: :right do 
     cells.style do |c| 
     c.font_style = :bold 
     c.background_color = "2857BE" 
     c.text_color = "ffffff" 
     c.width = 140 
     c.align = (c.column == 1)? :right : :left 
     end 
    end 
    end 

end 
+0

임시 파일을 생성하고 zip에 추가하려면 'Tempfile'을 사용할 수 있습니다. 사용이 끝나면 파일을 닫아야합니다. – kobaltz

+0

그래,하지만 어떻게 그 임시 파일에 내용을 추가 할 수 있습니까? – p0k3

+0

내 대답보기,이 생각을 확장했습니다. – kobaltz

답변

3

내 의견 더에 확장하려면, 아래의 코드 리팩토링을 참조하십시오. 보안상의 이유로 위험한 공개 디렉토리에 파일을 추가하는 대신 Tempfile을 사용할 수 있습니다. 이 부분을 약간 조정해야 할 수도 있지만 Tempfile 뒤에 기본 아이디어를 제공해야합니다.

def download 
    bills = Bill.search(params[:q]).result(distinct: true).paginate(:page => params[:page], limit: 20, order: "paid_at DESC") 

    require 'rubygems' 
    require 'zip' 
    zipfile_name = Tempfile.new(["#{Rails.root}/tmp/factures", '.zip']) 
    Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| 
    bills.each do |bill| 
     temp_pdf = Tempfile.new(["basic_questions_#{Time.now.to_i}", '.pdf']) 
     temp_pdf.binmode 
     temp_prawn_pdf = Prawn::Document.new(#WHATEVER YOUR PARAMETERS ARE) 
     temp_pdf.write temp_prawn_pdf.render 
     temp_pdf.rewind 
     zipfile.add("web_#{bill.reference}.pdf", "#{temp_pdf.path}") 
     temp_pdf.close 
    end 
    end 

    path = File.join(Rails.root, "public", "pdfs") 
    send_file File.join(path, "factures.zip") 
ensure 
    zipfile_name.close 
end 
+0

이것은 오랜 시간이 걸릴 수 있으므로 이것을'delayed_job' 또는 유사 항목으로 옮길 수 있습니다. 파일이 준비되면 언제든지 다운로드를 시작하거나 파일을 다운로드 할 준비가 된 아약스를 보낼 수 있습니다. 정말 모든 것이 가장 잘 들어 맞는지에 대한 응용 프로그램의 작성 범위에 달려 있습니다. – kobaltz

+0

보안을 위해 가져갑니다. 하지만 내 PDF는 모두 비어 있습니다. – p0k3

+0

'temp_prawn_pdf = Prawn :: Document.new (#WHATEVER 매개 변수가 있습니다.)'행에서, PDF 생성기가 무엇이든간에'새우 :: 문서 .....'를 설정해야합니다. 예를 들어, 문서가 성공적으로 생성 된 다른 코드를보고 그 코드를 삽입하십시오. 필요한 매개 변수를 수정합니다. – kobaltz