Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.StringReader;
- import java.io.StringWriter;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.transform.Result;
- import javax.xml.transform.Source;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMResult;
- 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.w3c.tidy.Tidy;
- import org.w3c.dom.Document;
- import org.apache.fop.apps.FOPException;
- import org.apache.fop.apps.Fop;
- import org.apache.fop.apps.FopFactory;
- import org.apache.fop.apps.MimeConstants;
- /*
- * Class that converts HTML to PDF using
- * the DOM interfaces of JTidy, Xalan, and FOP.
- *
- * @author N. Afshartous
- *
- */
- public class Html2PDF {
- public static void main(String[] args) {
- // open file
- if (args.length != 2) {
- System.out.println("Usage: Html2Pdf htmlFile styleSheet");
- System.exit(1);
- }
- FileInputStream input = null;
- String htmlFileName = args[0];
- try {
- input = new FileInputStream(htmlFileName);
- }
- catch (java.io.FileNotFoundException e) {
- System.out.println("File not found: " + htmlFileName);
- }
- Tidy tidy = new Tidy();
- Document xmlDoc = tidy.parseDOM(input, null);
- Document foDoc = xml2FO(xmlDoc, args[1]);
- String pdfFileName = htmlFileName.substring(0, htmlFileName.indexOf(".")) + ".pdf";
- try {
- OutputStream pdf = new FileOutputStream(new File(pdfFileName));
- pdf.write(fo2Pdf(foDoc));
- }
- catch (java.io.FileNotFoundException e) {
- System.out.println("Error creating PDF: " + pdfFileName);
- }
- catch (java.io.IOException e) {
- System.out.println("Error writing PDF: " + pdfFileName);
- }
- }
- /*
- * Applies stylesheet to input.
- *
- * @param xml The xml input Document
- *
- * @param stylesheet Name of the stylesheet
- *
- * @return Document Result of the transform
- */
- private static Document xml2FO(Document xml, String styleSheet) {
- DOMSource xmlDomSource = new DOMSource(xml);
- DOMResult domResult = new DOMResult();
- Transformer transformer = getTransformer(styleSheet);
- if (transformer == null) {
- System.out.println("Error creating transformer for " + styleSheet);
- System.exit(1);
- }
- try {
- transformer.transform(xmlDomSource, domResult);
- }
- catch (javax.xml.transform.TransformerException e) {
- return null;
- }
- return (Document) domResult.getNode();
- }
- private static byte[] fo2Pdf(Document argFoDoc) {
- // Holding area for result
- byte[] result = null;
- System.out.println("got into fo2PDF");
- // 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 = null;
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- //out = new BufferedOutputStream(os);
- try {
- // Step 3: Construct fop with desired output format
- Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, os);
- // 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 StringReader(documentToString(argFoDoc)));
- //Source src = new StreamSource(new File("C:\\a_test\\test.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);
- // Step 7: Put the results back to a byte array
- result = os.toByteArray();
- // Nevermind - Ryan wants the stream
- } catch (FOPException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (TransformerConfigurationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (TransformerException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- //Clean-up
- //try {
- //out.close();
- // } catch (IOException e) {
- // Nothing to do - if we can't close it, then it must be already closed (or never opened)
- //}
- } // end finally
- return result;
- } // end fo2Pdf
- /*
- * Create and return a Transformer for the specified stylesheet.
- *
- * Based on the DOM2DOM.java example in the Xalan distribution.
- */
- private static Transformer getTransformer(String styleSheet) {
- try {
- TransformerFactory tFactory = TransformerFactory.newInstance();
- DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
- dFactory.setNamespaceAware(true);
- DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
- Document xslDoc = dBuilder.parse(styleSheet);
- DOMSource xslDomSource = new DOMSource(xslDoc);
- return tFactory.newTransformer(xslDomSource);
- }
- catch (javax.xml.transform.TransformerException e) {
- e.printStackTrace();
- return null;
- }
- catch (java.io.IOException e) {
- e.printStackTrace();
- return null;
- }
- catch (javax.xml.parsers.ParserConfigurationException e) {
- e.printStackTrace();
- return null;
- }
- catch (org.xml.sax.SAXException e) {
- e.printStackTrace();
- return null;
- }
- }
- private static String documentToString(org.w3c.dom.Document doc) throws TransformerException {
- // Create dom source for the document
- DOMSource domSource=new DOMSource(doc);
- // Create a string writer
- StringWriter stringWriter=new StringWriter();
- // Create the result stream for the transform
- StreamResult result = new StreamResult(stringWriter);
- // Create a Transformer to serialize the document
- TransformerFactory tFactory =TransformerFactory.newInstance();
- Transformer transformer = tFactory.newTransformer();
- transformer.setOutputProperty("indent","yes");
- // Transform the document to the result stream
- transformer.transform(domSource, result);
- return stringWriter.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement