Timur Ramazanov Timur Ramazanov - 1 year ago 114
Ruby Question

Generated docx file is corrupted

I have a legacy Rails app, that can generate docx file. It's using just xml template, not any gem. Template is written using ERB syntax.

The problem is that generated file is marked as "corrupted" by MS Office Word, though LibreOffice on Linux opens it flawlessly. However, after recovering MS Office Word seems to open file without any content losses too.

I paste full XML template on pastebin.

While debugging I found out, that without the block, starting on the line 602, everything works fine. So I can't get, what's wrong with that particular piece of XML. I'll paste it right here for convenience

<% [task[:design_front], task[:design_back]].compact.each do |img_data| %>

<w:rFonts w:ascii="Arial" w:eastAsia="Times New Roman" w:hAnsi="Arial" w:cs="Arial" />
<w:noProof />
<w:sz w:val="18" />
<w:szCs w:val="18" />
<w:lang w:eastAsia="ru-RU" />
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="<%= img_data[:width] * 7400 %>" cy="<%= img_data[:height] * 7400 %>" />
<wp:effectExtent l="0" t="0" r="0" b="0" />
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="0" />
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<a:picLocks noChangeAspect="0" noChangeArrowheads="0" />
<a:blip r:embed="<%= img_data[:id] %>" cstate="print">
<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
<pic:spPr bwMode="auto">
<a:off x="0" y="0" />
<a:ext cx="<%= img_data[:width] * 7400 %>" cy="<%= img_data[:width] * 7400 %>" />
<a:prstGeom prst="rect">
<a:avLst />
<a:noFill />
<a:noFill />

<% end %>

I tried to compare recovered file with my file, but I didn't see any crucial differences. I don't have that diff right now, but I can reproduce it if necessary.

Can someone show me the way? :) What am I doing wrong?

I tried to make corrections, suggested by Martin P., but no luck. Here is a diff between my generated file and recovered version (recovered on the right)

Diff screenshot

Answer Source

After many hours of debugging the answer is found.

The last part of this puzzle was the file [Content_Types].xml. It contained the line <Default Extension="jpeg" ContentType="image/jpeg" />, but my images have .jpg extension. I changed Extension attribute to jpg and error was gone.

However, the additions suggested by Martin P. are necessary too (how can I credit him by the way?), because without them the resulting file remains corrupted, but with an another error message.

Thanks to everybody, who tried to help me. I hope, this answer will help someone in the future.