Hi
I am trying to get a servlet to produce pdf from data from a database. When
the servlet receives a request, it runs FOP (fop-0.20.5rc2), and outputs
this to the log:
[INFO] building formatting object tree
[INFO] setting up fonts
[INFO] [1]
[DEBUG] Last page-sequence produced 1 pages.
[INFO] Parsing of document complete, stopping renderer
[DEBUG] Initial heap size: 27343Kb
[DEBUG] Current heap size: 7130Kb
[DEBUG] Total memory used: -20213Kb
[DEBUG] Memory use is indicative; no GC was performed
[DEBUG] These figures should not be used comparatively
[DEBUG] Total time used: 2209ms
[DEBUG] Pages rendered: 1
[DEBUG] Avg render time: 2209ms/page
The browser starts Adobe Reader. But then it shows a blank page.
Here is the class that produces the pdf. It is a slight modification
of ExampleObj2PDF.java, included in fop-0.20.5rc2 :
package dk.q8.ssintra.priscirku;
import org.apache.fop.apps.Driver;
import org.apache.fop.messaging.MessageHandler;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.xml.sax.SAXException;
import javax.ejb.CreateException;
import javax.naming.NamingException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXResult;
import java.io.IOException;
import java.io.OutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.SQLException;
/**
* Created by IntelliJ IDEA.
* User: klkr
* Date: 31-03-2003
* Time: 15:40:28
* Genererer PDF.
*/
public class CirkuPrinter {
/**
* Constructor.
* @param xls Sti til filen hvor stylesheetet findes
* @throws ParserConfigurationException Fra JAXP
* @throws IOException Fra JAXP
* @throws SAXException Fra JAXP
*/
public CirkuPrinter(String xls) throws ParserConfigurationException,
IOException, SAXException {
styleSheet = StyleSheetHandler.getStyleSheet(xls);
}
private Source styleSheet;
/**
* Udskriver PDF til out.
* @param out Stream hvor cirkul�ret udskrives
* @throws IOException Ved fejl
* @throws CreateException Fra EJB
* @throws SQLException Fra EJB
* @throws NamingException Fra EJB
* @throws TransformerException Fra JAXP
*/
public void print(OutputStream out) throws IOException, CreateException,
SQLException, NamingException, TransformerException {
// Skaf data
CiData data = DBConnection.findData();
Source xml = CiData2XML.createSAXSource(data);
Driver driver = new Driver();
//Setup logger
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_DEBUG);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);
// Vi vil have PDF
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
//Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(styleSheet);
//Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(driver.getContentHandler());
//Start XSLT transformation and FOP processing
transformer.transform(xml, res);
out.flush();
}
/**
* Til afpr�vning.
* @param args 0: stylesheet 1: outputfil
* @throws Exception N�r noget g�r galt.
*/
public static void main(String[] args) throws Exception {
CirkuPrinter p = new CirkuPrinter(args[0]);
File fil = new File(args[1]);
OutputStream out = new FileOutputStream(fil);
p.print(out);
}
}
When I run it from the command line, I get the expected pdf. If
I write plain text directly to the out parameter when run in the
servlet, the text appears in the browser. What am I doing wrong?
Klaus Kristiansen
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]