Fail hard on missing images

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Fail hard on missing images

Oskar Berggren
Hi,

Sometimes a document will reference an image that FOP is unable to find. With FOP 2.0, it will print a SEVERE log and then continue processing the file, eventually exiting with a regular zero exit code.

Because there may be a significant amount of other logs, and because this FOP was invoked from a makefile that renders a bunch of documents, it's easy to miss the log about the missing image. And because FOP appears to not use a non-zero exit code, the make command will also not complain.

How can I make FOP scream louder on missing images (and perhaps also other errors)? It could complete processing the file, but crucially I would like it to exit with a non-zero exit code so that make will stop with an error message.


/Oskar

Reply | Threaded
Open this post in threaded view
|

ImageInputStream issue when using <fo:external-graphic ... />

Michael Hansen
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
Reply | Threaded
Open this post in threaded view
|

Re: ImageInputStream issue when using <fo:external-graphic ... />

Chris Bowditch
Hi Michael,

Can you generate the XSL-FO File? i.e. the result of the XSLT transform?
If you then call FOP from the command line instead of via your embedded
program do you see the issue? Can you also elaborate what you mean by
"occasionally" is that 1 in 100, 1 in 100000?

Thanks,

Chris

On 23/05/2016 18:13, Michael Hansen wrote:

> 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



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Fail hard on missing images

Jeremias Maerki-2
In reply to this post by Oskar Berggren
See http://xmlgraphics.apache.org/fop/2.1/events.html for an example of
exactly that.

HTH
Jeremias Maerki


On 12.05.2016 23:12:22 Oskar Berggren wrote:

> Hi,
>
>
> Sometimes a document will reference an image that FOP is unable to find. With FOP 2.0, it will print a SEVERE log and then continue processing the file, eventually exiting with a regular zero exit code.
>
>
> Because there may be a significant amount of other logs, and because this FOP was invoked from a makefile that renders a bunch of documents, it's easy to miss the log about the missing image. And because FOP appears to not use a non-zero exit code, the make command will also not complain.
>
>
> How can I make FOP scream louder on missing images (and perhaps also other errors)? It could complete processing the file, but crucially I would like it to exit with a non-zero exit code so that make will stop with an error message.
>
>
>
> /Oskar
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Fail hard on missing images

Oskar Berggren
Are you saying I need to create my own fop runner to do such a thing? It can't be done using the regular fop script?

Seems like an unfortunate design choice. Or maybe I'm missing something... I always thought the fop script was the primary method of using fop, but perhaps I'm wrong.

/Oskar



2016-05-26 14:10 GMT+01:00 Jeremias Maerki <[hidden email]>:
See http://xmlgraphics.apache.org/fop/2.1/events.html for an example of
exactly that.

HTH
Jeremias Maerki


On 12.05.2016 23:12:22 Oskar Berggren wrote:
> Hi,
>
>
> Sometimes a document will reference an image that FOP is unable to find. With FOP 2.0, it will print a SEVERE log and then continue processing the file, eventually exiting with a regular zero exit code.
>
>
> Because there may be a significant amount of other logs, and because this FOP was invoked from a makefile that renders a bunch of documents, it's easy to miss the log about the missing image. And because FOP appears to not use a non-zero exit code, the make command will also not complain.
>
>
> How can I make FOP scream louder on missing images (and perhaps also other errors)? It could complete processing the file, but crucially I would like it to exit with a non-zero exit code so that make will stop with an error message.
>
>
>
> /Oskar
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Fail hard on missing images

Jeremias Maerki-2
Yes, at the moment, this only works when interfacing through Java. The
ability to configure which events cause what for the command-line
interface has simply not been implemented, yet. I very rarely run FOP
from the shell. For me personally, FOP is primarily a software library
to integrate in other Java applications. But it happens to have a
command-line interface and a rather neglected Ant task plug-in.

I guess being able to fine-tune event behaviour through the user
configuration could be useful. But again, it's just not implemented, yet.
Given that you had to wait two weeks for an answer to your question
indicates that the event feature is known to and used by few. It doesn't
seem to be a popular requirement. In that way, it wasn't a design choice
but a missing demand. But the current design in no way stays in the way
of implementing what you expected.

Jeremias Maerki


On 29.05.2016 19:16:12 Oskar Berggren wrote:

> Are you saying I need to create my own fop runner to do such a thing? It can't be done using the regular fop script?
>
>
> Seems like an unfortunate design choice. Or maybe I'm missing something... I always thought the fop script was the primary method of using fop, but perhaps I'm wrong.
>
>
> /Oskar
>
>
>
> 2016-05-26 14:10 GMT+01:00 Jeremias Maerki <[hidden email]>:
> See http://xmlgraphics.apache.org/fop/2.1/events.html for an example of
> >  exactly that.
> >
> >  HTH
> >  Jeremias Maerki
> >
> >
> >  On 12.05.2016 23:12:22 Oskar Berggren wrote:
> >  > Hi,
> >  >
> >  >
> >  > Sometimes a document will reference an image that FOP is unable to find. With FOP 2.0, it will print a SEVERE log and then continue processing the file, eventually exiting with a regular zero exit code.
> >  >
> >  >
> >  > Because there may be a significant amount of other logs, and because this FOP was invoked from a makefile that renders a bunch of documents, it's easy to miss the log about the missing image. And because FOP appears to not use a non-zero exit code, the make command will also not complain.
> >  >
> >  >
> >  > How can I make FOP scream louder on missing images (and perhaps also other errors)? It could complete processing the file, but crucially I would like it to exit with a non-zero exit code so that make will stop with an error message.
> >  >
> >  >
> >  >
> >  > /Oskar
> >  >
> >  >
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> >  To unsubscribe, e-mail: [hidden email]
> >  For additional commands, e-mail: [hidden email]
> >
>
>
>
>
>
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]