caxlsx / caxlsx_rails

A Rails plugin to provide templates for the axlsx gem
MIT License
743 stars 84 forks source link

invalid byte sequence in UTF-8 when generating spreadsheet in Mailer #29

Open tigershen23 opened 9 years ago

tigershen23 commented 9 years ago

Hey there, I'm trying to attach an xlsx spreadsheet to a Sidekiq delayed email (only difference is that it's in the background), and I'm getting a WARN: invalid byte sequence in UTF-8 that blows up the sending job. I've tried all manner of different encoding options for the output from render_to_string, but no dice. This happens even when using very basic . The spreadsheet renders fine from the console and from a controller action. The email sends fine with no attachments. The following code is exactly what I'm running, with some names switched out. Gem version 0.3.0, Rails 4.1.7, Ruby 2.2.0

_reportmailer.rb

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

report.xlsx.axlsx

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

_Output from a pry session right after the render_tostring call

[6] pry(#)> xlsx
03:24:34 worker.1 | => "PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!�c8k��\u0000\u0000\u0000A\u0002\u0000\u0000\v\u0000\u0000\u0000_rels/.rels���N�0\f��<E���nH\b�e� ��\u0010*\u000F`\u0012�����\u0013��=�\u0003bh\b\u000E;Z�������\u0012f�B�=G\u0003��\u0005E���q0�����a��>���^����j$f\u0003c)�F�lG\n�\eN\u0014��g\tX�(�Nh'\u001CHo��J�w\u0006\u001C3U�2P1����ezb��\n\u0003��%�O\u0015���t��9P,'\u001A\u007F\\�:8\u0003rpk���8���5iY��6�\n:,�\t_��CR<�/���^����h)\u0014\u001D�SF�\u001FF��\u0015v\u0017�PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!������\u0001\u0000\u0000\t\u0005\u0000\u0000\r\u0000\u0000\u0000xl/styles.xml�T�j�0\u0010}�W\b}@em�@\u0017�P\u0002�B\u0013\n�C_�kym��Hr���\u0019I�-CC��\u000F���s��Fc���R<uBx4)�\u001D�����\u0010w�����\u0019��\u007FZc\u0015���\u0017�\u0006+x��HIr(�;�x�qU�Q��;t6��\f\u001FV\b��g�0-\n����i\u0004�3<D)�4\u0018�\u007F\n�{\u0002�uG���E%Y�Wek�V\u0005�\t\u0000k�\u0004z���\u001F��\u0012���&���k���� \u0001 I\u0018���K�;]\u0000�r��\v�k��%~�\a�T\e-�E2B0��\a���3=����\vTt2�\u0011vWS�\u0010pR�!R�\u001E�+g�w���F\u001A����p]\u0017�I�\f������Z\u0012�U��p�\u0002��<���V�����\u0000�q\u0016R>\u0005�?����\u0016%N\u0018'��f+��^C��\u0012����On�\u0012d�7����}��\u0005�\u007F�]\u0015���\u001E|\u0018��8���u|�b\u0015y\a���V�z��(�]\f?\u0006\a�����`�L[\u0003�0�\u0001���'�\u0010%�F�|��y�\u0018��\a�������~�/�/�-�\u0015F���-S8P�\u0004v�����\u0006PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!���\u0001\t\r\u0001\u0000\u0000�\u0001\u0000\u0000\u0011\u0000\u0000\u0000docProps/core.xmlm��N� \u0010��>E��\u0005Z5\eB�\a��41�F����K,�\u0000n��K����l�e���\u00182|;�.9���7%�\u0019A\t\u0018�7��%z�w�\u0006m+.-���\u0017�[pA�Ob��L�\u0012�C�\fc/����\"ab��;-B,]�����\u0005�\u0013r�5\u0004�� �$L�bD'e#\u0017��q�,h$�\u000E4��1�(�g�\n�\u0016�v��\u0015\u001D�i\u007F\u0015���\u001C�Z�a\u0018����8?�\u001F�O��WSe|\u0010F\u0002�x#�t B�*1v~�xu�O\u000F\u001F/�I��\u001D�>'���c�CUN�]J��5)\u0018����sr]���ppP��*������]U7\u007FPK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!���O��\u0000\u0000\u0000�\u0000\u0000\u0000\u0010\u0000\u0000\u0000docProps/app.xml���\n�0\u0014Ew��do\u0013\u001DDJ�.�������\r��B�,���\b�;^��p�n��b��\u001Ca+��\u0012\u0005�!�pn�u8�'���\u0012)@d\a��\u007FL�X�C-e2\v�1U\u0019c&\u0013E?r�q�4M�@O��\u0001Y\u001E�:J�\u0018��-�W(>�z�\u007F����/��g�>��_���\u0000PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!�u�\u0000��\u0000\u0000\u0000\n\u0001\u0000\u0000\u001A\u0000\u0000\u0000xl/_rels/workbook.xml.rels���\n�0\u0010��>E���*\"\"M{\u0011�W�\u000F\u0010�mZl~�F�oo���\a���|�T����A�'�$l�\u0012\u00049���\u0019\t���>@SW\u0017�U�\t\u001E��\"W\u001CK\u0018S\nGD�#Y��\u000F��3�hU�2\u001A\fJ��!���\u001E�'\u0003���S�P��i����\u0001�-��Y��0i:y}����1|SA�����;����O��\u0005PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!�=�9�\u0015\u0001\u0000\u0000�\u0002\u0000\u0000\u0013\u0000\u0000\u0000[Content_Types].xml��=O�0\u0010�w~E�\u0015�\u000E\f\b�$\u001D�\u0018�C�\u0001��$V�%�[�\u007F���K��������9[�W���\u000E\u0012��\ev�+V�WA\e�7�s�V>�U[o�\u0011��V�\r\er�OB�\u001A�I�!��J\u0017�����\u0017Q�Q� ���A��3�\\�����\u0005:���x^�gt�d��(�i\n���\u0004Z�\u0002y\u0002{���D��\u0006V�NDY��*2q��48�)�A����b-S~��RB\a�N!��<?���;t�Q@���\b�Y�A�������\u0012��*$�^~|�9}�q�\u0002��\u0002���\u0018\u0013H�\u0003@v�/�3�����\u0010��v�+w���_\u001C�s{�\u0003PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!�@*\u0014��\u0000\u0000\u0000)\u0001\u0000\u0000\u000F\u0000\u0000\u0000xl/workbook.xml�����0\fE��\u0015���\u00161#�j�\u0006!�C��\u0003B���&��\u0000��\u0013^b������z�\u0017\u0006uF\u0016O�@9-@al��x0��]O\u0016�l�\v�qOtTy:��>���Z�\u001E��)�\u0018s�#\u000E6��\u000FZFF��GLa�������\bwB��0��|�+jO\u0001c�C\u0018\a��W��(�r�e�l����\e�� \b��o��\u0011U�\u0001\r�\\u\t�V���4(�|\u0016�q��-�\\�Oz��\u000FPK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!��(�js\u0000\u0000\u0000�\u0000\u0000\u0000#\u0000\u0000\u0000xl/worksheets/_rels/sheet1.xml.relsU�K\u000E\u0002!\u0010\u0005�����i��1\u0006��\u001C��\a �\u0002q��&���R��W���I�xS�X���$APv�\u0011��p���\u0005\u0016�n��>\f\u000E��\u0018��\u001AB����.P�<�Jy,����\u0003��j��z�Y�3��\u0006\u0018�\u007FQs�\u0002PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!��v\u0015��\u0001\u0000\u0000\b\u0004\u0000\u0000\u0018\u0000\u0000\u0000xl/worksheets/sheet1.xml�SMo�0\f��W\u0018�/v\at\v�8=��: [��\u001F�n�M�Be���z��\u001F%;�\u0003l�n�����V��\e\u001D��u\nM*�\u0016����X(S���a�q)n��\u001E���\u0001(�t�RQ\u0013�7q��\u001A\u001A�\u0016���H������b�Z�E(jt�)I>��TF\f\u001Dn�����T9�a�5`hhbAK�e]�Z\u0017���V��\n\u001E����X�������VV�\u0001=����\u001E��@*J�\u001D�(^��1w,zR���\u001D��\u0014�\u001F,\u0012�\u0014H\u001AkI\u001E3�\fB1a����V\u0004���\f�w�����9�v\u0017�-?���\fZU���\u000EJ�H�\u0005\u00169�l7\u0002?;\r�\u0002�~����gI���V\u0015;�?�\u0005����q����;b��R��W\t_���������/Mf)�gi'��\v\\]\u0006=�����\"���\u0014�Is�$�6�\u0003�w�\u000Ex�gUP����\u0002~�=x�x�e�4��I��~�\u001D����?�Nko��\u000Eeqz;��V\u0019������ ��n����t���w4��\u0010s���,fuf�|u�,�K[)�b�����k\u0011���`\u001F�\b��W\u0012��U\"���I������{�Z�\f�m��D�}v�_���\aPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!�c8k��\u0000\u0000\u0000A\u0002\u0000\u0000\v\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000��\u0000\u0000\u0000\u0000_rels/.relsPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!������\u0001\u0000\u0000\t\u0005\u0000\u0000\r\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000��\u0002\u0001\u0000\u0000xl/styles.xmlPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!���\u0001\t\r\u0001\u0000\u0000�\u0001\u0000\u0000\u0011\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000���\u0002\u0000\u0000docProps/core.xmlPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!���O��\u0000\u0000\u0000�\u0000\u0000\u0000\u0010\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000��1\u0004\u0000\u0000docProps/app.xmlPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!�u�\u0000��\u0000\u0000\u0000\n\u0001\u0000\u0000\u001A\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000���\u0004\u0000\u0000xl/_rels/workbook.xml.relsPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!�=�9�\u0015\u0001\u0000\u0000�\u0002\u0000\u0000\u0013\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000���\u0005\u0000\u0000[Content_Types].xmlPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!�@*\u0014��\u0000\u0000\u0000)\u0001\u0000\u0000\u000F\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000��\u000E\a\u0000\u0000xl/workbook.xmlPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!��(�js\u0000\u0000\u0000�\u0000\u0000\u0000#\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000���\a\u0000\u0000xl/worksheets/_rels/sheet1.xml.relsPK\u0001\u00024\u0003\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!��v\u0015��\u0001\u0000\u0000\b\u0004\u0000\u0000\u0018\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000���\b\u0000\u0000xl/worksheets/sheet1.xmlPK\u0005\u0006\u0000\u0000\u0000\u0000\t\u0000\t\u0000N\u0002\u0000\u0000�\n\u0000\u0000\u0000\u0000"

Thanks in advance for your help.

straydogstudio commented 9 years ago

@tigershen23 Try passing layout: false:

xlsx = (render_to_string handlers: [:axlsx], formats: [:xlsx], template: "report", layout: false)

Let me know if that helps.

tigershen23 commented 9 years ago

@straydogstudio no dice :(, same error.

straydogstudio commented 9 years ago

@tigershen23 A few people have had this error, and I've never been able to duplicate it. Is it possible for you to create a simple Rails app in a Github repo that duplicates the error? Perhaps a stripped down version of your application?

tigershen23 commented 9 years ago

@straydogstudio Yeah, I'll see what I can do. Do you know of a way to force encoding on the render itself? I've found some general solutions to error in Ruby/Rails (not for axlsx_rails specifically), but it seems like the render_to_string return (I pasted an example above) is already a lost cause. Sorry, don't know much about byte encoding or else I would try to dig in more.

straydogstudio commented 9 years ago

@tigershen23 These can be hard errors to track down. Sorta vague. When I have time I will investigate forcing encoding in render to string.

There was another issue that solved this problem with their smtp_settings and ActionMailer. I've no idea what you have, but it might be worth taking a look just in case.

tigershen23 commented 9 years ago

@straydogstudio yeah I saw that, I'm afraid of messing up other mailers by fiddling with those but I could try. Thanks!

straydogstudio commented 9 years ago

@tigershen23 By the way, another option instead of a github repo is to send me your Gemfile and describe your setup (including platform, versions, etc.)

straydogstudio commented 9 years ago

@tigershen23 A recent issue showed a possible solution. Make sure your template for the attachment does not have the same name as the mailer template. If you try it let me know if it works.

straydogstudio commented 8 years ago

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.

ghost commented 7 years ago

I still have this issue

straydogstudio commented 7 years ago

First I'd double check that your sidekiq job isn't receiving parameters that are messed up when it tries to run. E.g. binary data, or something like it. Passing parameters to sidekiq isn't always straight forward.

Second, if you can create a stripped down repo with the error, that would be very helpful. I've never been able to reproduce it and have something to debug. I realize that takes time.

ghost commented 7 years ago

Actually sidekiq just receives ID and then run mailer, and the mailer service do a report render csv or xlsx. CSV is ok, xlxs not.

ghost commented 7 years ago

Found what caused problem! Problem is not with axlsx render_to_string indeed. In mailer:

file = render_to_string .... and rest of xlsx params
file = Base64.encode64(file)  # this line is important
....
# attaching file to mail (encoding param is important)
attachments.inline['File.xlsx'] = { :mime_type => Mime:XLSX, :content => file, :encoding => 'base64' }
mail() ....
straydogstudio commented 7 years ago

Excellent. I'm glad to hear it. I will update the README and any examples to make sure it is clear. We will find out if that helps.

straydogstudio commented 7 years ago

I'm reopening this issue until I can test the encoding.

nachokb commented 7 years ago

I've got a similar situation. I'm using axlsx-rails with render_anywhere (it's a gem which provided the ability to use actionpack's templating functionality from wherever you need it -- I've for a "Form" model which can have different types, each with a different template).

I've triggered this bug a few times, particularly when rendering partials from within partials (though only in a small percentage of cases so I don't really know what's happening and it's very hard to debug).

2 funny things:

  1. I tried to reproduce it in a simple test case but couldn't (please check [1], I'll update it now with my latest try);
  2. my workaround is… interesting. I've got something similar to this:
render template: '...', handlers: [...], formats: [...], locals: { ... }

and that fails, but this one doesn't:

[1].each do
  render template: '...', handlers: [...], formats: [...], locals: { ... }
end

and I've got no idea WHY.

I also checked the data I'm rendering, and everything is valid UTF-8 (in fact, I tried it with different data). So I'm fairly certain that's not the problem.

[1] https://gist.github.com/nachokb/f3cc995cf44acbfd0e63ee25515f4cb8

straydogstudio commented 7 years ago

@nachokb Now that is an interesting solution. I've guessed it is a scope issue, though I've never been able to replicate the problem and test it. I agree, whatever this is, it is hard to debug. Thanks for sharing what you've found.

hcyildirim commented 6 years ago

Hi,

I am getting same error.

ruby '2.5.0'
'rails', '~> 5.2.0'
'rubyzip', '~> 1.1.0'
'axlsx', '2.1.0.pre'
'axlsx_rails'
hcyildirim commented 6 years ago

Hi,

I reset the database, the excel file to be created is empty and I still get an error. Here is my base64 attachment.

Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Content-Transfer-Encoding: base64
Content-Disposition: inline;
 filename=ExcelFile.xlsx
Content-ID: <5b97b917b3a08_94653fcfed48b794707fa@mac-name.local.mail>

UEsDBBQAAAAIAAAQIexP8WLf1wAAAEACAAALAAAAX3JlbHMvLnJlbHOtksFO
wzAMhl8l8n1NNySE0LJdJqTdECoPYBK3jdrEUWKgvD2BA2JoCA47Wvb//d/B
2/0SZvVCuXiOBtZNC4qiZefjYOCxu1vdwH63faAZpV6U0aeiaiQWA6NIutW6
2JECloYTxbrpOQeUOuZBJ7QTDqQ3bXut83cGnDJVh3kgMbDM+pXz9MQ8NRUG
qntL9J8q7ntv6cD2OVCUM40/LkAdnYF8dGvQv7g4tveZa9JypkvbBBJ0KPgJ
X6XaQ1k8lS+vzd9emNKltWgRio7cOaOrDyN98gq7d1BLAwQUAAAACAAAECHs
1SfqzcUBAAAIBQAADQAAAHhsL3N0eWxlcy54bWylVF2L3CAU/SviD6hx2i50
iIFSCBTapbD70FdnopOAH0HNkvTX96ppYqDLDm0exuuZc869Xm9S+7Ao8dQL
EdCslfEM9yGMZ0L8tRea+3d2FAb+kdZpHmDrbsSPTvDOR5FW5FRVD0TzweCm
NpNudfDoaicTGD5tEMrL145hWlUYZbsvthMML/AQrUnXYUT+KqCvCVDfn7U+
e5+UZE3f1NKavQqKMwDWXAv0whXDn93AFQaN/5U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[Content_Types].xmlPK4!{
H2%xl/workbook.xmlPK4!=Xb<p#xl/worksheets/_rels/sheet1.xml.relsPK4!(xl/worksheets/sheet1.xmlPK       N

Isn't that awkward?

josephecombs commented 4 years ago

I'm on Rails 5.2, and got the error in this issue - I got this working by calling the new #render method and omitting the base64 option in the mailer itself:

str = MyXLSXController.render(:test_xlsx, locals: { some_local: @some_local }, layout: false)
options = {
  xlsx: str,
  email_array: ['asdf@gmail.com'],
}
JoeMailer.joe_mailer_instance_method(options).deliver_now

in JoeMailer:

def joe_mailer_instance_method(options)
    xlsx = options[:xlsx]
    attachments["test_filename.xlsx"] = { mime_type: Mime[:xlsx], content: xlsx }

    mail from: 'qwer@gmail.com', to: options[:email_array], subject: "Email Subject"
end
ksdputra commented 3 years ago

I dont know if this is helping for others.

I had problems sending xlsx attachment. I dont know the exact problem, but the file always corrupt (can't be opened). I use sendgrid.

After 2 hours debugging, I found my root cause is the base64 encoding itself, ruby adds \n every 60 chars. (see in this stackoverflow question for more information.)

So I use Base64.strict_encode(xlsx) instead of Base64.encoding(xlsx), and the problem solved.

straydogstudio commented 3 years ago

@ksdputra Excellent work! I'll change the README (unless you want to do a PR for that.) That would definitely cause problems.

dtakeshta commented 3 years ago

I'm getting the "Excel cannot open the file ..." error.

Rails 6.1 caxlsx (3.1.0) caxlsx_rails (0.6.2)

I was hoping the strict_encode64 was going to solve this, but it didn't. What I find strange is the file opens perfectly fine when I export using a link_to, but I'm unable to open the file when attaching to a mailer.

xlsx =
      render_to_string layout: false,
                       formats: [:xlsx],
                       template: 'frontend/v1/users/index',
                       locals: {
                         users: users,
                         relocations: relocations
                       }

    attachment = Base64.strict_encode64(xlsx)

    attachments['user_export.xlsx'] = {
      mime_type: Mime[:xlsx],
      content: attachment,
      encoding: 'base64'
    }
josephecombs commented 3 years ago

@dtakeshta try omitting the base64 argument or generally following my workaround detailed in my comment above from about a year ago.