/ Forside / Teknologi / Udvikling / Java / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
Java
#NavnPoint
molokyle 3688
Klaudi 855
strarup 740
Forvirret 660
gøgeungen 500
Teil 373
Stouenberg 360
vnc 360
pmbruun 341
10  mccracken 320
xml til pdf servlet
Fra : Claus Reimer


Dato : 06-01-03 15:57

Hej
Er der en der kan se hvorfor denne her ikke virker i explore men fungere
fint i netscape.
når den kører i en explorer, kommer der ingenting op og efter en 2 minuters
tid kommer den med en io fejl.

Nu har jeg brugt en uges tid på det, og er fuldstændig løbet tør for ide'er
så det ville virkeligt værer fedt hvis der er en der har løsningen.

Med Venlig Hilsen
Claus

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.List;
import java.net.*;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;

import org.xml.sax.InputSource;

//import org.dom4j.Node;
import org.w3c.dom.Node;
import org.dom4j.Element;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentFactory;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXContentHandler;


// Apache.org FOP stuff
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
import org.apache.fop.messaging.MessageHandler;

// Apache.org Avalon logger stuff needed by FOP
import org.apache.avalon.framework.*;
import org.apache.avalon.framework.logger.*;


public class PDFServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse
response) throws IOException, ServletException {
try {
ServletContext context = getServletContext();

response.setContentType("application/pdf");

// Receive the document and get the style-sheet url
System.out.println("Receive the document and get the style-sheet url");
DocumentFactory factory = DocumentFactory.getInstance();
Document document = factory.createDocument();
SAXReader saxReader = new SAXReader();
document = saxReader.read(request.getInputStream());
URL styleSheetUrl = new
URL(document.getRootElement().attribute("style-sheet").getValue());


// Get the style sheet
System.out.println("Get the style sheet");
java.io.ByteArrayOutputStream baos = new
java.io.ByteArrayOutputStream(2048);

SAXReader parser = new SAXReader(false);
Document styleSheetDoc = parser.read(styleSheetUrl);

// Put base url into style sheet
System.out.println("Put base url into style sheet");
String webappRoot = context.getRealPath("/").replace('\\','/');
Document tempdoc = DocumentHelper.createDocument();
Element baseURI = tempdoc.addElement("xsl:variable").addAttribute("name",
"base").addText("file://" + webappRoot + "WEB-INF/");

Element root = styleSheetDoc.getRootElement();
List list = root.content();
list.add(0, baseURI);

// write the style sheet into baos
System.out.println("write the style sheet into baos");
OutputFormat outformat = OutputFormat.createPrettyPrint();
outformat.setEncoding("UTF-8");

XMLWriter writer = new XMLWriter(baos, outformat);
writer.write(styleSheetDoc);
writer.flush();
writer.close();


// 2. create a transformer object and load it with the
stylesheet.
System.out.println("create a transformer object and load it
with the stylesheet.");
java.io.StringReader sr = new java.io.StringReader(new
String(baos.toByteArray()));
TransformerFactory tfactory =
TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer( new
StreamSource(sr));
baos.flush();
baos.close();

// write the document to an outputstream, so transformer can read it
System.out.println("write the document to an outputstream, so transformer
can read it");
DocumentStreamer docStreamer = new DocumentStreamer(document);

// transform the document
System.out.println("transform the document");
StringWriter foStream = new java.io.StringWriter();
transformer.transform(new
StreamSource(docStreamer.getInputStream()),new StreamResult(foStream));
docStreamer.flush();
docStreamer.close();

// redender it
System.out.println("redender it ");
Reader reader = new
java.io.StringReader(foStream.toString());
ByteArrayOutputStream out = new
java.io.ByteArrayOutputStream();

// 3. FOP driver setup
System.out.println("FOP driver setup");
Driver driver = new org.apache.fop.apps.Driver(new
org.xml.sax.InputSource(reader), out);
driver.setupDefaultMappings();
driver.setRenderer(Driver.RENDER_PDF);
//driver.setLogger(logger);
driver.run();

// and stream it
System.out.println("and stream it");
response.getOutputStream().write (out.toByteArray());
response.getOutputStream().write(new byte[8192]);
response.getOutputStream().flush();

}
catch (Exception e) {
System.out.println("bad thing!" + e);
}
}
}




class DocumentStreamer {

Document document;
PipedInputStream pipedInputStream;
PipedOutputStream pipedOutputStream;

DocumentStreamer(Document document) throws IOException {
pipedInputStream = new PipedInputStream();
pipedOutputStream = new PipedOutputStream(pipedInputStream);

DocumentWriter docWriter = new DocumentWriter(document,
pipedOutputStream);
}

public InputStream getInputStream() {
return pipedInputStream;
}

public OutputStream getOutputStream() {
return pipedOutputStream;
}

public void flush() throws IOException {
pipedOutputStream.flush();
}

public void close() throws IOException {
pipedInputStream.close();
pipedOutputStream.close();
}
}

class DocumentWriter extends Thread {
private Document document;
private PipedOutputStream pipedOutputStream;

DocumentWriter(Document document, PipedOutputStream pipedOutputStream) {
this.document = document;
this.pipedOutputStream = pipedOutputStream;

start();
}

public void run() {
try {
XMLWriter writer = new XMLWriter(pipedOutputStream);
writer.write(document);
writer.close();
}
catch (Exception e) {
}
}
}



 
 
Morten (07-01-2003)
Kommentar
Fra : Morten


Dato : 07-01-03 15:59

Når din servlet at levere sit output før balladen opstår?
Hvor langt når den?

Hvor lang tid tager det med netscape? Kan det være IE der timer
ud, og derfor lukker forbindelsen (hvilket giver en IOException)?
Må vi se de første par linier af din exception?

Prøv evt. at sende Content-Type og Content-Disposition headers.

response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline;filename=\"boo.pdf\"");

Morten

Søg
Reklame
Statistik
Spørgsmål : 177558
Tips : 31968
Nyheder : 719565
Indlæg : 6408905
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste