import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.logger.NullLogger;
import org.apache.fop.apps.Driver;
import org.apache.fop.messaging.MessageHandler;
import org.apache.fop.tools.DocumentInputSource;
import org.w3c.dom.Document;
public class Foptest {
public static void main(String[] args) {
Fopping();
}
public static void Fopping() {
Transformer transformer = null;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
org.apache.xalan.templates.StylesheetRoot xslInput = null;
try {
StreamSource sourceStream = new StreamSource(new File("exportPDF.xslt")); //
sourceStream.setSystemId(new File("exportPDF.xslt"));
xslInput = (org.apache.xalan.templates.StylesheetRoot) transformerFactory.newTemplates(sourceStream);
} catch (Exception exptn) {
exptn.printStackTrace();
}
synchronized (xslInput) {
transformer = xslInput.newTransformer();
}
DOMResult result = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("test.xml"));
Source source = new DOMSource(document);
result = new DOMResult();
transformer.transform(source, result);
} catch (Exception exptn) {
exptn.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
if (result == null) {
return;
}
/* Input into FOP. */
DocumentInputSource sourceFOP = new DocumentInputSource();
sourceFOP.setDocument((Document) result.getNode());
ByteArrayOutputStream out = new ByteArrayOutputStream();
/* Set up the FOP driver... */
Driver driver = new Driver(sourceFOP, out);
/* Logging */
Logger logger = new NullLogger();
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);
driver.setRenderer(Driver.RENDER_PDF);
try {
//Hier knallst
driver.run();
} catch (org.apache.fop.apps.FOPException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
byte[] outPutPDF = out.toByteArray();
try {
FileOutputStream fileOut = new FileOutputStream("test.pdf", false);
fileOut.write(outPutPDF);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioE) {
ioE.printStackTrace();
}
}
}