tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lina María Rojas Barahona <lmro...@cnb.uam.es>
Subject can not run a servlet that generates a pdf
Date Tue, 02 Nov 2004 16:37:41 GMT

Hello:

I am trying to write a PDF using a servlet in Apache Tomcat/5.0.27 , and It doesn´t work.
 I just call the servlet in an html page, I never use a response.getWriter, so I dont undestand
why I can not use response.getOutputStream in the servlet, 

---- SmartJPrint_PDFServlet1---

package demo.activetree.print.webapps;

import com.activetree.print.*;
import com.activetree.utils.AtStringUtil;
import com.activetree.resource.AtImageList;

import java.io.*;
import java.awt.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;

public class SmartJPrint_PDFServlet1 extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
                                        throws IOException, ServletException {
   //TODO-1: Use your own license key here
    SmartJPrint.setLicenseKey("8FE6-D08A-68A5-191A");

   //Set header first
    response.setHeader("Cache-Control", "max-age=30");
    response.setContentType("application/pdf");
   //TODO-3: Use "inline" for directly opening in the browser window.
   //String inline = "inline; filename=\"smartjprint_" + System.currentTimeMillis() + ".pdf\"";
    //response.setHeader("Content-disposition", inline);
   //TODO-2: Use "attachment" for doing a SaveAs... in the browser.
   String attachment = "attachment; filename=\"smartjprint_" + System.currentTimeMillis()
+ ".pdf\"";
   response.setHeader("Content-disposition", attachment);

   //Prepare the data.
   AtBook book = new AtBook();
    Color fgColor = SystemColor.black;
    Color bgColor = SystemColor.white;
    //Produce lines of Styled/Plain text, icons, 2D line data.
    produceRandomData(book);
    //Table-1.
    TableModel tabularData = createSampleTabularData();
    Font titleFont = new Font("Arial", Font.BOLD, 20);
    book.append(new AtStringElement("\nPermanent Employees\n", titleFont,
                                    AtPrintConstants.GENERAL_HEADER_FG1));
    Font normalBoldFont = new Font("Arial", Font.BOLD, 12);
    book.append(new AtStringElement("Table-1\n", normalBoldFont));
    Font font = new Font("Arial", Font.PLAIN, 12);
    book.append(tabularData, font, fgColor, bgColor);
    //Page Break - to put things next in a new page.
   book.append(new AtStringElement("\nPage Break (applied next to this line)\n", titleFont,
                                    AtPrintConstants.GENERAL_HEADER_FG1));
   book.append(AtElement.PAGE_BREAK); //show in next page.
    //Table-2.
    tabularData = createSampleTabularData();
    book.append(new AtStringElement("\nContract Employees\n", titleFont,
                                    AtPrintConstants.GENERAL_HEADER_FG1));
    book.append(new AtStringElement("Table-2\n", normalBoldFont));
    book.append(tabularData, font, fgColor, bgColor);

    //We are done with our data, now write to the client.
    try {
     ServletOutputStream outStream = response.getOutputStream();
     AtPdfPrinter pdfPrinter = new AtPdfPrinter();
      pdfPrinter.print(book, outStream);
      outStream.flush();
      outStream.close();
    }catch(Throwable t) {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.write("Got exception when doing a AtGenericPrinter.print(book, pdfStream) <p>");
      out.write(AtStringUtil.getStackTrace(t));
      out.write("</p>");
      out.close();
    }
  }

  public TableModel createSampleTabularData() {
    String[] cols = {"Employee Name", "SS No.", "Date Of Birth",
                     "Position", "Salary"};
    String[][] data = new String[5][cols.length];

    //Create same data for the table.
    String name = "Employee Name - ";
    String ssn = "SS No - ";
    String dob = "Birth Date - ";
    String position = "Position - ";
    String annualSalary = "Employee Salary - ";
    for (int row=0; row < data.length; row++) {
      data[row][0] = name + row;
      data[row][1] = ssn + row;
      data[row][2] = dob + row;
      data[row][3] = position + row;
      data[row][4] = annualSalary + row;
    }

    DefaultTableModel tableModel = new DefaultTableModel(data, cols);
    return tableModel;
  }

  public void produceRandomData(AtBook book) {
    Font titleFont = new Font("Arial", Font.BOLD, 20);
    Color titleColor = AtPrintConstants.GENERAL_HEADER_FG1;
    ImageIcon bullet = AtImageList.IMAGE_LIST.RIGHT_ARROW_16;

    AtStringElement note = new AtStringElement("Note: ", new Font("Helvitica", 3, 12)); //3
== BOLD + ITALIC
    book.write(note);
    book.write(new AtStringElement("This PDF document is generated using Smart JPrint (AtGenericPrinter
class).\n", new Font("Helvetica", Font.ITALIC, 12)));
    book.write(new AtLineElement(200, 1, AtLineElement.TOP, true));

    AtStringElement elm = new AtStringElement("Generate PDF Documents using \"Smart JPrint\"\n\n",
titleFont);
    elm.setForeground(titleColor);
    book.write(elm);
    book.write(bullet);
    book.write("Smart JPrint is 100% pure Java APIs for (i) PDF generation, " +
               "(ii) Automatically or Manually Printing, (iii) Multipage TIFF for FAX sending/viewing,
" +
               "(iv) JPEG/PNG image creation for each output pages.\n\n");
    elm = new AtStringElement("Add Plain/Styled text, icons/images, 2D graphics, tabular data
using " +
                              "Java's TableModel to automatically format and produce the output.");
    elm.setFont(new Font("Helvetica", Font.PLAIN, 12));
    book.write(elm);
    book.write("\n\n");
    book.write(bullet);
    elm = new AtStringElement("Produce output from Java Swing components in a matter of  "
+
                              "minute with 2 lines of code. Smart JPrint knows how to format
" +
                              "the contents in order to generate the output automatically.");
    elm.setFont(new Font("Helvetica", Font.PLAIN, 12));
    book.write(elm);
    book.write("\n\n");

  book.write(new AtStringElement("How to page break, TAB, Strike through?", titleFont, titleColor));
  book.write("\nA page break can be added using the AtPageBreak object or by " +
             "using the PAGE_BREAK constant defined in the AtElement class.\n");
    elm = new AtStringElement("\nThis line is an example of Strike through text.\n");
    elm.setStrikeThrough(true);
    elm.setStrikeThroughColor(SystemColor.darkGray);
    book.append(elm);
    elm = new AtStringElement("This line has a \tTAB character\n");
    book.append(elm);
  }
}
-----------------------------------------------------
ERROR

2004-11-02 14:54:18 StandardWrapperValve[PDFServlet1]: Servlet.service() para servlet PDFServlet1
lanzó excepción
java.lang.IllegalStateException: getOutputStream() ya ha sido llamado para esta respuesta
 at org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:599)
 at org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacade.java:163)
 at demo.activetree.print.webapps.SmartJPrint_PDFServlet1.doGet(SmartJPrint_PDFServlet1.java:67)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
 at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
 at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
 at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
 at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
 at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
 at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
 at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
 at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
 at java.lang.Thread.run(Unknown Source)


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message