1

It has been asked before, years ago, but with no answer corrupt docx when adding image

Hey

I have a template where i replace some variables and generate a docx document... That works fine..

As soon as i add an image, word tells me that the files is corrupt. But letting Word correct the error gives me the document as i expect it to be with the image.

Im adding image like this...

$docx->addBreak(array('type' => 'page'));
            $options = array(
            'src' => $value['110333870356bc784cf1b6b0.56722572.png'],
            'imageAlign' => 'center',
            'scaling' => 100,
            'spacingTop' => 10,
            'spacingBottom' => 0,
            'spacingLeft' => 0,
            'spacingRight' => 20,
            'textWrap' => 0,

            );
            $docx->addImage($options); 

Im lost here .. ? ..

Community
  • 1
  • 1
DTH
  • 1,133
  • 3
  • 16
  • 36
  • How are you serving your DOCX files when they are downloaded? Check your headers: Have a read here: http://stackoverflow.com/questions/12062337/serving-docx-files-through-my-webserver-are-corrupt-when-opened – Kinnectus Feb 16 '16 at 11:42
  • @BigChris: The docx is generated fine without image(s) .. The problem was adding an image was corrupting the file. Reason for this is explained below in my answer to the question .. – DTH Feb 16 '16 at 11:52

2 Answers2

1

After being in contact with 2mdc (Creator of phpDocX) they told me, that PHP is not able to get the right extension because of the filename of the image i am inserting. As a result phpDocX adds some wrong values (such as dpi and image extension name).

As mentioned in my question, Word is able to detect those errors and correct them.

Changed my routine which creates my "unique" image names, to make sure that dot's are not present and it fixed the problem ..

As a result, filename: 110333870356bc784cf1b6b0.56722572.png will instead become 110333870356bc784cf1b6b0656722572.png (or other generated filename).

DTH
  • 1,133
  • 3
  • 16
  • 36
1

Just to share my experience. I had exactly the same problem but not for the same reason. It was because the image file extension I wanted to insert (addImage) was in capital letter. Example :

  • image.JPG ==> result a "corrupt" file.
  • image.jpg ==> result OK !

I hope it will help someone...

user2126372
  • 31
  • 1
  • 6