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 |
|
* How to add page header/footer via PdfPageEventHelper : |
6 |
|
* http://faq.javaranch.com/java/ItextExample |
7 |
|
*/ |
8 |
|
|
9 |
import java.io.ByteArrayOutputStream; |
import java.io.ByteArrayOutputStream; |
10 |
import java.io.IOException; |
import java.io.IOException; |
15 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
16 |
|
|
17 |
import com.itextpdf.text.Document; |
import com.itextpdf.text.Document; |
18 |
|
import com.itextpdf.text.Image; |
19 |
import com.itextpdf.text.PageSize; |
import com.itextpdf.text.PageSize; |
20 |
import com.itextpdf.text.Paragraph; |
import com.itextpdf.text.Paragraph; |
|
import com.itextpdf.text.Rectangle; |
|
21 |
import com.itextpdf.text.pdf.PdfWriter; |
import com.itextpdf.text.pdf.PdfWriter; |
22 |
|
|
23 |
/** |
/** |
29 |
|
|
30 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
31 |
// Custom size Rectangle pageSize = new Rectangle(200f, 200f); |
// Custom size Rectangle pageSize = new Rectangle(200f, 200f); |
32 |
|
// PageSize.A4.rotate(); //A4 in landscape mode |
33 |
Document document = new Document(PageSize.A4); |
Document document = new Document(PageSize.A4); |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
ByteArrayOutputStream out = new ByteArrayOutputStream(32000); |
ByteArrayOutputStream out = new ByteArrayOutputStream(32000); |
39 |
try { |
try { |
40 |
PdfWriter.getInstance(document,out); |
PdfWriter writer = PdfWriter.getInstance(document,out); |
41 |
document.open(); |
document.open(); |
42 |
document.add(new Paragraph("Hello World")); |
document.add(new Paragraph("Hello World")); |
43 |
|
|
44 |
|
document.add(new Paragraph("Lorem ipsum")); |
45 |
|
Image img = Image.getInstance("http://app.t-hoerup.dk/Test3/ImageServlet"); |
46 |
|
img.scaleAbsolute(100, 100); |
47 |
|
img.setRotationDegrees(45); |
48 |
|
document.add(img); |
49 |
|
|
50 |
|
|
51 |
} catch (Exception e) { |
} catch (Exception e) { |
52 |
// handle exception |
// handle exception |
53 |
} |
} |
55 |
byte rawDocument[] = out.toByteArray(); |
byte rawDocument[] = out.toByteArray(); |
56 |
|
|
57 |
response.setContentType("application/pdf"); |
response.setContentType("application/pdf"); |
58 |
|
|
59 |
|
response.setHeader("Cache-Control", "no-cache"); |
60 |
|
response.setDateHeader("Expires", 0); |
61 |
|
|
62 |
|
//You will probably like to add this header in production, with some proper filename |
63 |
|
//response.setHeader("Content-disposition", "attachment; filename=pdfname.pdf"); |
64 |
|
|
65 |
|
|
66 |
response.setContentLength(rawDocument.length); |
response.setContentLength(rawDocument.length); |
67 |
response.getOutputStream().write( rawDocument ); |
response.getOutputStream().write( rawDocument ); |
68 |
} |
} |