Java Tutorial/PDF/Servlet

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Pdf and servlet

import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;

public class HelloWorldServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    String presentationtype = request.getParameter("presentationtype");
    Document document = new Document();
    try {
      if ("pdf".equals(presentationtype)) {
        response.setContentType("application/pdf");
        PdfWriter.getInstance(document, response.getOutputStream());
      } else if ("html".equals(presentationtype)) {
        response.setContentType("text/html");
        HtmlWriter.getInstance(document, response.getOutputStream());
      } else if ("rtf".equals(presentationtype)) {
        response.setContentType("text/rtf");
        RtfWriter2.getInstance(document, response.getOutputStream());
      } 
      document.open();
      document.add(new Paragraph("Hello World"));
      document.add(new Paragraph(new Date().toString()));
    } catch (DocumentException de) {
      de.printStackTrace();
      System.err.println("document: " + de.getMessage());
    }
    document.close();
  }
}





Set ContentType and transfer Pdf document to client

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class OutSimplePdf extends HttpServlet {
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    makePdf(request, response, "GET");
  }
  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    makePdf(request, response, "POST");
  }
  public void makePdf(HttpServletRequest request,
      HttpServletResponse response, String methodGetPost)
      throws ServletException, IOException {
    try {
      String msg = "your message";
      Document document = new Document();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      PdfWriter.getInstance(document, baos);
      document.open();
      document.add(new Paragraph(msg));
      document.add(Chunk.NEWLINE);
      document.add(new Paragraph("a paragraph"));
      document.close();
      response.setHeader("Expires", "0");
      response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
      response.setHeader("Pragma", "public");
      response.setContentType("application/pdf");
      response.setContentLength(baos.size());
      ServletOutputStream out = response.getOutputStream();
      baos.writeTo(out);
      out.flush();
    } catch (Exception e2) {
      System.out.println("Error in " + getClass().getName() + "\n" + e2);
    }
  }
  public void destroy() {
  }
}