当render_to_string正在准备页面wicked_pdf时,样式表不会加载

问题描述 投票:0回答:1

我正在使用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。

ruby-on-rails wicked-pdf
1个回答
0
投票

尝试设置格式(假设你有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
© www.soinside.com 2019 - 2024. All rights reserved.