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 in Mailer #29

Open
tigershen23 opened this issue May 1, 2015 · 24 comments
Open
Labels

Comments

@tigershen23
Copy link

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

report_mailer.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_to_string 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
Copy link
Collaborator

@tigershen23 Try passing layout: false:

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

Let me know if that helps.

@tigershen23
Copy link
Author

@straydogstudio no dice :(, same error.

@straydogstudio
Copy link
Collaborator

@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
Copy link
Author

@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
Copy link
Collaborator

@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
Copy link
Author

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

@straydogstudio
Copy link
Collaborator

@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
Copy link
Collaborator

@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
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.

@ghost
Copy link

ghost commented Dec 21, 2016

I still have this issue

  • axlsx_rails: 0.5.0
  • axlsx: 2.1.0pre
    Download from controller action - ok, but rendering to string from sidekiq worker raises "utf-8 error"

@straydogstudio
Copy link
Collaborator

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
Copy link

ghost commented Dec 22, 2016

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
Copy link

ghost commented Dec 22, 2016

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
Copy link
Collaborator

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
Copy link
Collaborator

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

@nachokb
Copy link

nachokb commented Apr 14, 2017

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
Copy link
Collaborator

@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
Copy link

hcyildirim commented Sep 7, 2018

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
Copy link

hcyildirim commented Sep 11, 2018

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: <[email protected]>

UEsDBBQAAAAIAAAQIexP8WLf1wAAAEACAAALAAAAX3JlbHMvLnJlbHOtksFO
wzAMhl8l8n1NNySE0LJdJqTdECoPYBK3jdrEUWKgvD2BA2JoCA47Wvb//d/B
2/0SZvVCuXiOBtZNC4qiZefjYOCxu1vdwH63faAZpV6U0aeiaiQWA6NIutW6
2JECloYTxbrpOQeUOuZBJ7QTDqQ3bXut83cGnDJVh3kgMbDM+pXz9MQ8NRUG
qntL9J8q7ntv6cD2OVCUM40/LkAdnYF8dGvQv7g4tveZa9JypkvbBBJ0KPgJ
X6XaQ1k8lS+vzd9emNKltWgRio7cOaOrDyN98gq7d1BLAwQUAAAACAAAECHs
1SfqzcUBAAAI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sp/F50zvw7PDAy6BVyLuVJM/1oh3MrdSF17fX2psHs0dZhsU5
sbPK0KELN+MvnRQPu+V7Ajsq2KBVb2joX5h55m8QparfSY634w/ru7S14qo2
TjqffbkWiY5qBvuERNiO/4qwG60K+aKsD7qObSYnnS6277wTH3ch3rN66bM7
/cvNH1BLAQI0AxQAAAAIAAAQIexP8WLf1wAAAEACAAALAAAAAAAAAAEAAACk
gQAAAABfcmVscy8ucmVsc1BLAQI0AxQAAAAIAAAQIezVJ+rNxQEAAAgFAAAN
AAAAAAAAAAEAAACkgQABAAB4bC9zdHlsZXMueG1sUEsBAjQDFAAAAAgAABAh
7JvbRkAMAQAA5QEAABEAAAAAAAAAAQAAAKSB8AIAAGRvY1Byb3BzL2NvcmUu
eG1sUEsBAjQDFAAAAAgAABAh7MjB8ieLAAAA4QAAABAAAAAAAAAAAQAAAKSB
KwQAAGRvY1Byb3BzL2FwcC54bWxQSwECNAMUAAAACAAAECHsniikzbgAAACW
AQAAGgAAAAAAAAABAAAApIHkBAAAeGwvX3JlbHMvd29ya2Jvb2sueG1sLnJl
bHNQSwECNAMUAAAACAAAECHs9oueoSABAACEAwAAEwAAAAAAAAABAAAApIHU
BQAAW0NvbnRlbnRfVHlwZXNdLnhtbFBLAQI0AxQAAAAIAAAQIex7CkjVxgAA
ADIBAAAPAAAAAAAAAAEAAACkgSUHAAB4bC93b3JrYm9vay54bWxQSwECNAMU
AAAACAAAECHsPVhiPHAAAACKAAAAIwAAAAAAAAABAAAApIEYCAAAeGwvd29y
a3NoZWV0cy9fcmVscy9zaGVldDEueG1sLnJlbHNQSwECNAMUAAAACAAAECHs
F4ecKOsBAADJAwAAGAAAAAAAAAABAAAApIHJCAAAeGwvd29ya3NoZWV0cy9z
aGVldDEueG1sUEsFBgAAAAAJAAkATgIAAOoKAAAAAA==

And this is decoded version

PK�����!Ob@��_rels/.relsN0_%}M7$в]&�*�`�Qb=�bh��;Z�fBx�M�e`౻[~}�^ѧj$��Hպ���ź9�:A'��7m{w�2Uy 1<5��{K*{o9P3?.@�|tkп8krK��t(	_CY</^ҥh�9�#}
wPK�����!'���
xl/styles.xmlT] �+�q.tR��ڥWg�A�ib�m�sν^oR(��Ь!gB�wv��i�`n�NGVTU�D6nuj'��>m��aZU�e/�/�IaD*	P�>{dM�
3\������JNr=eE"@0-wPp�4C�δAkPF�h77�z:-PźNCM�BQ(!�JWp�]��ᶭ��M[�$:^�Pcy!X}$ӭ�4*z.?g2'��Fl!t
vSZ��1š
M�>jyE6f�S+�P�.�*IE�U���|}'$Tx I��X?��V��H�)�H��PK�����!F@���docProps/core.xmlmN0_ʽuiDmv���E nQb&I;/\l{$�A$4IVJۺ$o.�-/c[>j0f�$����rFl �~�8�'䷨��y�Q(���TrR���%��4hcQg�oN\���p���n9r�>^ǯ�(D�%(b�0�O
Tɠg/�&SJ+bwkFWGx��}��cu}+�PK�����!'�docProps/app.xml
0�E�doS�DJ.PK�{!yF��/r8½Z!eOŮiE�hy�(z."$Ǭ�;)] )��(Li�Nd��m��6�t�[_#˷�(5/PK�����!(͸��xl/_rels/workbook.xml.rels
0_e��v+�J���V�QP«�|OVK��yn0NR�d-�SI8�6�:���c�OKCpKD5�Ii}Bl}NVU4�e;SW�$o&
2N"�D>8Eh˲Ѵ�	��
	>+_p�:�'xPMo�^_�PK�����!ö��¡ ���[Content_Types].xmln0E�ZXJ:�Ea;C�c!Uc!zATRKM,qL�Es�Z��HhÙ�/X�^�mfò�­�js�zYs|��U�N"��<MÚ�Ô¦R�b T�|.`M�\<O�f2FkÌ�B�>@;Ø�w×�(Sz"�3�Î�CO�zd4�kt�:u
��&wh[�{G���]FBB�K\V!G�3�{+0�-࿯1�dg��+ݭ�4~b,��r[5?PK�����!{
H2��xl/workbook.xmln0_%�R�F��B0qi&
+o}]nq4�(հZ�`0C}X|®3`t;I
}Si=F'K0�G�y�s�(>lt!P;k��e*}�^Nl˸��:7
m<$�Q�/"/``Zojg
;?PK�����!=Xb<p#xl/worksheets/_rels/sheet1.xml.relsUK��!��Bz40F��'41�_zeO�Z,�
T;�7��y)b!�#g6.sy�iL�����G
p��PK�����!�(���xl/worksheets/sheet1.xmlSn0�C��� N�Rd�-�7ŦmhHtGI
(7sV'/`Bl.�0��x}Zz@�J8ܸL4D*M]@+;0Rm%ku�d�Z^V*#b�XU[,�"�$�5suLpC��f��mYw�cRtG�2ku:�	
�wa'2%�G���8)�w
�O"�#tI+�k{B&xZFP��,"z?{
aye&U[EJ<ȱ��ۼ[̹��Br^b�Hѳ���Z�ti�Ytc>L,y;ˠUȻ$�̭�׷��GY9С�7/��{�;*ؠUoh_yo�I�뻴6N:}�j�D��
�m&'.��w!޳z;�PK��4����!Ob@���_rels/.relsPK��4����!'���
��xl/styles.xmlPK��4����!F@�����docProps/core.xmlPK��4����!'��+�docProps/app.xmlPK��4����!(͸����xl/_rels/workbook.xml.relsPK��4����!ö��¡ �����[Content_Types].xmlPK��4����!{
H2���%�xl/workbook.xmlPK��4����!=Xb<p#���xl/worksheets/_rels/sheet1.xml.relsPK��4����!�(�����xl/worksheets/sheet1.xmlPK��		N�

Isn't that awkward?

@josephecombs
Copy link

josephecombs commented Jun 29, 2020

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: ['[email protected]'],
}
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: '[email protected]', to: options[:email_array], subject: "Email Subject"
end

@ksdputra
Copy link

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
Copy link
Collaborator

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

@dtakeshta
Copy link

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
Copy link

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

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

No branches or pull requests

7 participants