2012-02-27 2 views
7

ActionMailer에서 새우 pdf를 첨부 파일로 렌더링하는 방법은 무엇입니까? 내가 delayed_job을 사용하고 이해하지 못한다면 어떻게 pdf 파일을 액션 메일러 (컨트롤러가 아닌)에서 렌더링 할 수 있을까요? 어떤 형식을 사용해야합니까?Rails 3 렌더링 새우 pdf 파일 ActionMailer

답변

7

Prawn에게 PDF를 문자열로 렌더링 한 다음 전자 메일의 첨부 파일로 추가하면됩니다. 첨부 파일에 대한 자세한 내용은 ActionMailer docs을 참조하십시오.

class ReportPdf 
    def initialize(report) 
    @report = report 
    end 

    def render 
    doc = Prawn::Document.new 

    # Draw some stuff... 
    doc.draw_text @report.title, :at => [100, 100], :size => 32 

    # Return the PDF, rendered to a string 
    doc.render 
    end 
end 

class MyPdfMailer < ActionMailer::Base 
    def report(report_id, recipient_email) 
    report = Report.find(report_id) 

    report_pdf_view = ReportPdf.new(report) 

    report_pdf_content = report_pdf_view.render() 

    attachments['report.pdf'] = { 
     mime_type: 'application/pdf', 
     content: report_pdf_content 
    } 
    mail(:to => recipient_email, :subject => "Your report is attached") 
    end 
end 
+0

이미 뷰/송장/show.pdf.prawn 있습니다. InvoicesController가 성공적으로 렌더링합니다. 메일러에서 render_to_string을 사용하여 렌더링하려했는데 PDF가 손상되었습니다. 이 기존 뷰 파일을 렌더링하는 방법? render_to_string의 형식 또는 형식을 지정해야 할 수도 있습니다. – maxs

0

내 솔루션 : I 메일 기능과 여러 부분 이메일 블록을 쓰기 정확하지 않았기 때문에

render_to_string('invoices/show.pdf', :type => :prawn) 

PDF가 손상되었습니다

다음은 예입니다.

3

PRAWN에 대한 RailsCast를 따랐습니다. 이미 말한 내용과 비슷한 목적을 달성하기 위해 첨부 파일 이름을 설정 한 다음 PDF를 작성했습니다.

InvoiceMailer :

def invoice_email(invoice) 
    @invoice = invoice 
    @user = @invoice.user 
    attachments["#{@invoice.id}.pdf"] = InvoicePdf.new(@invoice, view_context).render 
    mail(:to => @invoice.user.email, 
     :subject => "Invoice # #{@invoice.id}") 
    end