You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Adrian Wiesmann <aw...@somap.org> on 2009/05/23 10:42:47 UTC

Wrong encoding results in empty PDF? Was: Re: Empty PDF (on Windows)

Hello all

On Wed, 20 May 2009 11:17:42 +0100
Vincent Hennebert <vh...@gmail.com> 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: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


Re: Wrong encoding results in empty PDF? Was: Re: Empty PDF (on Windows)

Posted by Adrian Wiesmann <aw...@somap.org>.
Hi Vincent

> (Sorry for the delay.)

Absolutely no problem! Thanks for your help.

> You shouldn___t use
> the BAOS.toString method, but its write or writeTo methods instead, and
> use binary streams in the rest of your environment.

Damned, you are right!

I was very much concentrating on the Wicket side and how to feed the data
to the client without storing it on disc that I did not think about PDF
being binary and that there is actually a method to feed binary data
directly to the client with Wicket (although it is quite new :) ).

Now that I removed halve of my code and moved to binary only everything
works as expected. Thanks again for your help.

Cheers,
Adrian

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


Re: Wrong encoding results in empty PDF? Was: Re: Empty PDF (on Windows)

Posted by Vincent Hennebert <vh...@gmail.com>.
Hi Adrian,

(Sorry for the delay.)

I’ve tried to reproduce your problem and I think it comes from the fact
that you are handling the ByteArrayOutputStream as a character stream.
A PDF file is not a text file, it’s a binary file that just appears to
be partially readable in a text editor. You shouldn’t use the
BAOS.toString method, but its write or writeTo methods instead, and use
binary streams in the rest of your environment.
By doing this you shouldn’t have to play with the encoding, at that
stage of the processing at least. You ‘just’ have to make sure that the
strFO string is properly produced.

HTH,
Vincent


Adrian Wiesmann wrote:
> Hello all
> 
> On Wed, 20 May 2009 11:17:42 +0100
> Vincent Hennebert <vh...@gmail.com> 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: fop-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org