How to charge Wicked PDF stylesheets into ActionMailer in Rails

44 views Asked by At

I am using the wicked PDF gem and to load the styles for my page it works like a charm, it loads the styles and the content

The problem is when I want to attach a pdf in Mailer, the wicked_pdf_stylesheet_link_tag "pdf" styles don't want to load, and you see the attached pdf without my pdf.scss

This is my layouts/pdf/base

doctype html
html lang="#{I18n.locale}"
  head
    meta http-equiv="Content-Type" content="text/html" charset="utf-8"
    meta name="viewport" content="width=device-width, initial-scale=1.0"
    = wicked_pdf_stylesheet_link_tag "pdf" # Here is the problem in ActionMailer, do not want to load
    = wicked_pdf_javascript_include_tag "app"
  body onload="number_pages"
    #content
      = yield

My Wicked_pdf.rb

WickedPdf.config = {
  header: {
    spacing: 5,
    html: {
      template: 'orders/pdf/_header_pdf',
      layout: 'layouts/pdf/base'
    }
  },
  footer: {
    spacing: 8,
    html: {
      template: 'orders/pdf/_footer_pdf',
      layout: 'layouts/pdf/base'
    }
  },
  margin: {
    top: 78,
    right: 0,
    left: 0,
    bottom: 24
  },
  layout: 'layouts/pdf/base'
}

if Rails.env.production?
  WickedPdf.config[:exe_path] = Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
  WickedPdf.config[:enable_local_file_access] = true # change to true if it doesn't work
end

And my OrderMailer.rb

 # ...rest of code
def prepare_order_details(order:, recipient_email:, recipient_name:)          
  
    html = render_pdf_template
    attachments["#{order.company.name} - Orden #{order.order_id}.pdf"] = html

    mail(to: recipient_email, subject: subject) do |format|
      format.mjml
      format.text
    end
  end 

def render_pdf_template
    render_to_string_with_wicked_pdf(
      template: 'orders/pdf/show_pdf',
      layout: 'pdf/base',
      formats: [:pdf],
      pdf: @order.number
    )
  end

It's how I attach my pdf in the Mailer, my links work perfectly, but when I attach them in Mailer it doesn't load the style, this wicked_pdf_stylesheet_link_tag "pdf" not working in my attached mailer

Any help is welcome and happy day to you.

pdd: I try with put "media: 'all'" but not working to me wicked_pdf_stylesheet_link_tag "pdf", media: 'all'

0

There are 0 answers