• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2946
  • Last Modified:

Getting Apache FOP to work

I'm trying to get Apache FOP to work, so to generate a PDF file from a FO file. I'm keeping it simple, using the simple.fo example that comes with the fop-installation. I have done the following:

- Mac OSX 10.4
- Tomcat 5.5.12
- fop.jar, which was in the build-directory of the fop distribution, installed in the web-inf/lib directory of the webapp.
- fop.jsp in a directory in a webapp
- simple.fo in the same directory as fop.jsp

Below is the code of fop.jsp, the code of which I found somewhere, but had to change it to JSP and probably made some mistakes with it. This is new territory for me, setting something like this up. so bear with me.

##########################

<%@ page import="org.apache.fop.apps.FopFactory"%>
<%@ page import="org.apache.fop.apps.Fop"%>
<%@ page import="org.apache.fop.apps.MimeConstants"%>

<%
private FopFactory fopFactory = FopFactory.newInstance();
private TransformerFactory tFactory = TransformerFactory.newInstance();
       
public void doGet(HttpServletRequest request,
                   HttpServletResponse response) throws ServletException {
    try {
        response.setContentType("application/pdf");
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.getOutputStream());
        Transformer transformer = tFactory.newTransformer();
        Source src = new StreamSource("simple.fo");
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(src, res);
    } catch (Exception ex) {
        throw new ServletException(ex);
    }
}

%>
###########################

I get the following error:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 5 in the jsp file: /fop/fop.jsp
Generated servlet error:
Syntax error on token "(", ; expected

An error occurred at line: 5 in the jsp file: /fop/fop.jsp
Generated servlet error:
Syntax error on token ",", ; expected

An error occurred at line: 5 in the jsp file: /fop/fop.jsp
Generated servlet error:
Syntax error, insert ";" to complete LocalVariableDeclarationStatement


      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:409)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
0
grexx
Asked:
grexx
  • 7
  • 6
1 Solution
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
that is a servllet code you can't use it in JSP's as it is.
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
http://www.coreservlets.com/

check out for tutorial on servlet and Tomcat and then write a simple servlet and add the doGet method in it as you have done in above.

http://www.java2s.com/Code/Java/Servlets/Do-Get-Do-Post.htm
http://www.java2s.com/Code/Java/Servlets/Basics.htm

here are some of the examples of servlets.

bye.
0
 
grexxAuthor Commented:
I've tried to create a servlet like in the coreservlet book. The code is:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

I've saved this into webapps/test/WEB-INF/classes/HelloWorld.java, and then tried to compile it (javac HelloWorld.java). In the terminal I get the following error:

HelloWorld.java:2: package javax.servlet does not exist
import javax.servlet.*;
^
HelloWorld.java:3: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
HelloWorld.java:4: cannot find symbol
symbol: class HttpServlet
public class HelloWorld extends HttpServlet {
                                ^
HelloWorld.java:5: cannot find symbol
symbol  : class HttpServletRequest
location: class HelloWorld
  public void doGet(HttpServletRequest request,
                    ^
HelloWorld.java:6: cannot find symbol
symbol  : class HttpServletResponse
location: class HelloWorld
                    HttpServletResponse response)
                    ^
HelloWorld.java:7: cannot find symbol
symbol  : class ServletException
location: class HelloWorld
      throws ServletException, IOException {
             ^
6 errors

What am I doing wrong here?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
you need to include Servlet.jar or instead add all the jars  which starts with servlet in  to your  project class path.

in my case servlet-api.jar exists in
C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib

Thank You.
0
 
grexxAuthor Commented:
I use jEdit as editor, just edit files, don't use projects. Is this necessary? I suppose it wouldn't hurt to start using Eclipse, but that would be an extra challenge. Is there a way around this using the terminal?
0
 
grexxAuthor Commented:
I've downloaded Eclipse, created a project, created a file HelloWorld.java, and got the same errors. Then I tried to find a servlet.jar and j2ee.jar file, found both, and added them to the Eclipse classpath (Window > preferences > java > build path > classpath). Then I saved the helloworld file again, but the errors stayed. Is resaving enough here?

Btw, I got the servlet.jar from the Eclipse plugin folder, which refers to Tomcat 4.30, which I'm not using; I got the j2ee.jar from the Windows J2EE download. I don't know if this causes a problem.

As I've told you, I'm using a Mac, OSX 10.4, Java 5.0, Tomcat 5.5.
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
in Tomact 5.5 installation folder there exists a  Apache Software Foundation\Tomcat 5.5\common\lib folder just add all the jars to the build path and refreshthe project and try running the program.
0
 
grexxAuthor Commented:
Thanks! I think I'm nearly there. The errors are gone except for a warning. Here is the code:

import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;
       
public class CreatePdf extends HttpServlet {
      
      private FopFactory fopFactory = FopFactory.newInstance();
      private TransformerFactory tFactory = TransformerFactory.newInstance();

      public void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException {
          try {
              response.setContentType("application/pdf");
              Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.getOutputStream());
              Transformer transformer = tFactory.newTransformer();
              Source src = new StreamSource("simple.fo");
              Result res = new SAXResult(fop.getDefaultHandler());
              transformer.transform(src, res);
          } catch (Exception ex) {
              throw new ServletException(ex);
          }
      }
}

I've copied the compiled CreatePdf.class to the web-inf/class folder of the webapp. I've added the following mapping to the web.xml file:

    <servlet>
        <servlet-name>CreatePdfDoc</servlet-name>
        <servlet-class>CreatePdf</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>CreatePdfDoc</servlet-name>
        <url-pattern>/CreatePdfDoc</url-pattern>
    </servlet-mapping>

Then if I try to use the servlet the first time, I get the following error. (Trying another time results in a 404 error.)

*******************************************************
description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class CreatePdf
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:514)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
      org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
      org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
      org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
      java.lang.Thread.run(Thread.java:613)

root cause

java.lang.NoClassDefFoundError: org/apache/avalon/framework/configuration/ConfigurationException
      CreatePdf.<init>(CreatePdf.java:12)
      sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      java.lang.reflect.Constructor.newInstance(Constructor.java:494)
      java.lang.Class.newInstance0(Class.java:350)
      java.lang.Class.newInstance(Class.java:303)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:514)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
      org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
      org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
      org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
      java.lang.Thread.run(Thread.java:613)
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
your need to put the FOP jars( present in lib folder when you UnZip FOP) in the project buildpath.

ok now 1st right click on your project and select ->properties-> java build path(in the left panel) -> libraries (topTab) -> Add External jar.

just point your unzipped FOP folder i when you do add external jars.

All this i am telling if you are using eclipse.

Else you needto include all those jars in the class path manually.

in the command window you need to say

set classpath=%classpath%;c:/fop/blah.jar;c:/blah1.jar....
and so on

Thank You.



0
 
grexxAuthor Commented:
It still doesn't work... same error :-(

I copied the following jars to the web-inf/lib folder of the webapp:

1) From fop-0.92beta/lib: all jars (is this necessary?)
2) From fop-0.92beta/build: fop.jar

I added the following jars to the build path:

1) All jars from (1) and (2) above
2) All jars from apache-tomcat-5.5.12/common/lib

Does it matter in what folder the CreatePdf.class file is created?
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
Apache Avalon is a seperate project so you need to t\download it and then put the jars inside it lib directory in to your build path.

http://mirrors.isc.org/pub/apache/avalon

here you can download it.


>>1) From fop-0.92beta/lib: all jars (is this necessary?)

all the classes required for FOP to work will be present in Lib folder only so its a must.

Thank You.

now you just need to downlad avalon and put its lib folders content in your class path.

0
 
grexxAuthor Commented:
Thanks! I thought reloading the webapp would do the job, but restarting the machine made the difference. The next problem was streamsource, where the file was located, but I fould out that using a hard file path like file:///path/to/simple.fo did the trick. Is it safe to use this, or is it better to use a relative path or a url? I tried both, and that didn't work, but maybe there's another way to do this?

Anyway, thanks for all the help. I got it working - my first servlet - and that's the most important thing for now.
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
Good .

All the best with FOP.
:)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now