Responding to myself.

I have found the problem, this code below works. I am still interested if
anyone knows better way to do this.

Thanks,

Luke


/*
 * Test class to get this FOP stuff working
 */

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.fop.apps.Driver;
import org.xml.sax.*;

/**
 * This class is intended to demonstrate submitting a FO document in memory
 * (String) to FOP and write a PDF document to the server.
 */
public class FopServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse
response)
    throws ServletException {
        try {
            //the input
            File fofile = new
File("D:\\applications\\fop\\fop-0.20.5\\test.fo");
            //location to write to
            File indexPDF = new
File("D:\\applications\\fop\\fop-0.20.5\\index.pdf");
            //create an input stream from the input file
            InputStream in = new FileInputStream(fofile);
            //FOP Driver
            Driver driver = new Driver();
            //set the render type
            driver.setRenderer(Driver.RENDER_PDF);
            //Setup output stream
            OutputStream out = new FileOutputStream(indexPDF);
            out = new BufferedOutputStream(out);
            //configure the input and the output
            driver.setInputSource(new
InputSource("D:\\applications\\fop\\fop-0.20.5\\test.fo"));
            driver.setOutputStream(out);
            //run the driver
            driver.run();
            out.close();
            PrintWriter output = response.getWriter();
            output.println("<html><head><title>Success</title></head>\n"
            + "<body><h1>Nice Work!</h1></body></html>");
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }
}
----- Original Message ----- 
From: "Luke Shannon" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, January 05, 2005 4:58 PM
Subject: Need Some Help


> Hi All;
>
> Trying to get a FOP demo off the ground. The idea is an FO document comes
in
> from memory and is rendered to a PDF on the hard disk of the server.
>
> Just to get things going I am working with a FO file on the hard drive as
> well. I will tackle the contents of this file submitted in memory next.
>
> Below is my code.
>
> When I run it in IE it get a Pop up message telling me the "File does not
> begin with '%PDF-'". This is weird to me because I am not trying to send
it
> back through the response object, I don't know why the browser is even
> looking at it.
>
> When I run it in FireFox I get my success message printed, and the
document
> created. However when I trying and open it I am told there is a Sharing
> Violation. The writer was closed before I printed the success statement so
I
> am not sure where the conflict came from.
>
> Is there a better, smarter or easier way to do this? Any help would be
> appreciated.
>
> Thanks,
>
> Luke
>
> /*
>  * Test class to get this FOP stuff working
>  */
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import org.apache.fop.apps.Driver;
> import org.xml.sax.*;
>
> /**
>  * This class is intended to demonstrate submitting a FO document in
memory
>  * (String) to FOP and write a PDF document to the server.
>  */
> public class FopServlet extends HttpServlet {
>
>     public void doGet(HttpServletRequest request, HttpServletResponse
> response)
>     throws ServletException {
>         try {
>             //the input
>             File fofile = new
> File("D:\\applications\\fop\\fop-0.20.5\\test.fo");
>             //location to write to
>             File indexPDF = new
> File("D:\\applications\\fop\\fop-0.20.5\\index.pdf");
>             //create an input stream from the input file
>             InputStream in = new FileInputStream(fofile);
>             //FOP Driver
>             Driver driver = new Driver();
>             //set the render type
>             driver.setRenderer(Driver.RENDER_PDF);
>             //Setup output stream
>             OutputStream out = new FileOutputStream(indexPDF);
>             out = new BufferedOutputStream(out);
>             //configure the input and the output
>             driver.setInputSource(new
> InputSource("D:\\applications\\fop\\fop-0.20.5\\test.fo"));
>             driver.setOutputStream(out);
>             //run the driver
>             driver.run();
>             //get the content
>             String pdfFileContent = out.toString();
>             //create the file writer
>             FileWriter writer = new FileWriter(indexPDF);
>             //get content into buffer
>             BufferedReader br = new BufferedReader(new
> StringReader(pdfFileContent));
>             //inialize a string to hold each line
>             String str = "";
>             //write the file
>             while ((str = br.readLine()) != null) {
>                 writer.write(str);
>             }
>             //close up the writer
>             writer.close();
>             //write the success message
>             PrintWriter output = response.getWriter();
>             output.println("<html><head><title>Success</title></head>\n"
>             + "<body><h1>Nice Work!</h1></body></html>");
>         } catch (Exception ex) {
>             throw new ServletException(ex);
>         }
>     }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to