Loading ...
Sorry, an error occurred while loading the content.
 

Re: [XSL-FO] Outputting to ByteArrayOutputStream results in blank document on Windows

Expand Messages
  • Werner Donné
    I think this is an encoding problem. Your code doesn t specify the encoding, neither when reading the input, nor when creating a string with the result.
    Message 1 of 4 , Sep 9, 2004
      I think this is an encoding problem. Your code doesn't specify the encoding,
      neither when reading the input, nor when creating a string with the result.
      Because of that the default encoding, which can vary depending on the system,
      is used.

      Instead of using new FileReader(file) you should write:

      new InputStreamReader(new FileInputStream(file), <encoding>);

      And instead of outputStream.toString() it should be outputSteam.toString(<encoding).
      Having said that, I wouldn't convert the PDF byte array to a string but keep it
      as a byte array.

      Werner.

      spbutman wrote:
      > Hello All:
      >
      > As part of a larger program, I've written a Java method to take an XML
      > document as a parameter, convert it to PDF (using FOP 0.20.5), and
      > return the converted string. It works just as expexted on Linux.
      > Unfortunately, the same is not true on Windows. Whenever I run the
      > program on Windows I get a series of blank pages. It's the correct
      > number of blank pages, but that's little comfort. ;-)
      >
      > I've slimmed the code down as much as I could and pasted it to the end
      > of this email. Additionally, I have created a zip file at
      > http://www.technologycrossing.com/wintest.zip that illustrates the
      > problem. If you download and unpack the zip file, run 'ant clean' and
      > 'ant', you'll see what I mean. It works fine under Linux, but gives
      > blank pages under windows. (Sorry about the length of the zip file.
      > I've included several external jar files for the sake of
      > completeness.)
      >
      > I've tried running under different versions of Java and different
      > versions of windows, all with the same result.
      >
      > Any help would be gratefully accepted. Thanks in advance.
      >
      > Here is a class demonstrating the problem:
      >
      > import java.io.ByteArrayOutputStream;
      > import java.io.File;
      > import java.io.FileOutputStream;
      > import java.io.FileReader;
      > import java.io.IOException;
      > import java.io.StringReader;
      > import java.util.Properties;
      > import org.apache.fop.apps.Driver;
      > import org.apache.fop.apps.FOPException;
      > import org.apache.fop.messaging.MessageHandler;
      > import org.apache.log4j.Logger;
      > import org.apache.log4j.PropertyConfigurator;
      > import org.xml.sax.InputSource;
      > import java.io.FileNotFoundException;
      >
      > class WindowsTest {
      > /**
      > * Used to log events.
      > */
      > private static Logger logger = Logger.getLogger( WindowsTest.class );
      >
      > public static void main( String args[] ) {
      > Properties properties = new Properties( );
      > properties.setProperty( "log4j.rootLogger", "INFO, testLog" );
      > properties.setProperty( "log4j.appender.testLog",
      > "org.apache.log4j.ConsoleAppender" );
      > properties.setProperty( "log4j.appender.testLog.layout",
      > "org.apache.log4j.PatternLayout" );
      > properties.setProperty(
      > "log4j.appender.testLog.layout.ConversionPattern",
      > "%d{ISO8601} %-5p: %m%n" );
      > PropertyConfigurator.configure( properties );
      > try {
      > // Read contents of file passed as first parameter.
      > File foFile = new File( args[0] );
      > char buffer[] = new char[( (int) foFile.length( ) )];
      > FileReader fileReader = new FileReader( foFile );
      > fileReader.read( buffer );
      > fileReader.close( );
      > String fo = new String( buffer );
      > String pdf = getPdf( fo );
      > if( pdf != null ) {
      > FileOutputStream output = new FileOutputStream( args[1] );
      > output.write( pdf.getBytes( ) );
      > output.close( );
      > }
      > }
      > catch( FileNotFoundException fnfe ) {
      > System.out.println( "FileNotFoundException encountered "
      > + "when trying to open file." );
      > fnfe.printStackTrace( );
      > }
      > catch( IOException ioe ) {
      > System.out.println( "I/O Error while creating files." );
      > ioe.printStackTrace( );
      > }
      > }
      >
      > private static String getPdf( String hrXml ) {
      > String pdf = null;
      > try {
      > org.apache.avalon.framework.logger.Logger localLogger = new
      > org.apache.avalon.framework.logger.Log4JLogger(
      > logger );
      > ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
      > Driver driver = new Driver(
      > new InputSource( new StringReader( hrXml ) ), outputStream );
      > MessageHandler.setScreenLogger( localLogger );
      > driver.setLogger( localLogger );
      > driver.setRenderer( Driver.RENDER_PDF );
      > driver.run( );
      > pdf = outputStream.toString( ).trim( );
      > }
      > catch( FOPException fope ) {
      > logger.error( "FOP exception encountered while transforming to
      > PDF." );
      > logger.info( "The error encountered was:\n" + fope.getMessage( ) );
      > }
      > catch( IOException ioe ) {
      > logger
      > .error( "I/O configuration exception encountered while
      > transforming to PDF." );
      > logger.info( "The error encountered was:\n" + ioe.getMessage( ) );
      > }
      > return ( pdf );
      > }
      > }
      >
      >
      >
      >
      >
      > Yahoo! Groups Links
      >
      >
      >
      >
      >
      >

      --
      Werner Donné -- Re BVBA
      Engelbeekstraat 8
      B-3300 Tienen
      tel: (+32) 486 425803 e-mail: werner.donne@...
    • Victor Vishnyakov
      ... ByteArrayOutputStream( ); ... hrXml ) ), outputStream ); ... As far as PDF file is binary in the most cases (ASCII filters are used rarely) you should not
      Message 2 of 4 , Sep 9, 2004
        > ByteArrayOutputStream outputStream = new
        ByteArrayOutputStream( );
        > Driver driver = new Driver(
        > new InputSource( new StringReader(
        hrXml ) ), outputStream );
        > MessageHandler.setScreenLogger( localLogger );
        > driver.setLogger( localLogger );
        > driver.setRenderer( Driver.RENDER_PDF );
        > driver.run( );
        > pdf = outputStream.toString( ).trim( );

        As far as PDF file is binary in the most cases (ASCII filters are used
        rarely) you should not convert PDF to string. Keep it binary.

        Regards,
        Victor Vishnaykov



        > -----Original Message-----
        > From: spbutman [mailto:spbutman@...]
        > Sent: Thursday, September 09, 2004 7:02 AM
        > To: XSL-FO@yahoogroups.com
        > Subject: [XSL-FO] Outputting to ByteArrayOutputStream results
        > in blank document on Windows
      • spbutman
        You are correct; it was an encoding problem. I changed my method to return a byte array instead of a String and everything works beautifully. Thanks to
        Message 3 of 4 , Sep 9, 2004
          You are correct; it was an encoding problem. I changed my method to
          return a byte array instead of a String and everything works
          beautifully.

          Thanks to everyone for the help!


          --- In XSL-FO@yahoogroups.com, Werner Donné <werner.donne@r...>
          wrote:
          > I think this is an encoding problem. Your code doesn't specify the
          encoding,
          > neither when reading the input, nor when creating a string with the
          result.
          > Because of that the default encoding, which can vary depending on
          the system,
          > is used.
          >
          > Instead of using new FileReader(file) you should write:
          >
          > new InputStreamReader(new FileInputStream(file), <encoding>);
          >
          > And instead of outputStream.toString() it should be
          outputSteam.toString(<encoding).
          > Having said that, I wouldn't convert the PDF byte array to a string
          but keep it
          > as a byte array.
          >
          > Werner.
          >
          > spbutman wrote:
          > > Hello All:
          > >
          > > As part of a larger program, I've written a Java method to take
          an XML
          > > document as a parameter, convert it to PDF (using FOP 0.20.5), and
          > > return the converted string. It works just as expexted on Linux.
          > > Unfortunately, the same is not true on Windows. Whenever I run
          the
          > > program on Windows I get a series of blank pages. It's the
          correct
          > > number of blank pages, but that's little comfort. ;-)
          > >
          > > I've slimmed the code down as much as I could and pasted it to
          the end
          > > of this email. Additionally, I have created a zip file at
          > > http://www.technologycrossing.com/wintest.zip that illustrates the
          > > problem. If you download and unpack the zip file, run 'ant
          clean' and
          > > 'ant', you'll see what I mean. It works fine under Linux, but
          gives
          > > blank pages under windows. (Sorry about the length of the zip
          file.
          > > I've included several external jar files for the sake of
          > > completeness.)
          > >
          > > I've tried running under different versions of Java and different
          > > versions of windows, all with the same result.
          > >
          > > Any help would be gratefully accepted. Thanks in advance.
          > >
          > > Here is a class demonstrating the problem:
          > >
          > > import java.io.ByteArrayOutputStream;
          > > import java.io.File;
          > > import java.io.FileOutputStream;
          > > import java.io.FileReader;
          > > import java.io.IOException;
          > > import java.io.StringReader;
          > > import java.util.Properties;
          > > import org.apache.fop.apps.Driver;
          > > import org.apache.fop.apps.FOPException;
          > > import org.apache.fop.messaging.MessageHandler;
          > > import org.apache.log4j.Logger;
          > > import org.apache.log4j.PropertyConfigurator;
          > > import org.xml.sax.InputSource;
          > > import java.io.FileNotFoundException;
          > >
          > > class WindowsTest {
          > > /**
          > > * Used to log events.
          > > */
          > > private static Logger logger = Logger.getLogger(
          WindowsTest.class );
          > >
          > > public static void main( String args[] ) {
          > > Properties properties = new Properties( );
          > > properties.setProperty( "log4j.rootLogger", "INFO, testLog" );
          > > properties.setProperty( "log4j.appender.testLog",
          > > "org.apache.log4j.ConsoleAppender" );
          > > properties.setProperty( "log4j.appender.testLog.layout",
          > > "org.apache.log4j.PatternLayout" );
          > > properties.setProperty(
          > > "log4j.appender.testLog.layout.ConversionPattern",
          > > "%d{ISO8601} %-5p: %m%n" );
          > > PropertyConfigurator.configure( properties );
          > > try {
          > > // Read contents of file passed as first parameter.
          > > File foFile = new File( args[0] );
          > > char buffer[] = new char[( (int) foFile.length( ) )];
          > > FileReader fileReader = new FileReader( foFile );
          > > fileReader.read( buffer );
          > > fileReader.close( );
          > > String fo = new String( buffer );
          > > String pdf = getPdf( fo );
          > > if( pdf != null ) {
          > > FileOutputStream output = new FileOutputStream( args[1] );
          > > output.write( pdf.getBytes( ) );
          > > output.close( );
          > > }
          > > }
          > > catch( FileNotFoundException fnfe ) {
          > > System.out.println( "FileNotFoundException encountered "
          > > + "when trying to open file." );
          > > fnfe.printStackTrace( );
          > > }
          > > catch( IOException ioe ) {
          > > System.out.println( "I/O Error while creating files." );
          > > ioe.printStackTrace( );
          > > }
          > > }
          > >
          > > private static String getPdf( String hrXml ) {
          > > String pdf = null;
          > > try {
          > > org.apache.avalon.framework.logger.Logger localLogger = new
          > > org.apache.avalon.framework.logger.Log4JLogger(
          > > logger );
          > > ByteArrayOutputStream outputStream = new By
          teArrayOutputStream( );
          > > Driver driver = new Driver(
          > > new InputSource( new StringReader( hrXml ) ), outputStream );
          > > MessageHandler.setScreenLogger( localLogger );
          > > driver.setLogger( localLogger );
          > > driver.setRenderer( Driver.RENDER_PDF );
          > > driver.run( );
          > > pdf = outputStream.toString( ).trim( );
          > > }
          > > catch( FOPException fope ) {
          > > logger.error( "FOP exception encountered while transforming to
          > > PDF." );
          > > logger.info( "The error encountered was:\n" + fope.getMessage(
          ) );
          > > }
          > > catch( IOException ioe ) {
          > > logger
          > > .error( "I/O configuration exception encountered while
          > > transforming to PDF." );
          > > logger.info( "The error encountered was:\n" + ioe.getMessage(
          ) );
          > > }
          > > return ( pdf );
          > > }
          > > }
          > >
          > >
          > >
          > >
          > >
          > > Yahoo! Groups Links
          > >
          > >
          > >
          > >
          > >
          > >
          >
          > --
          > Werner Donné -- Re BVBA
          > Engelbeekstraat 8
          > B-3300 Tienen
          > tel: (+32) 486 425803 e-mail: werner.donne@r...
        Your message has been successfully submitted and would be delivered to recipients shortly.