package cl.molavec.xslfo;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import cl.molavec.jaxb.Content;
public class simpleObject2PDF {
/**
* @param args
* @throws FOPException
* @throws TransformerException
* @throws IOException
* @throws JAXBException
*/
public static void main(String[] args) throws FOPException, TransformerException, IOException, JAXBException {
//Buffer, InputStream and OutputStream
byte[] xmlBuffer= new byte[1024*1024];
OutputStream xmlOutputStream= new ByteArrayOutputStream(1024);
//InputStream xmlInputStream= new InputStream();
Content content = new Content();
content.setTitle("Hello W3Schools - Object2PDF");
content.setDescription(
"At W3Schools you will find all the Web-building tutorials you"+
"need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP."
);
JAXBContext jaxbContext = JAXBContext.newInstance(Content.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.marshal(content, xmlOutputStream);
//IOUtils.copy(xmlInputStream,xmlOutputStream);
// Step 1: Construct a FopFactory
// (reuse if you plan to render multiple documents!)
FopFactory fopFactory = FopFactory.newInstance();
// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream fopOutputStream = new BufferedOutputStream(new FileOutputStream(new File(
"."+File.separator+"resources"+File.separator+"pdfs"+File.separator+
"example_03.pdf")));
try {
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, fopOutputStream);
// Step 4: Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
//with XSLT:
Source xslt = new StreamSource(new File(
"."+File.separator+"resources"+File.separator+"templates"+File.separator+
"example_02.xsl"));
Transformer transformer = factory.newTransformer(xslt);
// Step 5: Setup input and output for XSLT transformation
// Setup input stream
Source src = new StreamSource(new ByteArrayInputStream(xmlBuffer));
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Step 6: Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
//Clean-up
fopOutputStream.close();
}
}
}