An easier way of doing this is by using a cheesy javascript trick. When you
want to go to create your pdf link to an
HTML or JSP page instead which states "Please wait" (maybe an animated gif
would be nice). Put an onload task in the body tag that does a
document.location() change to your PDF creating servlet.

Since your "Please wait" page will stay up until the PDF returns you get
what you are looking for without polling the server.

- Jim 

-----Original Message-----
From: Kenny G. Dubuisson, Jr. [mailto:[EMAIL PROTECTED]
Sent: Wednesday, June 25, 2003 12:11 PM
To: [EMAIL PROTECTED]
Subject: [iText-questions] Example of HTML "please wait" screen before
PDF generation


Hello all.  I've been working on the capability of a "please wait" screen
that will display while my PDF is being generated.  I got some code from
Bruno and I've been trying to make it work to no avail.  What the code is
supposed to do is write to the output stream in HTML format the "please
wait" screen and refresh every 3 seconds.  If when it refreshes the document
is done, change the output stream to PDF and send the document.  I have it
to the point that it seems to switch to PDF but never displays the document
(which at this point is just a page with the word "test" on it).

The code separated into two files: MyPdfGenerator and PleaseWait Servlet.
I've included the two pieces of code below.  Any ideas/comments would be
very much appreciated.  Once I get this working I'll post to the list for
future reference.  Thanks,
Kenny

// MyPdfGeneration.java
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class MyPdfGenerator implements Runnable
{
   public static final int NOT_STARTED = 0;
   public static final int BUSY = 1;
   public static final int FINISHED = 2;
   public static final int ABORTED = 3;

   private int status = NOT_STARTED;
   private int counter = 0;
   private static final int TOTAL = 1000;
   private ByteArrayOutputStream baos = new ByteArrayOutputStream();

   public void run()
   {
      try
      {
         status = BUSY;
         Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);
         PdfWriter writer = PdfWriter.getInstance(document, baos);
         document.open();
         PdfContentByte cb = writer.getDirectContent();

         document.add(new Paragraph("test"));

         document.close();
         status = FINISHED;
      }
      catch (DocumentException de)
      {
         status = ABORTED;
      }
   }

   public boolean isBusy()
   {
      return status == BUSY;
   }

   public boolean isFinished()
   {
      return status == FINISHED;
   }

   public boolean isAborted()
   {
      return status == ABORTED;
   }

   public int getStatus()
   {
      return status;
   }

   public int size()
   {
      return baos.size();
   }

   public void writePdf(OutputStream out) throws IOException
   {
      baos.writeTo(out);
      out.flush();
      baos.close();
   }
}

// PleaseWaitServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PleaseWaitServlet extends HttpServlet
{
   protected void service(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException
   {
      HttpSession session = request.getSession(true);
      MyPdfGenerator generator = (MyPdfGenerator)
session.getAttribute("PDF");
      if (generator == null)
      {
         response.setHeader("Refresh", "3");
         generator = new MyPdfGenerator();
         Thread t = new Thread(generator);
         t.start();
         session.setAttribute("PDF", generator);
         doHtmlOutput(response, "document generation was initialized..." +
            "please wait");
         return;
      }
      if (generator.isBusy())
      {
         response.setHeader("Refresh", "3");
         doHtmlOutput(response, "document generation is busy...");
         return;
      }
      if (generator.isFinished())
      {
         doPdfOutput(response, generator);
         session.removeAttribute("PDF");
         return;
      }
      if (generator.isAborted())
      {
         doHtmlOutput(response, "document generation was aborted");
         session.removeAttribute("PDF");
         return;
      }
   }

   protected void doHtmlOutput(HttpServletResponse response, String message)
      throws IOException
   {
      response.setContentType("Text/html");
      PrintWriter out = response.getWriter();
      out.println("<html><body>");
      out.println(message);
      out.println("</body></html>");
      out.flush();
      out.close();
   }

   protected void doPdfOutput(HttpServletResponse response, MyPdfGenerator
      generator) throws IOException
   {
      response.setContentType("application/pdf");
      response.setContentLength(generator.size());
      response.setBufferSize(generator.size());
      ServletOutputStream out = response.getOutputStream();
      generator.writePdf(out);
   }
}



-------------------------------------------------------
This SF.Net email is sponsored by: INetU
Attention Web Developers & Consultants: Become An INetU Hosting Partner.
Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission!
INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

------------------------------------------------------------------------------
This e-mail transmission may contain information that is proprietary, privileged 
and/or confidential and is intended exclusively for the person(s) to whom it is 
addressed. Any use, copying, retention or disclosure by any person other than the 
intended recipient or the intended recipient's designees is strictly prohibited. If 
you are not the intended recipient or their designee, please notify the sender 
immediately by return e-mail and delete all copies. 


==============================================================================



-------------------------------------------------------
This SF.Net email sponsored by: Parasoft
Error proof Web apps, automate testing & more.
Download & eval WebKing and get a free book.
www.parasoft.com/bulletproofapps1
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to