Advertisement
Guest User

FOPpdf

a guest
Oct 17th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1.  
  2. public class FOPpdf {
  3.  
  4.     public InputStream toDo(InputStream xmlStream) throws TransformerException, IOException, ConfigurationException, SAXException {
  5.    
  6.         File xsltfile = new File("HelloWorld.xsl");    
  7.         StreamSource source = new StreamSource (xmlStream);  
  8.         StreamSource transformSource = new StreamSource(xsltfile);    
  9.         FopFactory fopFactory = FopFactory.newInstance();
  10.        
  11.         DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
  12.         File file1 = new File("fonts.xsl");  
  13.         Configuration cfg = cfgBuilder.buildFromFile(file1);
  14.         fopFactory.setUserConfig(cfg);
  15.        
  16.  
  17.         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  18.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  19.         Transformer xslfoTransformer;
  20.                
  21.         try {
  22.             xslfoTransformer = getTransformer(transformSource);        
  23.             Fop fop;
  24.             try {
  25.                 fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);
  26.                
  27.                 Result res = new SAXResult(fop.getDefaultHandler());
  28.                
  29.                 try {
  30.                    
  31.                    
  32.                     xslfoTransformer.transform(source, res);
  33.                  
  34.                     return new ByteArrayInputStream(outStream.toByteArray());
  35.  
  36.                 } catch (TransformerException e) {
  37.                     throw e;
  38.                 }
  39.             } catch (FOPException e) {
  40.                 throw e;
  41.             }
  42.         } catch (TransformerConfigurationException e) {
  43.             throw e;
  44.         } catch (TransformerFactoryConfigurationError e) {
  45.             throw e;
  46.         }
  47.     }
  48.  
  49.     private Transformer getTransformer(StreamSource streamSource) {    
  50.         TransformerFactoryImpl impl = new TransformerFactoryImpl();
  51.         try {      
  52.             return impl.newTransformer(streamSource);
  53.         } catch (TransformerConfigurationException e) {
  54.            throw new RuntimeException(e);
  55.         }
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement