Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using FOP (Formatting Objects Processor) to create pdf from a servlet

Status
Not open for further replies.

womblies

Programmer
Feb 28, 2003
62
IE
On my Jsp page i have a call to servlet thats using Fop to create a pdf report. In the servlet it needs two input sources of xsl and xml.
My problem occurs with the xml input source, i need to have xml input source as a call to another servlet which searchs a database and dynamically creates xml format ie(out.println("<aa></aa>")).

The pdf will be produced but it will have no information on it.
 
OK, so now we have an idea of the problem but ... which is the exact question?

Cheers,
Dian
 
the problem that i can't get the report to generate properly when i dynamically generate the xml from a servlet.
as an input source for the xml i'm using:
Code:
Source src = new StreamSource("[URL unfurl="true"]http://localhost:8080/Report/servlet/ReportRee020DSB....)[/URL]
which when run will generate a blank pdf.
i know that the call to the servlet 'reportRee..' will generate the correct xml but fop can't understand it.

but if i called a file from the hard drive with sample data
Code:
Source src = new StreamSource(new File("C:/fop-0.20.5/data.xml"));
it will show data on the pdf.

my problem is that i can't use the file method.
 
Not sure about this, but maybe

Code:
Source xslSource = new StreamSource(new URL([URL unfurl="true"]http://localhost:8080/Report/servlet/ReportRee020DSB....).openStream());[/URL]

could do the trick.

You can always write to a temp file, but that's not really elegant.

Cheers,
Dian
 
no that didn't work for me.it is getting into the other servlet and going through the steps to create the xml but still it outputing a blank pdf.

would the problem be what i'm outputting on the 'ReportRee020DSB' servlet
Code:
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<rpt>");
out.println("  <booking>");
out.println("    <BKIN>"+ reportListINVOCEDSB1.getValue(new Integer(programObject.geteBKIN()).toString(),"0") +"</BKIN>");
out.println("    <BKIT><![CDATA["+ new Integer(programObject.geteBKIT()).toString() +"]]></BKIT>");
out.println("    <BKDS><![CDATA["+ utils.translateChar(utils.ReplaceChar(programObject.geteBKDS())) +"]]></BKDS>");
out.println("    <BKUN><![CDATA["+ new Integer(programObject.geteBKUN()).toString() +"]]></BKUN>");
out.println("    <BKPR><![CDATA["+ utils.roundDoubleToString(programObject.geteBKPR(),2) +"]]></BKPR>");
out.println("    <BKTO><![CDATA["+ utils.roundDoubleToString(programObject.geteBKTO(),2) +"]]></BKTO>");
out.println("  </booking>");
out.println("</rpt>");
 
I'd try writing it to a temp file to check it's fully correct.

Cheers,
Dian
 
i have tried just calling 'ReportRee020DSB' by itself from a jsp page and the xml comes out correctly
 
that is exactly what i'm calling from the StreamSource and its not returning aything

is it anything to do with println returning a void
 
Ok, let's see if I understand the problem:

1.- You call the servlet
2.- You write the response to an xml disk file that seems to be ok
3.- You generate the pdf from that xml disk file and it doesn't work.

My fisrt conclusion is that the problem is in the servlet, not the jsp.

Cheers,
Dian
 
yes i can call the servlet(Fop).
in this servlet(Fop) i call another servlet(ReportRee020DSB) to output the xml required.


Code:
 Fop Servlet
package report;
import java.io.*;
import java.util.*;
import java.net.URL;
import javax.rmi.PortableRemoteObject;
import javax.ejb.CreateException;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.XSLTInputHandler;
import org.apache.fop.messaging.MessageHandler;

import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;

//import org.w3c.dom.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

//import oracle.xml.parser.v2.*;

public class TestFop extends HttpServlet {
	private ReportUtils utils;
	protected TransformerFactory transformerFactory;
	static final String TYPE_REQUEST_PARAM = "type";
	
	Logger log = null;
	
	public TestFop(){
		log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
		this.transformerFactory = TransformerFactory.newInstance();
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException{
		try{
			String typeParam = request.getParameter(TYPE_REQUEST_PARAM);
			Driver driver = new Driver();
			driver.setLogger(this.log);
			driver.setRenderer(Driver.RENDER_PDF);
	
			//Setup a buffer to obtain the content length
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			driver.setOutputStream(out);
		
			//Setup Transformer
			Source xsltSrc = new StreamSource("[URL unfurl="true"]http://localhost:8080/Report/Jsp/test.xsl");[/URL]
			Transformer transformer = this.transformerFactory.newTransformer(xsltSrc);
		
			//Make sure the XSL transformation's result is piped through to FOP
			Result res = new SAXResult(driver.getContentHandler());
		
			//Setup input
			//Source src = new StreamSource("[URL unfurl="true"]http://localhost:8080/Report/Jsp/data.xml");[/URL]			// test xml file
			[b]Source src = new StreamSource((new URL("[URL unfurl="true"]http://localhost:8080/Report/servlet/ReportRee020DSB?target=CFrame&BKIN=1&pBKIN=1&rangeBKIN0001=1&pFLD=&pMESSAGE=&pTRASNA=null&pSIOS=1&pXMLFLAG=4")).openStream());[/URL][/b]
			transformer.transform(src, res);
		
			//Prepare response
			response.setContentType("application/pdf");
			response.setContentLength(out.size());
			
			//Send content to Browser
			response.getOutputStream().write(out.toByteArray());
			response.getOutputStream().flush();
		}catch (Exception ex) {
			throw new ServletException(ex);
		}
	}
}

to test i called the servlet((ReportRee020DSB) by itself and it will output to the screen the required xml.

but when i call 'ReportRee020DSB' as an input stream(bold text)it will generate a blank pdf

yes i agree the problem is in the servlet but where
 
I'd do this test:

1.- Call the servlet and write the response to a file
2.- Generate the pdf using that file.

Cheers,
Dian

 
i can't use the above method of writing to a tmp file to generate the pdf.

is there a way of debuging what format 'new StreamSource(new file(...))' takes in the info and duplicating it from the call to the servlet
 
>>> i can't use the above method of writing to a tmp file to generate the pdf.

Why not ? Its only going to take a bit of time to write a test case for it.



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
That's true, and I don't mean to make it work like that, just to see what's really happening and help to identify which part of the chain process is failing.

Cheers,
Dian
 
hi

when i call
Code:
Source xslSource = new StreamSource(new URL([URL unfurl="true"]http://localhost:8080/Report/servlet/ReportRee020DSB....).openStream());[/URL]
what should that servlet be returing, or type of format returned
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top