Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FOPpdf {
- public InputStream toDo(InputStream xmlStream) throws TransformerException, IOException, ConfigurationException, SAXException {
- File xsltfile = new File("HelloWorld.xsl");
- StreamSource source = new StreamSource (xmlStream);
- StreamSource transformSource = new StreamSource(xsltfile);
- FopFactory fopFactory = FopFactory.newInstance();
- DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
- File file1 = new File("fonts.xsl");
- Configuration cfg = cfgBuilder.buildFromFile(file1);
- fopFactory.setUserConfig(cfg);
- FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- Transformer xslfoTransformer;
- try {
- xslfoTransformer = getTransformer(transformSource);
- Fop fop;
- try {
- fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);
- Result res = new SAXResult(fop.getDefaultHandler());
- try {
- xslfoTransformer.transform(source, res);
- return new ByteArrayInputStream(outStream.toByteArray());
- } catch (TransformerException e) {
- throw e;
- }
- } catch (FOPException e) {
- throw e;
- }
- } catch (TransformerConfigurationException e) {
- throw e;
- } catch (TransformerFactoryConfigurationError e) {
- throw e;
- }
- }
- private Transformer getTransformer(StreamSource streamSource) {
- TransformerFactoryImpl impl = new TransformerFactoryImpl();
- try {
- return impl.newTransformer(streamSource);
- } catch (TransformerConfigurationException e) {
- throw new RuntimeException(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement