Re: getting FOP to work with Java using 0.94

2007-11-02 Thread Michael Tracey Zellmann
Thanks.

closing the OutputStream fixed my problem, and your way of getting the
xml file is clearly better.

I used to get a Fop instance directly from the constructor, but that
is deprecated now, in favor of the FopFactory. That was the change I
had encountered.

On Nov 2, 2007 3:41 AM, Jeremias Maerki [EMAIL PROTECTED] wrote:
 FOP's API hasn't changed. I don't remember any change in that area that
 could trigger this, but I could also be wrong. Anyway, what I'm missing
 in your code snippet is a try..finally around the code where the
 OutputStream is closed in the finally section. That omission is most
 likely the reason for the file corruption.

 As a side-note, it's inefficient to load the XML file as a DOM and then
 pass it to the XSL transformation. Please take a look at our examples
 and use a SAX-based approach:
 http://xmlgraphics.apache.org/fop/0.94/embedding.html#ExampleXML2PDF

 Jeremias Maerki




 On 02.11.2007 00:42:58 Michael Tracey Zellmann wrote:
  I was using 0.93 successfully some time ago, and I have another
  application to use,
  so I downloaded 0.94.
 
  I am using Java 1.5.0_11
 
  I tried to set things up correctly, but the PDF I generated can't be
  read by Adobe Reader 8. I get a message that the file has been damaged
  and can't be repaired.
 
  I have some code that reads a source document, builds an Fop from a
  FopFactory using MimeConstants.MIME_PDF and an OutputStream. I build a
  Transformer from a TransformerFactory using an XSLT file, get a
  SAXResult from the fop, and then transform the source into the result.
 
  I have tried a very simple style sheet but no success.
 
  Can anyone help me understand what is wrong, or point me to a basic
  example that will work with the new version?
 
  Here is the code:
 
 
 DocumentBuilderFactory dbf =
  DocumentBuilderFactory.newInstance();
 DocumentBuilder db = null;
 Document input = null;
 try {
 db = dbf.newDocumentBuilder();
 input = db.parse(new
  File(input/astruct/primitives/System.xml));
 } catch (ParserConfigurationException e) { 
  e.printStackTrace();
 } catch (SAXException e) { e.printStackTrace();
 } catch (IOException e) { e.printStackTrace(); }
 Source src = new DOMSource(input);
 FopFactory fopFactory = FopFactory.newInstance();
 OutputStream out = null;
 File outputDir = new File(output);
 File outputFile = new File(outputDir, System.pdf);
 try {
 out = new BufferedOutputStream(new
  FileOutputStream(outputFile));
 } catch (FileNotFoundException e) {e.printStackTrace();}
 Fop fop = null;
 try {
 fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
 } catch (FOPException e) { e.printStackTrace(); }
 File xsltFile = new File(xslt/module-fo.xslt);
 TransformerFactory trFactory = 
  TransformerFactory.newInstance();
 Transformer transformer = null;
 try {
 transformer = trFactory.newTransformer(new
  StreamSource(xsltFile));
 } catch (TransformerConfigurationException e)
  {e.printStackTrace();}
 Result res = null;
 try {
 res = new SAXResult(fop.getDefaultHandler());
 } catch (FOPException e) { e.printStackTrace(); }
 try {
 transformer.transform(src, res);
 } catch (TransformerException e) { e.printStackTrace();}
 


 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: getting FOP to work with Java using 0.94

2007-11-02 Thread Jeremias Maerki
FOP's API hasn't changed. I don't remember any change in that area that
could trigger this, but I could also be wrong. Anyway, what I'm missing
in your code snippet is a try..finally around the code where the
OutputStream is closed in the finally section. That omission is most
likely the reason for the file corruption.

As a side-note, it's inefficient to load the XML file as a DOM and then
pass it to the XSL transformation. Please take a look at our examples
and use a SAX-based approach:
http://xmlgraphics.apache.org/fop/0.94/embedding.html#ExampleXML2PDF

Jeremias Maerki



On 02.11.2007 00:42:58 Michael Tracey Zellmann wrote:
 I was using 0.93 successfully some time ago, and I have another
 application to use,
 so I downloaded 0.94.
 
 I am using Java 1.5.0_11
 
 I tried to set things up correctly, but the PDF I generated can't be
 read by Adobe Reader 8. I get a message that the file has been damaged
 and can't be repaired.
 
 I have some code that reads a source document, builds an Fop from a
 FopFactory using MimeConstants.MIME_PDF and an OutputStream. I build a
 Transformer from a TransformerFactory using an XSLT file, get a
 SAXResult from the fop, and then transform the source into the result.
 
 I have tried a very simple style sheet but no success.
 
 Can anyone help me understand what is wrong, or point me to a basic
 example that will work with the new version?
 
 Here is the code:
 
 
DocumentBuilderFactory dbf =
 DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document input = null;
try {
db = dbf.newDocumentBuilder();
input = db.parse(new
 File(input/astruct/primitives/System.xml));
} catch (ParserConfigurationException e) { e.printStackTrace();
} catch (SAXException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace(); }
Source src = new DOMSource(input);
FopFactory fopFactory = FopFactory.newInstance();
OutputStream out = null;
File outputDir = new File(output);
File outputFile = new File(outputDir, System.pdf);
try {
out = new BufferedOutputStream(new
 FileOutputStream(outputFile));
} catch (FileNotFoundException e) {e.printStackTrace();}
Fop fop = null;
try {
fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
} catch (FOPException e) { e.printStackTrace(); }
File xsltFile = new File(xslt/module-fo.xslt);
TransformerFactory trFactory = 
 TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = trFactory.newTransformer(new
 StreamSource(xsltFile));
} catch (TransformerConfigurationException e)
 {e.printStackTrace();}
Result res = null;
try {
res = new SAXResult(fop.getDefaultHandler());
} catch (FOPException e) { e.printStackTrace(); }
try {
transformer.transform(src, res);
} catch (TransformerException e) { e.printStackTrace();}
 


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]