Question: Encoding error using wicked_pdf and rails ActiveJob

Question

Encoding error using wicked_pdf and rails ActiveJob

Answers 1
Added at 2016-12-15 22:12
Tags
Question

I am using wicked-pdf to simplify generating pdf invoices in my Rails 5 application. I have two actions making use of the pdf functionality of wicked-pdf. The first action generates the pdf and renders it in a new browser window. The second method attaches the pdf to an email.

Both of these actions work just fine. The issue comes in when I set my pdf mailer action to 'deliver_later' using ActiveJob/Sidekiq. When I add 'deliver_later' I am presented with a error stating:

"\xFE" from ASCII-8BIT to UTF-8

This error does not happen if I use the "deliver_now" command. Using "deliver_now" send the email and attaches the PDF correctly.

Here is some of my code for the mailing action, mailer and job:

invoices_controller.rb

...
def create
  respond_to do |format|
    format.html do
      pdf = render_to_string( pdf: @order.ruling_invoice,
                          template: "orders/invoices/show.pdf.haml",
                          encoding: "utf8",
                          locals: { order: @order.decorate}
                        )
      SendInvoiceMailJob.new.perform( @order, pdf, @order.token )
      redirect_to order_url(id: @order.id, subdomain: current_company.slug),     notice: "This invoice has been emailed."
    end
  end
end
...

send_invoice_mail_job.rb

...
def perform( order, pdf, order_token )
  InvoiceMailer.send_invoice(order, pdf, order_token).deliver_later
end
...

invoice_mailer.rb

...
def send_invoice( order, pdf_invoice , invoice_token)
    @order = order
    attachments["#{invoice_token}.pdf"] = pdf_invoice
    mail(
    to: @order.email,
        from: @order.seller_email
    )
end
...

Why would this code work using "deliver_now" in send_invoice_mail_job.rb but it doesn't work using "deliver_later" ?

Answers to

Encoding error using wicked_pdf and rails ActiveJob

nr: #1 dodano: 2016-12-16 22:12

You can't just throw binary data (the PDF) into job arguments.

https://github.com/mperham/sidekiq/wiki/Best-Practices#1-make-your-job-parameters-small-and-simple

Source Show
◀ Wstecz