Search code examples
svgitextvaadinnosuchmethoderrorbatik

Error exporting vaadin chart to pdf, using Batik and Itext


everyone.

I'm getting this error when I try to export a chart to a pdf file. I took the example from the Itext site and implemented it in my code. This is a snippet of the code where it does the exportation

else if (isGrafico(componente)) {    
                System.out.println("============= Encontrou um Grafico");
                Configuration conf = ((GraficoComp) componente)
                        .getGraficoGerado().getConfiguration();
                String svgStr = SVGGenerator.getInstance().generate(conf);
                System.out.println(svgStr);

                 /** The SVG document factory. */
                SAXSVGDocumentFactory factory;
                /** The SVG bridge context. */
                BridgeContext ctx;
                /** The GVT builder */
                GVTBuilder builder;

                String parser = XMLResourceDescriptor.getXMLParserClassName();
                factory = new SAXSVGDocumentFactory(parser);

                UserAgent userAgent = new UserAgentAdapter();
                DocumentLoader loader = new DocumentLoader(userAgent);
                // Ok =========================================================
                // \/ Error happens in this line
                ctx = new BridgeContext(userAgent, loader);
                ctx.setDynamicState(BridgeContext.DYNAMIC);

                builder = new GVTBuilder();

                // =============================================
                PdfContentByte cb = writer.getDirectContent();
                PdfTemplate chart = cb.createTemplate(900, 900);
                drawSvg(factory,builder,ctx,chart, svgStr);
                cb.addTemplate(chart, 0, 0);

And below is the error:

2014-06-17T13:46:08.623+0000|WARNING: ApplicationDispatcher[/AmbienteRelatorio] PWC1231: Servlet.service() for servlet Ambienterelatorio Application threw exception
java.lang.NoSuchMethodError: org.json.JSONObject.put(Ljava/lang/String;Ljava/util/Collection;)Lorg/json/JSONObject;
at com.vaadin.server.VaadinService.createCriticalNotificationJSON(VaadinService.java:1574)
at com.vaadin.server.VaadinService.handleExceptionDuringRequest(VaadinService.java:1424)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinPortlet.handleRequest(VaadinPortlet.java:435)
at com.vaadin.server.VaadinPortlet.serveResource(VaadinPortlet.java:538)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:118)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:71)
at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:93)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:785)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:649)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:483)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:454)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:350)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:300)
at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:634)
at com.liferay.portlet.InvokerPortletImpl.invokeResource(InvokerPortletImpl.java:746)
at com.liferay.portlet.InvokerPortletImpl.serveResource(InvokerPortletImpl.java:503)
at com.liferay.portal.action.LayoutAction.processPortletRequest(LayoutAction.java:941)
at com.liferay.portal.action.LayoutAction.processLayout(LayoutAction.java:664)
at com.liferay.portal.action.LayoutAction.execute(LayoutAction.java:244)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at com.liferay.portal.struts.PortalRequestProcessor.process(PortalRequestProcessor.java:174)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
at com.liferay.portal.servlet.MainServlet.callParentService(MainServlet.java:533)
at com.liferay.portal.servlet.MainServlet.service(MainServlet.java:510)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

Solution

  • This is not an iText question. Your stacktrace doesn't refer to iText anywhere. It refers to Liferay, Struts and Vaadin and it informs you that you about a problem with http://www.json.org/

    You are referring to an example I have written, but AFAIK I'm not using JSON in that example anywhere. Having a NoSuchMethodError can be caused by a number of things:

    1. There's a reference to a method that doesn't exist. This is a programming error. Normally your code can't be compiled if it uses a method that doesn't exist.
    2. There's a reference to a method that is present in two different jars of the same product. Because of the ambiguity, your JVM doesn't know which method you want. In this case, you need to clean up your CLASSPATH.
    3. There's a reference to a method that did exist, but that was changed. In this case you need to recompile all your code.

    Regarding case 3. Take for instance the class Foo with a method bar().

    public class Foo {
        public void bar() {}
    }
    

    You are using this class in your application.

    Foo foo = new Foo();
    foo.bar();
    

    Now suddenly the bar() method changes like this:

    public class Foo {
        public int bar() { return 0; }
    }
    

    If you replace the jar containing the Foo class, your application will throw a NoSuchMethodError because the signature of the method has changed. This can be confusing because the method is there. It's just that your application can't find it, because your application is looking for void bar() instead of int bar().