1 |
torben |
614 |
|
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 |
|
|
response.setContentLength(rawDocument.length);
|
43 |
|
|
response.getOutputStream().write( rawDocument );
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
}
|