
/*
 * xslt2PDF.java
 *
 * Created on 19 de junio de 2007, 16:54
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package isorat;

import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;


import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

/**
 *
 * @author ricard
 */
public class XML2PDF {
    private String xmlname;
    private String xsltname;
    private String pdfname;
    
       /** Creates a new instance of xslt2PDF */
    public XML2PDF(String xmlname,String xsltname,String pdfname) {
        this.xmlname=xmlname;
        this.xsltname=xsltname;
        this.pdfname=pdfname;
  
    }
   public void generatePDF()
   {
          
        

   File xmlfile=new File(xmlname);
   File xsltfile=new File(xsltname);
   File pdffile=new File(pdfname);
   try{
   FopFactory fopFactory=FopFactory.newInstance();
   FOUserAgent foUserAgent=fopFactory.newFOUserAgent();
   OutputStream out= new java.io.FileOutputStream(pdffile);
   out = new java.io.BufferedOutputStream(out);
   
    Fop fop=fopFactory.newFop(MimeConstants.MIME_PDF,foUserAgent,out);
    TransformerFactory factory = TransformerFactory.newInstance();
    
    Transformer transformer= factory.newTransformer(new StreamSource(xsltfile));
    transformer.setParameter("versionParam","2.0");
   Source src= new StreamSource(xmlfile);
   Result res= new SAXResult(fop.getDefaultHandler());
     transformer.transform(src,res);
     out.close();
   
   System.out.println("Succes!!!");
   
   }catch(Exception e){
    e.printStackTrace(System.err);
    System.exit(-1);
   }
   
   }
   
//      public static void main(final String[] args) {
//
//       XML2PDF xml2pdf=new XML2PDF("iso_rat.xml","transform.xslt","output.pdf");
//       xml2pdf.generatePDF();
//
//    }
}
