package org.ateliermyosotis.commun.pdf; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.SQLException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamSource; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; //import org.apache.fop.apps.MimeConstants; import org.apache.xmlgraphics.util.MimeConstants; //import org.apache.fop.apps.Fop; //import org.apache.fop.apps.FopFactory; //import org.apache.fop.apps.MimeConstants; public class FopTransformeur { public FopTransformeur() { super(); // TODO Auto-generated constructor stub } public void transformePDF(String fichier, String fichierXSL, String type) throws Exception { // TODO Auto-generated constructor stub FopFactory fopFactiory = FopFactory.newInstance(); // String xml = fichier + ".fo"; String xml = fichier + ".xml"; String pdf = null; InputStream ifo = null; OutputStream opdf = null; StreamSource stylesource = null; InputStream ixsl = null; try { ifo = new FileInputStream(xml); Fop fop = null; if ("-PDF".equals(type)) { pdf = fichier + ".pdf"; opdf = new FileOutputStream(pdf); fop = fopFactiory.newFop(MimeConstants.MIME_PDF, opdf); } if ("-TXT".equals(type)) { pdf = fichier + ".txt"; opdf = new FileOutputStream(pdf); fop = fopFactiory.newFop(MimeConstants.MIME_PLAIN_TEXT, opdf); } // String feuilleStyle = "/feuillesdestyle" + fichierXSL; String feuilleStyle = "/" + fichierXSL; // String feuilleStyle = fichierXSL; TransformerFactory factory = TransformerFactory.newInstance(); stylesource = new StreamSource(ClassLoader.class.getResourceAsStream(feuilleStyle)); // stylesource = new StreamSource(feuilleStyle); System.out.print(fichier + "\n"); System.out.print(stylesource.toString() + "\n"); Transformer transformer = factory.newTransformer(stylesource); Source source = new StreamSource(ifo); Result result = new SAXResult(fop.getDefaultHandler()); transformer.transform(source, result); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); throw new Exception(e); } finally { try { opdf.flush(); } finally { try { opdf.close(); } finally { ifo.close(); if (!(ixsl == null)) ixsl.close(); } } } } public void transforme(String fichier, String type) { // TODO Auto-generated constructor stub String fo = fichier + ".fo"; // String pdf = type; String fop = "fop.bat " + "\"" + fo + "\"" + " " + type; Runtime runtime = Runtime.getRuntime(); System.out.println("type fop" + fop); Process process = null; byte[] b = new byte[1024]; try { process = runtime.exec(fop); InputStream ise = process.getErrorStream(); // InputStream is = process.getInputStream(); // Fop } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }