Page 1 of 1

Using Image files in HTML <img>

PostPosted: Thu Feb 05, 2015 7:20 pm
by dirtymike
I'm trying to understand a problem with either Missing Image messages or Read-Error.

I'm trying to set an <img src="img.png" class="default-img"> inside an XHTML string.

When running the project I see:

Feb 05, 2015 2:13:58 AM org.docx4j.org.xhtmlrenderer.util.JDKXRLogger log
INFO: Matcher created with 134 selectors
Feb 05, 2015 2:13:58 AM org.docx4j.org.xhtmlrenderer.util.JDKXRLogger log
INFO: img.png is not a URL; may be relative. Testing using parent URL file:/path/to/./
Feb 05, 2015 2:13:58 AM org.docx4j.org.xhtmlrenderer.util.JDKXRLogger log
INFO: img.png is not a URL; may be relative. Testing using parent URL file:/path/to/./
Feb 05, 2015 2:13:58 AM org.docx4j.org.xhtmlrenderer.util.JDKXRLogger log
INFO: img.png is not a URL; may be relative. Testing using parent URL file:/path/to/./


I want to use some large images, do datauri isn't going to work here and the importer seems to dislike them anyway--rejecting them as valid URLs.

When I open the generated docx in LibreOffice, I see either [MISSING IMAGE] or Read-Error. Maybe I'm not understanding how it works. Does the image get read and copied into the docx archive for portability? Does the docx need to be in the same directory as the image to load it? I'm using XHTMLImporterImpl.convert( htmlstr, null ) and XHTMLImporterImpl.convert( htmlstr, '/path/to/img/dir/' ) and different variations on that.

Re: Using Image files in HTML <img>

PostPosted: Thu Feb 05, 2015 9:05 pm
by jason
Does the docx need to be in the same directory as the image to load it?


See line 60 of https://github.com/plutext/docx4j-Impor ... fault.java

Note that you can implement your own XHTMLImageHandler

Does the image get read and copied into the docx archive for portability?


Yes

Re: Using Image files in HTML <img>

PostPosted: Fri Feb 06, 2015 4:54 am
by dirtymike
So in order to be able to use any image file in an <img> tag using XHTML Import, I have to implement a custom XHTMLImageHandler?

/path/to/file/img.png

<img src="file://path/to/file/img.png"> and similar src's will not work "out of the box"?

Re: Using Image files in HTML <img>

PostPosted: Fri Feb 06, 2015 6:11 am
by dirtymike
Looked through the docx's xml files.

In footer.xml I find

<pic:pic><pic:nvPicPr><pic:cNvPr id="1" name=""/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip r:embed="rId5"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="5732145" cy="2023112"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic>


In _rels/document.xml.rels I find

<Relationship Target="media/document_image_rId5.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId5"/></Relationships>


And the file does exist in media/document_image_rId5.png and it is the image set inside <img src="file:///path/to/img.png" />.

Re: Using Image files in HTML <img>

PostPosted: Fri Feb 06, 2015 7:12 am
by jason
dirtymike wrote:So in order to be able to use any image file in an <img> tag using XHTML Import, I have to implement a custom XHTMLImageHandler?


No, you don't

dirtymike wrote:<img src="file://path/to/file/img.png"> and similar src's will not work "out of the box"?


Yes, should work out of the box.

Re: Using Image files in HTML <img>

PostPosted: Fri Feb 06, 2015 7:29 am
by dirtymike
Thanks, cool. My post above yours shows that the image relationship and pic XML elements are generated where expected. However, LibreOffice is showing a read error. As far as I can tell, I have all dependencies on my system and I'm just using the jar out of maven.

Re: Using Image files in HTML <img>

PostPosted: Fri Feb 06, 2015 11:35 am
by jason
dirtymike wrote:My post above yours shows that the image relationship and pic XML elements are generated where expected.


Right, I saw that. Just wanted to be absolutely clear for the benefit of anyone else stumbling upon this thread.

dirtymike wrote:However, LibreOffice is showing a read error.


Your image rel etc look OK. I suspect the problem might be something else. What version of LO? It'd be interesting if you could attach a docx exhibiting the issue.

Re: Using Image files in HTML <img>

PostPosted: Fri Feb 06, 2015 12:58 pm
by dirtymike
I realized I was adding the image to the footer part, I created a footer.xml.rels with the same xml from document.xml.rels and then the image appeared.