/[projects]/miscJava/Test3/src/PdfServlet.java
ViewVC logotype

Contents of /miscJava/Test3/src/PdfServlet.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 616 - (show annotations) (download)
Wed Feb 24 16:23:21 2010 UTC (14 years, 2 months ago) by torben
File size: 1599 byte(s)
Add comment about "Content-Disposition" header
1
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5
6 import javax.servlet.ServletException;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import com.itextpdf.text.Document;
12 import com.itextpdf.text.PageSize;
13 import com.itextpdf.text.Paragraph;
14 import com.itextpdf.text.Rectangle;
15 import com.itextpdf.text.pdf.PdfWriter;
16
17 /**
18 * Servlet implementation class PdfServlet
19 */
20 public class PdfServlet extends HttpServlet {
21 private static final long serialVersionUID = 1L;
22
23
24 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
25 // Custom size Rectangle pageSize = new Rectangle(200f, 200f);
26 Document document = new Document(PageSize.A4);
27
28
29 ByteArrayOutputStream out = new ByteArrayOutputStream(32000);
30 try {
31 PdfWriter.getInstance(document,out);
32 document.open();
33 document.add(new Paragraph("Hello World"));
34
35 } catch (Exception e) {
36 // handle exception
37 }
38 document.close();
39 byte rawDocument[] = out.toByteArray();
40
41 response.setContentType("application/pdf");
42
43 response.setHeader("Cache-Control", "no-cache");
44 response.setDateHeader("Expires", 0);
45
46 //You will probably like to add this header in production, with some proper filename
47 //response.setHeader("Content-disposition", "attachment; filename=pdfname.pdf");
48
49
50 response.setContentLength(rawDocument.length);
51 response.getOutputStream().write( rawDocument );
52 }
53
54 }

  ViewVC Help
Powered by ViewVC 1.1.20