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/15 19:14:01 UTC

Empty PDF on Windows

Hello everybody

I am having a strange problem with FOP and would like to hear your
comments. I am having a UI engine which renders HTML, Swing and now also
PDF via FOP. On some systems I get content in the PDF (text, image,
table), on other systems the PDF have the correct number of pages, but
the pages are empty (white). When opening the empty PDF, I can see the
header information looking all right (and I can see the creator tag
correctly in the PDF). 

The empty PDF are empty not only in Adobe Acrobat Professional, but also
in Reader, KPDF and other readers.

All systems have the same code. Used libraries seem to be the same on all
systems (Ant script compiles code and generates WAR).

Systems where everything works:

- Linux, Eclipse, SUN JDE 1.6, Tomcat 6.
- Windows Vista, Eclipse, SUN JDE 1.6, Tomcat 6.

Systems where it does not work:

- Linux, Tomcat 5, IBM JVM.
- Windows Vista, Tomcat 6, SUN JRE.

The obvious difference is that on the systems where the PDFs are generated
correctly the application is run from within Eclipse.

I get no error or obvious debug message on any system whatsoever.

Does anybody have an idea where I could look at or what to test to find
this bug or misconfiguration? Could it be a memory problem, something
missing..?

I found some threads discussing empty PDF but no solutions to my problem
so far.

Thanks for your help in advance.

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


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

Posted by Adrian Wiesmann <aw...@somap.org>.
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: Empty PDF (on Windows)

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

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.


HTH,
Vincent

Adrian Wiesmann wrote:
> Sam,
> 
> Thanks for your help.
> 
>> I had a very similar problem to this.  Just before I transformed the fo
>> to pdf I needed to set the system property to headless.  This would be
>> done like:
>> System.setProperty("java.awt.headless", "true");  Though I'd recommend
>> storing a string of the original value and resetting it to this value
>> after you trasformation.
> 
> I already do this when starting up the application. At least in the web
> version of the tool.
> 
> Unfortunately your hint did not result in a solution. I tried as you said.
> With no luck. I then added a transformer errorlistener to log any errors
> which might have been undetected otherwise. But the only failure I get is
> about a table not being 100% or something.
> 
> The situation is really weird: 
> 
> - Runnin on Linux using a SUN JDK running within Eclipse... 
> - ...as well as deployed to a Tomcat 6 as a war file works.
> 
> - Running on Windows deployed as a WAR file to Tomcat 6 running in a JRE
> does *not* work.
> 
> - Running on Linux using IBM JRE deployed to Tomcat 5 as a WAR file does
> *not* work.
> 
> - Running on Windows within Eclipse with a JDK and within Tomcat 6
> seems to work. At least as far as I can remember (have no access to that
> system right now).
> 
> And I always tried Firefox as well as IE. Just to make sure this is not a
> cache or mime issue.
> 
> I start to believe that this is a JRE vs JDK issue? I remember from my
> JasperReports days, that there was an issue on JRE systems and that I had
> to add a "compiler" JAR on those systems. Could this be the same here?
> 
> Cheers,
> Adrian


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


Re: Empty PDF (on Windows)

Posted by Adrian Wiesmann <aw...@somap.org>.
Sam,

Thanks for your help.

> I had a very similar problem to this.  Just before I transformed the fo
> to pdf I needed to set the system property to headless.  This would be
> done like:
> System.setProperty("java.awt.headless", "true");  Though I'd recommend
> storing a string of the original value and resetting it to this value
> after you trasformation.

I already do this when starting up the application. At least in the web
version of the tool.

Unfortunately your hint did not result in a solution. I tried as you said.
With no luck. I then added a transformer errorlistener to log any errors
which might have been undetected otherwise. But the only failure I get is
about a table not being 100% or something.

The situation is really weird: 

- Runnin on Linux using a SUN JDK running within Eclipse... 
- ...as well as deployed to a Tomcat 6 as a war file works.

- Running on Windows deployed as a WAR file to Tomcat 6 running in a JRE
does *not* work.

- Running on Linux using IBM JRE deployed to Tomcat 5 as a WAR file does
*not* work.

- Running on Windows within Eclipse with a JDK and within Tomcat 6
seems to work. At least as far as I can remember (have no access to that
system right now).

And I always tried Firefox as well as IE. Just to make sure this is not a
cache or mime issue.

I start to believe that this is a JRE vs JDK issue? I remember from my
JasperReports days, that there was an issue on JRE systems and that I had
to add a "compiler" JAR on those systems. Could this be the same here?

Cheers,
Adrian

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


Re: Empty PDF on Windows

Posted by Sam Fuqua <sa...@gmail.com>.
Adrian,

I had a very similar problem to this.  Just before I transformed the fo to
pdf I needed to set the system property to headless.  This would be done
like:
System.setProperty("java.awt.headless", "true");  Though I'd recommend
storing a string of the original value and resetting it to this value after
you trasformation.



On Fri, May 15, 2009 at 1:14 PM, Adrian Wiesmann <aw...@somap.org>wrote:

> Hello everybody
>
> I am having a strange problem with FOP and would like to hear your
> comments. I am having a UI engine which renders HTML, Swing and now also
> PDF via FOP. On some systems I get content in the PDF (text, image,
> table), on other systems the PDF have the correct number of pages, but
> the pages are empty (white). When opening the empty PDF, I can see the
> header information looking all right (and I can see the creator tag
> correctly in the PDF).
>
> The empty PDF are empty not only in Adobe Acrobat Professional, but also
> in Reader, KPDF and other readers.
>
> All systems have the same code. Used libraries seem to be the same on all
> systems (Ant script compiles code and generates WAR).
>
> Systems where everything works:
>
> - Linux, Eclipse, SUN JDE 1.6, Tomcat 6.
> - Windows Vista, Eclipse, SUN JDE 1.6, Tomcat 6.
>
> Systems where it does not work:
>
> - Linux, Tomcat 5, IBM JVM.
> - Windows Vista, Tomcat 6, SUN JRE.
>
> The obvious difference is that on the systems where the PDFs are generated
> correctly the application is run from within Eclipse.
>
> I get no error or obvious debug message on any system whatsoever.
>
> Does anybody have an idea where I could look at or what to test to find
> this bug or misconfiguration? Could it be a memory problem, something
> missing..?
>
> I found some threads discussing empty PDF but no solutions to my problem
> so far.
>
> Thanks for your help in advance.
>
> Cheers,
> Adrian
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org
>
>


-- 
Sam Fuqua
ΣΝ ΘΗ 454