ImageInputStream issue when using <fo:external-graphic ... />
Michael Hansen <[email protected]>
| Newsgroups | gmane.text.xml.fop.user |
|---|---|
| Message-ID | <[email protected]> |
Hello community,
using FOP 2.1 I occasionally get the following exception when trying to transform xml to pdf using images:
java.lang.IllegalStateException: ImageInputStream is not reset for: another_image.png
at org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.returnSource(AbstractImageSessionContext.java:230)
at org.apache.xmlgraphics.image.loader.cache.ImageCache.needImageInfo(ImageCache.java:129)
at org.apache.xmlgraphics.image.loader.ImageManager.getImageInfo(ImageManager.java:139)
at org.apache.fop.fo.flow.ExternalGraphic.bind(ExternalGraphic.java:81)
I am searching for a solution for a little while now, without success.
My (simplified) xsl file looks like:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format"
exclude-result-prefixes="fo">
<xsl:template name="whatever">
<xsl:variable name="imageTest" as="xs:string">
<xsl:value-of select="/report/data/whatever/image"/>
</xsl:variable>
<fo:block>
<fo:external-graphic src="url('data:image/jpeg;base64,{$imageTest}')" content-width="scale-to-fit"
content-height="scale-to-fit" scaling="uniform" inline-progression-dimension.maximum="160mm"
block-progression-dimension.maximum="40mm"/>
</fo:block>
<fo:block>
<fo:external-graphic src="another_image.png" width="13.5cm" content-width="scale-to-fit" content-height="scale-to-fit" scaling="uniform"/>
</fo:block>
</xsl:template>
</xsl:stylesheet>
and the responsible java part for transformation looks like:
final TransformerFactory tf = TransformerFactory.newInstance();
final DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final DOMSource domSource = new DOMSource(db.parse(xmlFile.toFile()));
final InputStream inputStream = resourceLoader.getResource("classpath: path/to/file.xsl").getInputStream();
final StreamSource xslt = new StreamSource(inputStream);
final ClasspathUriResolver resolver = new ClasspathUriResolver(resourceLoader);
tf.setURIResolver(resolver);
final Transformer transformer = tf.newTransformer(xslt);
final Resource resource = resourceLoader.getResource("classpath:fop.xconf");
final File configFile = resource.getFile();
final FopConfParser parser = new FopConfParser(configFile);
final FopFactoryBuilder builder = parser.getFopFactoryBuilder();
final FopFactory factory = builder.build();
try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
final Fop fop = factory.newFop(MimeConstants.MIME_PDF, outStream);
final Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(domSource, res);
} catch(...) {
}
We are getting a java.nio.file.Path-object "xmlFile", manipulating the DOMSource befor transforming it into a PDF file. The fop.xconf-file just contains fonts-configurations.
So, a little more complex than the examples on xmlgraphics.apache.org, but this shouldn't result in an image issue...
The problems start when we started to add the second image using <fo:external-graphic ... />
I have no clue what I am doing wrong. Any recommendation|help is welcome.
// Michael