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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 617 - (show annotations) (download)
Wed Feb 24 19:56:38 2010 UTC (14 years, 2 months ago) by torben
File size: 2063 byte(s)
Image sample
1 /* Sample servlet to demonstrate the usage of iText for generation pdf documents.
2 *
3 * For more in-depth look at the API docs or the book "iText in action"
4 */
5
6 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
8
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import com.itextpdf.text.Document;
15 import com.itextpdf.text.Image;
16 import com.itextpdf.text.PageSize;
17 import com.itextpdf.text.Paragraph;
18 import com.itextpdf.text.pdf.PdfWriter;
19
20 /**
21 * Servlet implementation class PdfServlet
22 */
23 public class PdfServlet extends HttpServlet {
24 private static final long serialVersionUID = 1L;
25
26
27 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28 // Custom size Rectangle pageSize = new Rectangle(200f, 200f);
29 // PageSize.A4.rotate(); //A4 in landscape mode
30 Document document = new Document(PageSize.A4);
31
32
33
34
35 ByteArrayOutputStream out = new ByteArrayOutputStream(32000);
36 try {
37 PdfWriter writer = PdfWriter.getInstance(document,out);
38 document.open();
39 document.add(new Paragraph("Hello World"));
40
41 document.add(new Paragraph("Lorem ipsum"));
42 Image img = Image.getInstance("http://app.t-hoerup.dk/Test3/ImageServlet");
43 img.scaleAbsolute(100, 100);
44 img.setRotationDegrees(45);
45 document.add(img);
46
47
48 } catch (Exception e) {
49 // handle exception
50 }
51 document.close();
52 byte rawDocument[] = out.toByteArray();
53
54 response.setContentType("application/pdf");
55
56 response.setHeader("Cache-Control", "no-cache");
57 response.setDateHeader("Expires", 0);
58
59 //You will probably like to add this header in production, with some proper filename
60 //response.setHeader("Content-disposition", "attachment; filename=pdfname.pdf");
61
62
63 response.setContentLength(rawDocument.length);
64 response.getOutputStream().write( rawDocument );
65 }
66
67 }

  ViewVC Help
Powered by ViewVC 1.1.20