IBM webMethods Hybrid Integration

IBM webMethods Hybrid Integration

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.


#TechXchangePresenter
 View Only
Expand all | Collapse all

Converting xml to pdf with fop 1.1

webMethods Community Member

webMethods Community MemberMon March 24, 2014 11:33 AM

webMethods Community Member

webMethods Community MemberFri August 08, 2014 04:42 PM

webMethods Community Member

webMethods Community MemberTue August 12, 2014 12:38 AM

webMethods Community Member

webMethods Community MemberTue October 31, 2017 04:19 PM

  • 1.  Converting xml to pdf with fop 1.1

    Posted Tue March 11, 2014 07:11 AM

    Hi everybody

    I’m trying to use fop 1.1 library to covert a xml file to pdf with the fop library on webMethods 8.0.
    The code I am using is quite basic, it’s actually the sample code from the fop home page except I have the xml data in a string rather than reading it from a file. I put all the jars file in the package/code/jars folder.

    I keep getting the the following error:

    The code is as follows:

    IDataCursor idc = pipeline.getCursor();
    String xmlString = IDataUtil.getString( idc, "xmlData" );
    String xsltString = IDataUtil.getString ( idc, "xsltData" );
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FopFactory fopFactory = FopFactory.newInstance();
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Source xml = new StreamSource( new StringReader( xmlString ) );
    Source xslt = new StreamSource( new StringReader( xsltString ) );
    try{
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    foUserAgent.getRendererOptions();
    Fop fop = fopFactory.newFop( MimeConstants.MIME_PDF, foUserAgent, out );
    Transformer transformer = tFactory.newTransformer( xslt );
    Result res = new SAXResult( fop.getDefaultHandler() );
    transformer.transform( xml, res );
    }
    catch( FOPException e ){
    IDataUtil.put( idc, "errorMessage", e.getMessage() );
    }
    finally{
    idc.destroy();
    }

    Does anybody have any experience with using the fop 1.1 library on a webMethods server and could perhaps let me know where my problem lies.

    thanks


    #webMethods
    #Integration-Server-and-ESB


  • 2.  RE: Converting xml to pdf with fop 1.1

    Posted Tue March 11, 2014 09:17 AM

    Can you provide the value of xmlData and xsltData, so I can try it in my environment.


    #webMethods
    #Integration-Server-and-ESB


  • 3.  RE: Converting xml to pdf with fop 1.1

    Posted Tue March 11, 2014 09:41 AM

    Here is the xml:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <root>
    <name>shyam</name>
    <friend>
    <name>Abc</name>
    <phNo>90909090909</phNo>
    </friend>
    <friend>
    <name>Xyz</name>
    <phNo>32323232323</phNo>
    </friend>
    </root>

    and this is the xslt:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.1"
    xmlns:xsl=http://www.w3.org/1999/XSL/Transform 
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="fo">
    <xsl:template match="root">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
    <fo:simple-page-master master-name="my-page">
    <fo:region-body margin="1in"/>
    </fo:simple-page-master>
    </fo:layout-master-set>
    
    <fo:page-sequence master-reference="my-page">
    <fo:flow flow-name="xsl-region-body">
    <fo:block>Hello, <xsl:value-of select="name" />!</fo:block>
    <fo:block>
    <fo:table>
    <fo:table-body>
    <fo:table-row>
    <fo:table-cell border="solid 1px black" 
    text-align="center" font-weight="bold">
    <fo:block>
    No.
    </fo:block>
    </fo:table-cell>
    <fo:table-cell border="solid 1px black" 
    text-align="center" font-weight="bold">
    <fo:block>
    Name
    </fo:block>
    </fo:table-cell>
    <fo:table-cell border="solid 1px black" 
    text-align="center" font-weight="bold">
    <fo:block>
    Phone Number
    </fo:block>
    </fo:table-cell>
    </fo:table-row>
    <xsl:for-each select="./friend">
    <fo:table-row>
    <fo:table-cell border="solid 1px black" text-align="center">
    <fo:block>
    <xsl:value-of select="position()" />
    </fo:block>
    </fo:table-cell>
    <fo:table-cell border="solid 1px black" text-align="center">
    <fo:block>
    <xsl:value-of select="name" />
    </fo:block>
    </fo:table-cell>
    <fo:table-cell border="solid 1px black" text-align="center">
    <fo:block>
    <xsl:value-of select="phNo" />
    </fo:block>
    </fo:table-cell>
    </fo:table-row>
    </xsl:for-each>
    </fo:table-body>
    </fo:table>
    </fo:block>
    </fo:flow>
    </fo:page-sequence>
    </fo:root>
    </xsl:template>
    </xsl:stylesheet>

    This is just some test data I found online, it doesn’t matter however since I can’t get past the

    Fop fop = fopFactory.newFop( MimeConstants.MIME_PDF, foUserAgent, out ); 

    line. This line causes the previously mentioned error.


    #Integration-Server-and-ESB
    #webMethods


  • 4.  RE: Converting xml to pdf with fop 1.1

    Posted Wed March 19, 2014 05:25 PM

    Hi Brynjolfur.

    I got the same error.

    How did you solved it?

    Thanks


    #webMethods
    #Integration-Server-and-ESB


  • 5.  RE: Converting xml to pdf with fop 1.1

    Posted Thu March 20, 2014 06:11 AM

    I actually haven’t been able to solve this problem yet, I’m still getting the same error. I’m wondering if there is a clash between libraries in server but haven’t been able to verify that yet. There is a older package on the server which uses a older version of Fop, however when I tried using the Fop 1.1 on a newly set up Wm 9.5 server I got the same error so if there is a clash of libraries its between something built into the IS.

    I managed to get the transformer to run and produce a pdf file by adding the following code:

    PDFRendererConfigurator pdfConfig = new PDFRendererConfigurator( foUserAgent );
    PDFDocumentHandlerMaker pdfMaker = new PDFDocumentHandlerMaker();
    IFDocumentHandler pdfHandler = pdfMaker.makeIFDocumentHandler( foUserAgent );
    pdfConfig.configure( pdfHandler ); 
    foUserAgent.setDocumentHandlerOverride( pdfHandler );

    but the pdf file is corrupted so this solution doesn’t really work so now I am basically stuck.


    #webMethods
    #Integration-Server-and-ESB


  • 6.  RE: Converting xml to pdf with fop 1.1

    Posted Mon March 24, 2014 08:15 AM

    This issue has now been resolved by adding the libraries to the Java ClassPath on the server, that is putting them into the
    [b][install directory]\IntegrationServer\lib\jars[/b]
    folder and restarting the server.


    #webMethods
    #Integration-Server-and-ESB


  • 7.  RE: Converting xml to pdf with fop 1.1

    Posted Mon March 24, 2014 10:20 AM

    Hi Brynjolfur

    Thanks for your quick update.

    I’ve just tested and works fine.

    Thank you very much.


    #Integration-Server-and-ESB
    #webMethods


  • 8.  RE: Converting xml to pdf with fop 1.1

    Posted Mon March 24, 2014 11:33 AM

    Glad to hear issue resolved :smiley:


    #Integration-Server-and-ESB
    #webMethods


  • 9.  RE: Converting xml to pdf with fop 1.1

    Posted Thu August 07, 2014 01:46 AM

    Hi All,

    I am trying to convert XML String to PDF using fop library.
    fop version: 1.0
    wM version 8.2

    i have placed the fop-sources.jar in IntegrationServer/lib/jars folder and restarted the server.

    but i am not able to compile the code, getting error like.
    flow.java:48: cannot find symbol
    symbol : class FopFactory

    can you please tell me what are the libraries you had imported while developing the code.
    i have added java.util.* , java.io.*

    Thanks,
    Shiva


    #webMethods
    #Integration-Server-and-ESB


  • 10.  RE: Converting xml to pdf with fop 1.1

    Posted Thu August 07, 2014 02:53 PM

    try to put the fop.jar, not fop-sources.jar to that dir.


    #Integration-Server-and-ESB
    #webMethods


  • 11.  RE: Converting xml to pdf with fop 1.1

    Posted Fri August 08, 2014 04:51 AM

    Thanks Wang :smiley:
    it worked!!!

    i was able to write the file into Integration server directory.

    but if try to send this file as a mail attachment ,the resulting PDF file seems to be corrupted.
    i am passing PDF content as bytes to attachments/content and setting the ContentType =application/pdf.

    anything else to be done?

    Thanks,
    Shiva


    #webMethods
    #Integration-Server-and-ESB


  • 12.  RE: Converting xml to pdf with fop 1.1

    Posted Fri August 08, 2014 04:42 PM

    try to set: encoding=base64


    #webMethods
    #Integration-Server-and-ESB


  • 13.  RE: Converting xml to pdf with fop 1.1

    Posted Tue August 12, 2014 12:38 AM


  • 14.  RE: Converting xml to pdf with fop 1.1

    Posted Tue October 31, 2017 03:56 PM

    Guys, I am using fop 2.2. I used following code (very much the same as what Brynjolfur shared above, but I get NullPointerException from the last line "transformer.transform( xml, res ). The xml and res objects are not null. What could be causing this error?

    
    import com.wm.app.b2b.server.ServiceException;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.PrintWriter;
    import java.io.StringReader;
    import java.io.StringWriter;
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.sax.SAXResult;
    import javax.xml.transform.stream.StreamSource;
    import org.apache.fop.apps.FOPException;
    import org.apache.fop.apps.FOUserAgent;
    import org.apache.fop.apps.Fop;
    import org.apache.fop.apps.FopFactory;
    import org.apache.fop.apps.MimeConstants;
    import org.apache.fop.render.intermediate.IFContext;
    import org.apache.fop.render.intermediate.IFDocumentHandler;
    import org.apache.fop.render.pdf.PDFDocumentHandlerMaker;
    import org.apache.fop.render.pdf.PDFRendererConfig;
    import org.apache.fop.render.pdf.PDFRendererConfigurator;
    
    IDataCursor idc = pipeline.getCursor();  
    short linenum = 0;
    String xmlString = IDataUtil.getString( idc, "xmlData" );  
    String xsltString = IDataUtil.getString ( idc, "xsltData" );  
    String errMsg = "";
    
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    
    FopFactory fopFactory = null;
    TransformerFactory tFactory = null;  
    Source xml = null;  
    Source xslt = null;  
    
    try{  
    fopFactory = FopFactory.newInstance(new File("./fop-2.2/conf/fop.xconf"));  
    linenum++; // 1
    tFactory = TransformerFactory.newInstance();  
    linenum++; // 2
    xml = new StreamSource( new StringReader( xmlString ) );  
    linenum++; // 3
    xslt = new StreamSource( new StringReader( xsltString ) ); 
    linenum++; // 4
    
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();  
    linenum++; // 5
    PDFRendererConfigurator pdfConfig = new PDFRendererConfigurator( foUserAgent, new PDFRendererConfig.PDFRendererConfigParser() );  
    linenum++; // 6
    PDFDocumentHandlerMaker pdfMaker = new PDFDocumentHandlerMaker();  
    linenum++; // 7 
    IFDocumentHandler pdfHandler = pdfMaker.makeIFDocumentHandler( new IFContext(foUserAgent) );  		    
    linenum++; // 8
    pdfConfig.configure( pdfHandler );
    linenum++; // 9
    foUserAgent.setDocumentHandlerOverride( pdfHandler );
    
    linenum++; // 10
    foUserAgent.getRendererOptions();  
    linenum++; // 11
    Fop fop = fopFactory.newFop( MimeConstants.MIME_PDF, foUserAgent, out );  
    linenum++; // 12
    Transformer transformer = tFactory.newTransformer( xslt );  
    linenum++; // 13
    Result res = new SAXResult( fop.getDefaultHandler() ); 						
    linenum++; // 14
    transformer.transform( xml, res );
    }  
    catch( Exception e ){
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw));
    errMsg = errMsg + "Error at linenum " + linenum + ": " + sw.toString();
    IDataUtil.put( idc, "errorMessage", errMsg );  
    throw new ServiceException (errMsg);
    }  
    finally{  
    
    if(out!=null) try {out.close();}catch(Exception e) {};
    idc.destroy();  
    }  

    Well, I ended up using the sample from https://netjs.blogspot.com.tr/2015/07/how-to-create-pdf-from-xml-using-apache-fop.html. It worked.


    #Integration-Server-and-ESB
    #webMethods


  • 15.  RE: Converting xml to pdf with fop 1.1

    Posted Tue October 31, 2017 04:19 PM

    can you provide the full error message


    #webMethods
    #Integration-Server-and-ESB