Timur Ramazanov
Timur Ramazanov

Reputation: 51

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:r>
              <w:rPr>
                <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" />
              </w:rPr>
              <w:drawing>
                <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" />
                  <wp:cNvGraphicFramePr>
                    <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="0" />
                  </wp:cNvGraphicFramePr>
                  <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">
                        <pic:nvPicPr>
                          <pic:cNvPicPr>
                            <a:picLocks noChangeAspect="0" noChangeArrowheads="0" />
                          </pic:cNvPicPr>
                        </pic:nvPicPr>
                        <pic:blipFill>
                          <a:blip r:embed="<%= img_data[:id] %>" cstate="print">
                            <a:extLst>
                              <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                                <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
                              </a:ext>
                            </a:extLst>
                          </a:blip>
                        </pic:blipFill>
                        <pic:spPr bwMode="auto">
                          <a:xfrm>
                            <a:off x="0" y="0" />
                            <a:ext cx="<%= img_data[:width] * 7400 %>" cy="<%= img_data[:width] * 7400 %>" />
                          </a:xfrm>
                          <a:prstGeom prst="rect">
                            <a:avLst />
                          </a:prstGeom>
                          <a:noFill />
                          <a:ln>
                            <a:noFill />
                          </a:ln>
                        </pic:spPr>
                      </pic:pic>
                    </a:graphicData>
                  </a:graphic>
                </wp:inline>
              </w:drawing>
            </w:r>

          <% 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?

UPDATE 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

Upvotes: 0

Views: 583

Answers (3)

Timur Ramazanov
Timur Ramazanov

Reputation: 51

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.

Upvotes: 0

Jan Vanderschoot
Jan Vanderschoot

Reputation: 1

The fact that MS Office Word replaced the value of r:embed suggests that there was no definition of @Id="image_1" in the relationships file of this document. The relevant relationships file is probably word/_rels/document.xml.rels.

Upvotes: 0

xystum
xystum

Reputation: 1009

As far as I see, you are missing two element and some attributes.

(1) The wp:inline needs to have a wp:docPr element containing an id, name, and descr attribute.

<wp:docPr id="<% id %>" name="<% picture_name %>" descr="<% full_file_path_to_the_picture %>"/>

(2) The pic:nvPicPr element needs to have a pic:cNvPr element containing the same attributes.

<pic:cNvPr id="<% id %>" name="<% picture_name %>" descr="<% full_file_path_to_the_picture %>"/>

Of course you have to insert the missing variables (<% .. %>).

Here I marked the line where to insert the elements using comments:

<% [task[:design_front], task[:design_back]].compact.each do |img_data| %>
            <w:r>
              <w:rPr>
                <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" />
              </w:rPr>
              <w:drawing>
                <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" />
<!-- insert wp:docPr here -->
                  <wp:cNvGraphicFramePr>
                    <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="0" />
                  </wp:cNvGraphicFramePr>
                  <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">
                        <pic:nvPicPr>
                          <pic:cNvPicPr>
<!-- insert pic:cNvPr here -->
                            <a:picLocks noChangeAspect="0" noChangeArrowheads="0" />
                          </pic:cNvPicPr>
                        </pic:nvPicPr>
                        <pic:blipFill>
                          <a:blip r:embed="<%= img_data[:id] %>" cstate="print">
                            <a:extLst>
                              <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                                <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
                              </a:ext>
                            </a:extLst>
                          </a:blip>
                        </pic:blipFill>
                        <pic:spPr bwMode="auto">
                          <a:xfrm>
                            <a:off x="0" y="0" />
                            <a:ext cx="<%= img_data[:width] * 7400 %>" cy="<%= img_data[:width] * 7400 %>" />
                          </a:xfrm>
                          <a:prstGeom prst="rect">
                            <a:avLst />
                          </a:prstGeom>
                          <a:noFill />
                          <a:ln>
                            <a:noFill />
                          </a:ln>
                        </pic:spPr>
                      </pic:pic>
                    </a:graphicData>
                  </a:graphic>
                </wp:inline>
              </w:drawing>
            </w:r>

          <% end %>

If you look at your diff, you may see those elements added.

Upvotes: 1

Related Questions