Hello all

On Wed, 20 May 2009 11:17:42 +0100
Vincent Hennebert <[email protected]> wrote:

> Are you sure you closed any applicable stream properly? That___s the
> only thing I can think of and might explain why it___s working on some
> platforms and not others.

Yes, I do.

But I guess I found the problem. Although no solution yet (looking for
comments).

I changed to encoding in the Transformer and the ByteArrayOutputStream to
UTF-8. With this change I have an empty PDF on my development system as
well. When I change back to Charset.defaultCharset which is ISO-8859-1 on
this system, the PDF contains text again.

Now I currently have no access to the other systems to test. But I guess
that is my problem.

What I don't understand is why when changing the encoding to UTF-8 I do
not get any text? IMHO that should work? Or is there a third object in my
transformation which needs to have the encoding set? What I do is this:

private ResourceStreamRequestTarget convertFO2PDF(String strFO, String
strAuthor, String strTitle)     {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                StringReader sr = new StringReader(strFO);
                
                // String strEncoding = "UTF-8";
                String strEncoding = Charset.defaultCharset().name();
                
                LOG.info(MessageFormat.format("Default charset: {0}",
Charset.defaultCharset()));             LOG.info(MessageFormat.format("Chosen
charset: {0}", strEncoding));           
LOG.info(MessageFormat.format("Available charsets: {0}",
Charset.availableCharsets()));          
        try
        {
                FopFactory fopFactory = FopFactory.newInstance();
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            // configure foUserAgent as desired
            foUserAgent.setCreator("Gozer.FOP");
            foUserAgent.setAuthor(strAuthor);
            foUserAgent.setTitle(strTitle);
            foUserAgent.setCreationDate(new Date());
            
            // TODO: load a config for the fopFactory
            
            // Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
foUserAgent, out);

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

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

            // Resulting SAX events (the generated FO) must be piped
through to FOP            Result res = new
SAXResult(fop.getDefaultHandler());            
            transformer.setErrorListener(new
FopTransformerErrorListener());           
transformer.setOutputProperty(OutputKeys.ENCODING, strEncoding);          
             // Start XSLT transformation and FOP processing
            System.setProperty("java.awt.headless", "true");
                LOG.info("Headless mode before FOPing: " +
GraphicsEnvironment.isHeadless());              
                transformer.transform(src, res);
        }
        catch (Exception e)
        {
            LOG.error(MessageFormat.format("FOP transformation crashed:
{0}", e.getLocalizedMessage()));        }
        finally
        {
                try
                {
                        out.close();
                        sr.close();
                }
            catch (Exception e)
            {
                LOG.error(MessageFormat.format("FOP transformation
finalisation crashed: {0}", e.getLocalizedMessage()));            }
        }
  
        ResourceStreamRequestTarget ret = null;
        try
        {
                // MimeConstants.MIME_PDF; // MimeConstants.MIME_RTF
                ret = new ResourceStreamRequestTarget(new
StringResourceStream(out.toString(strEncoding), MimeConstants.MIME_PDF)); 
      }        catch(UnsupportedEncodingException e)
        {
                LOG.error(MessageFormat.format("Encoding crashed: {0}",
e.getLocalizedMessage()));        }
        
        return ret;
    }


Any help appreciated. :)

Cheers,
Adrian

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

Reply via email to