Re: can not run a servlet that generates a pdf

2004-11-02 Thread Jon Wingfield
You are calling getWriter in your catch clause after you have already 
called getOutputStream. That's illegal.
You could set up an error page in web.xml to report the exception 
instead of catching it.

Jon
Lina María Rojas Barahona wrote:
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) 
");
  out.write(AtStringUtil.getStackTrace(t));
  out.write("");
  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, At

can not run a servlet that generates a pdf

2004-11-02 Thread Lina María Rojas Barahona

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) 
");
  out.write(AtStringUtil.getStackTrace(t));
  out.write("");
  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);
b