我正在使用wicked_pdf生成pdf报告。从页面下载非常有用。
现在我尝试将pdf作为邮件附件发送而不保存文件。
reports_controller.rb
def send_personal_report
pdf_html = render_to_string(template:'personal.pdf', layout: 'pdf.html')
pdf = WickedPdf.new.pdf_from_string(pdf_html)
UserMailer.report_email(pdf).deliver_now
end
布局/ pdf.html.erb
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<%= wicked_pdf_stylesheet_link_tag "custom" %>
<title>Отчет</title>
<meta name="description" content="">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<body>
<%= yield %>
</body>
样式表/ custom.scss
@import "bootstrap-sprockets";
@import "bootstrap";
user_mailer.rb
def report_email(pdf)
attachments['report.pdf'] = {mime_type: 'application/pdf', content: pdf}
mail(to: '[email protected]', subject: 'Report')
end
但毕竟我在电子邮件中的附件只是一个原始的HTML。
尝试设置格式(假设你有html.erb和text.erb模板):
def report_email(pdf)
#attachments['report.pdf'] = {mime_type: 'application/pdf', content: pdf}
mail(to: '[email protected]', subject: 'Report') do |f|
f.text
f.html
f.pdf do
attachments['report.pdf'] = {mime_type: 'application/pdf', content: pdf}
end
end
end