Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.OutputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.StringReader;
  7. import java.io.StringWriter;
  8.  
  9. import javax.xml.parsers.DocumentBuilder;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import javax.xml.transform.Result;
  12. import javax.xml.transform.Source;
  13. import javax.xml.transform.Transformer;
  14. import javax.xml.transform.TransformerConfigurationException;
  15. import javax.xml.transform.TransformerException;
  16. import javax.xml.transform.TransformerFactory;
  17.  
  18. import javax.xml.transform.dom.DOMResult;
  19. import javax.xml.transform.dom.DOMSource;
  20. import javax.xml.transform.sax.SAXResult;
  21. import javax.xml.transform.stream.StreamResult;
  22. import javax.xml.transform.stream.StreamSource;
  23.  
  24. import org.w3c.tidy.Tidy;
  25. import org.w3c.dom.Document;
  26.  
  27. import org.apache.fop.apps.FOPException;
  28. import org.apache.fop.apps.Fop;
  29. import org.apache.fop.apps.FopFactory;
  30. import org.apache.fop.apps.MimeConstants;
  31.  
  32. /*
  33. * Class that converts HTML to PDF using
  34. * the DOM interfaces of JTidy, Xalan, and FOP.
  35. *
  36. * @author N. Afshartous
  37. *
  38. */
  39. public class Html2PDF {
  40.  
  41.  
  42. public static void main(String[] args) {
  43.  
  44. // open file
  45. if (args.length != 2) {
  46. System.out.println("Usage: Html2Pdf htmlFile styleSheet");
  47. System.exit(1);
  48. }
  49.  
  50. FileInputStream input = null;
  51. String htmlFileName = args[0];
  52.  
  53. try {
  54.  
  55. input = new FileInputStream(htmlFileName);
  56.  
  57. }
  58. catch (java.io.FileNotFoundException e) {
  59. System.out.println("File not found: " + htmlFileName);
  60. }
  61.  
  62. Tidy tidy = new Tidy();
  63. Document xmlDoc = tidy.parseDOM(input, null);
  64.  
  65. Document foDoc = xml2FO(xmlDoc, args[1]);
  66.  
  67. String pdfFileName = htmlFileName.substring(0, htmlFileName.indexOf(".")) + ".pdf";
  68. try {
  69. OutputStream pdf = new FileOutputStream(new File(pdfFileName));
  70. pdf.write(fo2Pdf(foDoc));
  71. }
  72. catch (java.io.FileNotFoundException e) {
  73. System.out.println("Error creating PDF: " + pdfFileName);
  74. }
  75. catch (java.io.IOException e) {
  76. System.out.println("Error writing PDF: " + pdfFileName);
  77. }
  78.  
  79.  
  80. }
  81.  
  82.  
  83. /*
  84. * Applies stylesheet to input.
  85. *
  86. * @param xml The xml input Document
  87. *
  88. * @param stylesheet Name of the stylesheet
  89. *
  90. * @return Document Result of the transform
  91. */
  92. private static Document xml2FO(Document xml, String styleSheet) {
  93.  
  94. DOMSource xmlDomSource = new DOMSource(xml);
  95. DOMResult domResult = new DOMResult();
  96.  
  97. Transformer transformer = getTransformer(styleSheet);
  98.  
  99. if (transformer == null) {
  100. System.out.println("Error creating transformer for " + styleSheet);
  101. System.exit(1);
  102. }
  103. try {
  104. transformer.transform(xmlDomSource, domResult);
  105. }
  106. catch (javax.xml.transform.TransformerException e) {
  107. return null;
  108. }
  109. return (Document) domResult.getNode();
  110.  
  111. }
  112.  
  113.  
  114. private static byte[] fo2Pdf(Document argFoDoc) {
  115. // Holding area for result
  116. byte[] result = null;
  117. System.out.println("got into fo2PDF");
  118.  
  119. // Step 1: Construct a FopFactory
  120. // (reuse if you plan to render multiple documents!)
  121. FopFactory fopFactory = FopFactory.newInstance();
  122.  
  123. // Step 2: Set up output stream.
  124. // Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
  125. OutputStream out = null;
  126. ByteArrayOutputStream os = new ByteArrayOutputStream();
  127. //out = new BufferedOutputStream(os);
  128.  
  129. try {
  130. // Step 3: Construct fop with desired output format
  131. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, os);
  132.  
  133. // Step 4: Setup JAXP using identity transformer
  134. TransformerFactory factory = TransformerFactory.newInstance();
  135. Transformer transformer = factory.newTransformer(); // identity transformer
  136.  
  137. // Step 5: Setup input and output for XSLT transformation
  138. // Setup input stream
  139. Source src = new StreamSource(new StringReader(documentToString(argFoDoc)));
  140. //Source src = new StreamSource(new File("C:\\a_test\\test.fo"));
  141.  
  142. // Resulting SAX events (the generated FO) must be piped through to FOP
  143. Result res = new SAXResult(fop.getDefaultHandler());
  144.  
  145.  
  146. // Step 6: Start XSLT transformation and FOP processing
  147. transformer.transform(src, res);
  148.  
  149. // Step 7: Put the results back to a byte array
  150. result = os.toByteArray();
  151. // Nevermind - Ryan wants the stream
  152.  
  153. } catch (FOPException e) {
  154. // TODO Auto-generated catch block
  155. e.printStackTrace();
  156. } catch (TransformerConfigurationException e) {
  157. // TODO Auto-generated catch block
  158. e.printStackTrace();
  159. } catch (TransformerException e) {
  160. // TODO Auto-generated catch block
  161. e.printStackTrace();
  162. } finally {
  163. //Clean-up
  164.  
  165. //try {
  166. //out.close();
  167. // } catch (IOException e) {
  168. // Nothing to do - if we can't close it, then it must be already closed (or never opened)
  169. //}
  170.  
  171. } // end finally
  172.  
  173. return result;
  174. } // end fo2Pdf
  175.  
  176.  
  177.  
  178. /*
  179. * Create and return a Transformer for the specified stylesheet.
  180. *
  181. * Based on the DOM2DOM.java example in the Xalan distribution.
  182. */
  183. private static Transformer getTransformer(String styleSheet) {
  184.  
  185. try {
  186.  
  187. TransformerFactory tFactory = TransformerFactory.newInstance();
  188.  
  189. DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
  190.  
  191. dFactory.setNamespaceAware(true);
  192.  
  193. DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
  194. Document xslDoc = dBuilder.parse(styleSheet);
  195. DOMSource xslDomSource = new DOMSource(xslDoc);
  196.  
  197. return tFactory.newTransformer(xslDomSource);
  198.  
  199. }
  200. catch (javax.xml.transform.TransformerException e) {
  201. e.printStackTrace();
  202. return null;
  203. }
  204. catch (java.io.IOException e) {
  205. e.printStackTrace();
  206. return null;
  207. }
  208. catch (javax.xml.parsers.ParserConfigurationException e) {
  209. e.printStackTrace();
  210. return null;
  211. }
  212. catch (org.xml.sax.SAXException e) {
  213. e.printStackTrace();
  214. return null;
  215. }
  216.  
  217. }
  218.  
  219. private static String documentToString(org.w3c.dom.Document doc) throws TransformerException {
  220. // Create dom source for the document
  221. DOMSource domSource=new DOMSource(doc);
  222. // Create a string writer
  223. StringWriter stringWriter=new StringWriter();
  224. // Create the result stream for the transform
  225. StreamResult result = new StreamResult(stringWriter);
  226. // Create a Transformer to serialize the document
  227. TransformerFactory tFactory =TransformerFactory.newInstance();
  228. Transformer transformer = tFactory.newTransformer();
  229. transformer.setOutputProperty("indent","yes");
  230. // Transform the document to the result stream
  231. transformer.transform(domSource, result);
  232. return stringWriter.toString();
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement