Package cl.molavec.xslfo

Source Code of cl.molavec.xslfo.SimpleQuotation2PDF

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 java.io.PrintWriter;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;

public class SimpleQuotation2PDF {

  /**
   * @param args
   * @throws TransformerException
   * @throws IOException
   * @throws ParserConfigurationException
   * @throws SAXException
   */
  public static void main(String[] args) throws TransformerException, IOException, ParserConfigurationException, SAXException {
    // 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+
        "quotation.pdf")));

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

        // 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+
          "quotation.xsl"));
        Transformer transformer = factory.newTransformer(xslt);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        // 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+
          "quotation.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);
       
        //Console
        transformer.transform(src, xmlOutput);
        System.out.println(xmlOutput.getWriter().toString());
       
        //File
        PrintWriter outFile = new PrintWriter(
            "."+File.separator+"resources"+File.separator+"templates"+File.separator+
          "quotation_converted.xml");
        outFile.println(xmlOutput.getWriter().toString());
        outFile.close();
       

    } finally {
        //Clean-up
        out.close();
    }
  }

}
TOP

Related Classes of cl.molavec.xslfo.SimpleQuotation2PDF

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.