Hi, I am createing a PDF file in memory using java.io.ByteArrayOutputStream. The application sends the PDF bytes to the client via the servlet's output stream.
I'm trying to write pdfcontent direct to the browser.It works fine for <document.add>, but using <PdfContentbyte> has no effect. here is the code, if anybody has come across this problem.. plz let me know.. email to : [EMAIL PROTECTED] Appreciate ur help. Thanks Kiruthika package com.pdfservlet; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; // import the iText packages import com.lowagie.text.*; import com.lowagie.text.pdf.*; import com.utils.Logger; /** * * a servlet that will generate a PDF document * and send the document to the client via the * ServletOutputStream * * @author Sean C. Sullivan * */ public class PDFServlet extends HttpServlet { /** * * */ Logger logs = null; public PDFServlet() { super(); logs = new Logger(); } /** * * * we implement doGet so that this servlet will process all * HTTP GET requests * * @param req HTTP request object * @param resp HTTP response object * */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException { DocumentException ex = null; ByteArrayOutputStream baosPDF = null; try { System.out.println("Test for the logs in Tomcat"); baosPDF = generatePDFDocumentBytes(req, this.getServletContext()); StringBuffer sbFilename = new StringBuffer(); sbFilename.append("filename_"); sbFilename.append(System.currentTimeMillis()); sbFilename.append(".pdf"); //////////////////////////////////////////////////////// // Note: // // It is important to set the HTTP response headers // before writing data to the servlet's OutputStream // //////////////////////////////////////////////////////// // // // Read the HTTP 1.1 specification for full details // about the Cache-Control header // resp.setHeader("Cache-Control", "max-age=30"); resp.setContentType("application/pdf"); // // // The Content-disposition header is explained // in RFC 2183 // // http://www.ietf.org/rfc/rfc2183.txt // // The Content-disposition value will be in one of // two forms: // // 1) inline; filename=foobar.pdf // 2) attachment; filename=foobar.pdf // // In this servlet, we use "inline" // StringBuffer sbContentDispValue = new StringBuffer(); sbContentDispValue.append("inline"); sbContentDispValue.append("; filename="); sbContentDispValue.append(sbFilename); resp.setHeader( "Content-disposition", sbContentDispValue.toString()); resp.setContentLength(baosPDF.size()); ServletOutputStream sos; /* sos = resp.getOutputStream(); System.out.println("Test for the logs in Tomcat , write outputstream"); baosPDF.writeTo(sos); sos.flush();*/ System.out.println("Test for the logs in Tomcat , write outputstream"); resp.getOutputStream().write(baosPDF.toByteArray()); resp.getOutputStream().flush(); logs.logEvent("servlet output stream " + baosPDF.toString()); } catch (DocumentException dex) { resp.setContentType("text/html"); PrintWriter writer = resp.getWriter(); writer.println( this.getClass().getName() + " caught an exception: " + dex.getClass().getName() + "<br>"); writer.println("<pre>"); dex.printStackTrace(writer); writer.println("</pre>"); } finally { if (baosPDF != null) { baosPDF.reset(); } } } protected ByteArrayOutputStream generatePDFDocumentBytes( final HttpServletRequest req, final ServletContext ctx) throws DocumentException { /*PdfReader reader = new PdfReader(fopout.toByteArray()); int n = reader.getNumberOfPages(); Document document = new Document(reader.getPageSizeWithRotation(1)); PdfWriter writer = PdfWriter.getInstance(document, outfile);*/ Document doc = new Document(); ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); PdfWriter docWriter = null; try { docWriter = PdfWriter.getInstance(doc, baosPDF); logs.logEvent("Instance created for "+ docWriter); doc.addAuthor(this.getClass().getName()); doc.addCreationDate(); doc.addProducer(); doc.addCreator(this.getClass().getName()); doc.addTitle("This is a title."); doc.addKeywords("pdf, itext, Java, open source, http"); doc.setPageSize(PageSize.LETTER); logs.logEvent("setPageSize "); //PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdfdoc.pdf")); // step 3: we open the document doc.open(); logs.logEvent("open doc "); doc.add(new Paragraph( "This document was created by a class named: " + this.getClass().getName())); doc.newPage(); logs.logEvent(" new page added to doc "); doc.add(new Paragraph( " 2.1 This document was created on " + new java.util.Date())); cb.showTextAligned(PdfContentByte.ALIGN_LEFT," pdf content byte kjsdhfkjsdhfjkshfkshfks test application", 125, 330, 0); cb.endText(); }catch (DocumentException dex) { baosPDF.reset(); throw dex; }catch(Exception ioe) { System.err.println(ioe.getMessage()); } finally { if (doc != null) { doc.close(); logs.logEvent(" doc close "); } if (docWriter != null) { docWriter.close(); logs.logEvent(" docwriter close "); } } if (baosPDF.size() < 1) { throw new DocumentException( "document has " + baosPDF.size() + " bytes"); } return baosPDF; } } ____________________________________________________________ Find what you are looking for with the Lycos Yellow Pages http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC=lycos10 ------------------------------------------------------- This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click _______________________________________________ iText-questions mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/itext-questions