Package cl.molavec.xslfo

Source Code of cl.molavec.xslfo.simpleXSLT2PDF

package cl.molavec.xslfo;

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

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.fop.apps.FOPException;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;

public class simpleXSLT2PDF {

  /**
   * @param args
   * @throws FOPException
   * @throws TransformerException
   * @throws IOException
   */
  public static void main(String[] args) throws FOPException, TransformerException, IOException {
    // 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 out = new BufferedOutputStream(new FileOutputStream(new File(
        "."+File.separator+"resources"+File.separator+"pdfs"+File.separator+
        "example_02.pdf")));

    try {
        // Step 3: Construct fop with desired output format
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

        // 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 File(
            "."+File.separator+"resources"+File.separator+"templates"+File.separator+
          "example_02.xml"));

        // 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
        out.close();
    }
  }

}
TOP

Related Classes of cl.molavec.xslfo.simpleXSLT2PDF

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.