Forum


Replies: 4   Views: 4633
"replacetemplatevariablebyhtml" and "downloadimages"
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by HalloWelt  · 22-03-2013 - 09:29

Hi,
I've got a little issue with the "replaceTemplateVariableByHTML" method. When I try to embed images using the "downloadImages" option Word reports "unreadable content" and that the file is corrupted. Word _can_ actually open the files and the content is alright, but the error message makes the user feel uncomfortable.
First I tried the example "examples/intermediate/HTML.php" which uses 'addHTML' method and works fine but does not embed (download) the images.
After that I tried the following code:[code]
$oPHPDocX = new CreateDocx();
$oPHPDocX->addTemplate( $sUploadPath.'/template/Template.docx' );
$oPHPDocX->replaceTemplateVariableByHTML(
'CONTENT',
'block',
'<p>Some Text</p>
<img src="file:///development/PHPDOCX/res/city.jpg" />
<p>Some Text</p>',
array(
'isFile' => false,
'parseDivsAsPs' => true,
'downloadImages' => true,
'wordStyles' =>array(
'<p>' => 'Standard'
)
)
);
[/code]
This code causes the error described above. If I remove the "'downloadImages' => true," line the error disappears. But that's not what I need. I have to include the images in the DOCX file.

I'd be very grateful for some hints. Btw.: I'm using PHPDOCX v3.2.

best regards,
Robert Vogel

[edit]: I've already tried the tipps from this post: [url]http://www.phpdocx.com/help/forum?vasthtmlaction=viewtopic&t=2820.0[/url]

Posted by HalloWelt  · 11-04-2013 - 12:13

Hmmm... it seems to be connected with the DOCX template I use and the file type of the image that should be included.
When I use my own template GIFs are no problem, but as soon as a JPG is included I get the "unreadable content" error in Word.
When I use the "examples/files/testHTML2mdc.docx" template (which also is used in "examples/easy/ReplaceTemplateVariableByHTML.php") I can embed all image file types.

This is strange... Does anybody have an idea? May it be because I'm using Word 2010 to create my template?

Posted by HalloWelt  · 11-04-2013 - 12:13

I think I found the problem: My template has already an image embedded. Therefore the "[Content_Types].xml" already had the entry[code]
<Default Extension="jpg" ContentType="image/jpeg"/>
[/code]
Now that my HTML contained other JPEGs, PHPDOCX adds the line[code]
<Default Extension="jpg" ContentType="image/jpg"/>
[/code] to "[Content_Types].xml". Now there are two entries for the file extension "jpg", one mapping to MIME type "image/jpeg" and one mapping to MIME type "image/jpg". Btw. "image/jpeg" seems to be the official one.

The cause of the issue is in "CreateTemplate::addContentTypes()" in "classes/conf/CreateTemplate.inc:390":[code]
//let´s check that tha conten type does not exist yet
if(strpos(self::$_contentTypes, $args[0]) === false){
$domContentTypes = new DomDocument();
[...]
self::$_contentTypes = $domContentTypes->saveXML();
}[/code]

'strpos()' doesn't find a "default extension" entry with ContentType "image/jpg" and therefore it gets added.
Going back in the call stack I found that in the same file in line 1691 ("CreateTemplate::TemplateHTMLRels()")[code]
$this->addContentTypes('<Default Extension="'.$extension.'" ContentType="image/'.$extension.'"/>');
[/code]
gets called, with the assumption that the proper MIME type / ContentType for an image with the file extension "X" ist "image/X". Maybe this need to be fixed.

I'd like to file a bug on this topic. Is there a bugtracker?

Posted by admin  · 11-04-2013 - 12:13

Hello,

Thanks for your report I have added it to our bugtracker to check and fix it.

Regards.