2016-08-09 4 views
0

레일 4에서 나는 pdf 생성시 gem 'wicked_pdf', '1.0.3'gem 'wkhtmltopdf-binary', '0.9.9.3'을 사용하고 있습니다. 나는 Rails 4 - Wicked PDF 파일을 이메일로 보내기

def send_invoice_pdf_mail(user, file_path) 
    filename = file_path.split("/").last 
    attachments["#{filename}"] = File.read(file_path) 
    mail(:to =>user.email, :subject => "File has been sent") do |format| 
    format.html { render :partial =>"/emails/users/sample", :locals=>{:user=>user}, :layout=>"email"} 
    end 
end 

지금 문제가 둘 다, 나는 우편물에서 Email.deliver_sample_file_mail(@user, "#{Rails.root}/public/download_pdfs/#{@user.id}_file.pdf")

컨트롤러에이 줄을 추가하여 메일을 통해이 파일을 보내고, 여기에서 파일로

respond_to do |format| 
    format.html 
    format.pdf do 
    render :pdf => "#{@user.id}_file", 
     :disposition => "attachment", 
     :save_to_file => Rails.root.join('public/download_pdfs', "#{@user.id}_file.pdf"), 
     :layout => false 
    end 
end 

그것을 다운로드하고 파일을 다운로드하고 이메일 보내기가 함께 일어나고 있습니다.

어떻게 파일 다운로드 기능을 피할 수 있습니까? 어떻게 .pdf 파일을 생성하여 이메일 ID로만 보내야합니까?

답변

1

이 당신을 도울 것입니다, 다운로드

pdf = render_to_string pdf: "#{@user.id}_file", encoding: "UTF-8" 

# then save to a file 
save_path = Rails.root.join('public/download_pdfs','#{@user.id}_file.pdf') 
File.open(save_path, 'wb') do |file| 
    file << pdf 
end 

희망없이 다음과 같이 파일을 저장할 수 있습니다.

+0

"template : tmplates/pdf"문을 제거하십시오. –

+0

제발, 내 편집을 참조 –

+0

고맙습니다. 그 지금 일하고있어. –

관련 문제