Package cl.molavec.xslfo

Source Code of cl.molavec.xslfo.simpleFO2PDF

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 simpleFO2PDF {

  private static String FILENAME = "quotation_template";
  private static String PATH =
      "."+File.separator+"resources"+File.separator+"templates"+File.separator+
      "fo"+File.separator+
      "basic"+File.separator;
     
  private static String EXAMPLE_FILENAME_FO = PATH + FILENAME +".fo";
  private static String EXAMPLE_FILENAME_PDF =PATH + FILENAME +".pdf";

  /**
   * @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(EXAMPLE_FILENAME_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();
        Transformer transformer = factory.newTransformer(); // identity transformer

        // Step 5: Setup input and output for XSLT transformation
        // Setup input stream
        Source src = new StreamSource(new File(EXAMPLE_FILENAME_FO ));

        // 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.simpleFO2PDF

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.