开发者

FOP and IKVM in .NET - Images Not Working

开发者 https://www.devze.com 2023-04-03 03:26 出处:网络
UPDATE2: I got it working completely now! Scroll way down to find out how... UPDATE: I got it working! Well... partially. Scroll down for the answer...

UPDATE2: I got it working completely now! Scroll way down to find out how...

UPDATE: I got it working! Well... partially. Scroll down for the answer...

I'm trying to get my FO file to show an external image upon transforming it to PDF (or RTF for that matter, but I'm not sure whether RTFs are even capable of displaying images (they are)) with FOP, but I can't seem to get it working. (The question asked here is different than mine.)

I am using IKVM 0.46.0.1 and have compiled a FOP 1.0 dll to put in .NET; this code worked fine when I didn't try to add images:

private void convertFoByMimetype(java.io.File fo, java.io.File outfile, string mimetype)
{
    OutputStream output = null;

    try
    {
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        // configure foUserAgent as desired

        // Setup ou开发者_StackOverflow中文版tputput stream.  Note: Using BufferedOutputStream
        // for performance reasons (helpful with FileOutputStreams).
        output = new FileOutputStream(outfile);
        output = new BufferedOutputStream(output);

        // Construct fop with desired output format
        Fop fop = fopFactory.newFop(mimetype, foUserAgent, output);

        // Setup JAXP using identity transformer
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); // identity transformer

        // Setup input stream
        Source src = new StreamSource(fo);

        // Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        // Start XSLT transformation and FOP processing
        transformer.transform(src, res);
    }
    catch (Exception ex)
    ...
}

However, when I (or rather a DocBook2FO transformation) added the following code:

<fo:external-graphic src="url(images/interface.png)" width="auto" height="auto" content-width="auto" content-height="auto" content-type="content-type:image/png"></fo:external-graphic>

into the FO file, the image did not show. I read through a bit of the FAQ on Apache's site, which says:

3.3. Why is my graphic not rendered?

Most commonly, the external file is not being found by FOP. Check the following:

Empty or wrong baseDir setting.

Spelling errors in the file name (including using the wrong case).

...

Other options did not seem to be my case (mainly for the reason below - "The Weird Part"). I tried this:

...
try
{
    fopFactory.setBaseURL(fo.getParent());
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    foUserAgent.setBaseURL(fo.getParent());
    FOURIResolver fourir = fopFactory.getFOURIResolver();
    foUserAgent.setURIResolver(fourir);
    // configure foUserAgent as desired
...

with no avail.

The Weird Part

When I use the command-line implementation of FOP, it works fine and displays my image with no problem. (I don't want to go the run-command-line-from-program route, because I don't want to force the users to install Java AND the .NET framework when they want to use my program.)

The png file is generated from GDI+ from within my application (using Bitmap.Save). I also tried different png files, but none of them worked for me.

Is there anything I might be missing?

Thanks a bunch for getting this far

UPDATE and possible answer

So I might have figured out why it didn't work. I put some time into studying the code (before I basically just copypasted it without thinking about it much). The problem is indeed in the wrong basedir setting.

The key is in this chunk of code:

    // Setup JAXP using identity transformer
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(); // identity transformer

    // Setup input stream
    Source src = new StreamSource(fo);

    // Resulting SAX events (the generated FO) must be piped through to FOP
    Result res = new SAXResult(fop.getDefaultHandler());

    // Start XSLT transformation and FOP processing
    transformer.transform(src, res);

What happens here is an identity transformation, which routes its own result into an instance of FOP I've created before. This effectively changes the basedir of the routed FO into that of the application's executable. I have yet to figure out how to do this without a transformation and route my input directly into FOP, but for the moment I worked around this by copying my images into the executable's directory.

Which is where another problem came in. Now whenever I try to execute the code, I get an exception at the line that says transformer.transform(src, res);, which confuses the pants out of me, because it doesn't say anything. The ExceptionHelper says:

java.lang.ExceptionInInitializerError was caught

and there is no inner exception or exception message. I know this is hard to debug just from what I wrote, but I'm hoping there might be an easy fix.

Also, this e-mail seems vaguely related but there is no answer to it.

UPDATE2

Finally, after a few sleepless nights, I managed to get it working with one of the simplest ways possible.

I updated IKVM, compiled fop with the new version and replaced the IKVM references with the new dlls. The error no longer occurs and my image renders fine.

I hope this helps someone someday


I'm using very similar code, although without the FOUserAgent, Resolvers, etc. and it works perfectly.

Did you try setting the src attribute in the XSLT without the url() function?

What might help you diagnose the problem further are the following statements:

java.lang.System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog")
java.lang.System.setErr(New java.io.PrintStream(New TraceStream(TraceStream.Level.Error)))
java.lang.System.setOut(New java.io.PrintStream(New TraceStream(TraceStream.Level.Info)))

Where TraceStream is a .NET implementation of a java.io.OutputStream which writes to your favorite logger. I posted a version for the Common.Logging package at http://pastebin.com/XH1Wg7jn.


Here's a post not to leave the question unanswered, see "Update 1" and "Update 2" in the original post for the solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消