Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid byte sequence in UTF-8 when generating spreadsheet #37

Closed
samuelshih opened this issue Jul 15, 2015 · 10 comments
Closed

Invalid byte sequence in UTF-8 when generating spreadsheet #37

samuelshih opened this issue Jul 15, 2015 · 10 comments

Comments

@samuelshih
Copy link

@straydogstudio
In my rails app, I'm trying to attach a generated xlsx file (basic) in a Sidekiq delayed mail. I'm getting the error: WARN: invalid byte sequence in UTF-8 every time I run the sending jobs.

mailer.rb

xlsx = (render_to_string handlers: [:axlsx], formats: [:xlsx],
    template: "report", layout: false)
attachments["#{subject}.xlsx"] = { mime_type: Mime::XLSX, content: xlsx }

report.xlsx.axlsx

wb.add_worksheet(:name => "Custom Report") do |sheet|
   sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border
   sheet.add_row [1, 2, 3], :style => blue_border
end

Gem version 0.3.0, Rails 4.1.7, Ruby 2.2.0

Gemfile

...
gem 'sidekiq'
gem 'sidetiq'
gem 'rack-timeout'
gem 'axlsx_rails'

If I comment out the xlsx block, email sends fine.
Thanks for your help in advance 😄

@samuelshih
Copy link
Author

@straydogstudio any updates?

@straydogstudio
Copy link
Collaborator

Sorry @samuelshih I haven't been able to get to it. One way you could help is to create a simple Rails repo in Github that demonstrates the issue. Then I won't have to set that up, and it will make sure I have the setup you do (with the entire Gemfile.) Would you be willing to do that?

@samuelshih
Copy link
Author

@straydogstudio I figured it out!!

xlsx = (render_to_string handlers: [:axlsx], formats: [:xlsx],
    template: "report", layout: false)
attachments["#{subject}.xlsx"] = { mime_type: Mime::XLSX, content: xlsx }
  mail(
      to: [email protected],
      from: "[email protected]",
      subject: subject,
      template_path: 'mailer',
      template_name: "report"
    )

The template in the xlsx block report can't have the same name as the html template. So I had to rename it to

xlsx = (render_to_string handlers: [:axlsx], formats: [:xlsx],
    template: "xls_report", layout: false)
attachments["#{subject}.xlsx"] = { mime_type: Mime::XLSX, content: xlsx }

It was always breaking on mail( ....), so I was thinking it might be getting the template names confused even when I wasn't attaching the xlsx file, so there it is 😄

@straydogstudio
Copy link
Collaborator

Fantastic! That's great. I think I may know what that means then, but I'll have to check on that. Nice to have a simple solution!

@straydogstudio
Copy link
Collaborator

I'm going to leave this open as a reminder to look into this.

@kikihakiem
Copy link

Hey @straydogstudio , I also faced the same problem. It looks like rails are trying to surround the xlsx file with layouts:
screen shot 2015-11-29 at 4 55 20 pm

To fix this problem I have to either rename the axlsx template or by passing render layout: false, so my controller action looks like this:

respond_to do |format|
  format.html
  format.xlsx { render layout: false } # avoid surrounding xml with layout
end

I prefer the later one.

@straydogstudio
Copy link
Collaborator

@kikihakiem Thanks for the update. The format.xls really shouldn't come into play, as I don't register anything under that MIME type. But that is interesting.

What Rails / Axlsx / axlsx_rails versions do you have?

@kikihakiem
Copy link

Rails 4.2.4, axlsx 2.0.1, axlsx_rails 0.4.0
I'm using stdlib for format.xls. However I got the same issue when generating xls file and the solution is also same, so I think it's rails issue.

@straydogstudio
Copy link
Collaborator

I have not been able to reproduce this bug. But, based on the experience of others (here and in other issues) the latest release of axlsx_rails (0.5.0) explicitly sets layout to false. Advising others to set the layout to false appears to fix these issues. I am cautiously optimistic. Please try 0.5.0 and comment on this issue (or open another) if the failure happens again.

@nachokb
Copy link

nachokb commented Apr 14, 2017

ref #29 (dupe)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants