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; |
import java.io.ByteArrayOutputStream; |
7 |
import java.io.IOException; |
import java.io.IOException; |
12 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
13 |
|
|
14 |
import com.itextpdf.text.Document; |
import com.itextpdf.text.Document; |
15 |
|
import com.itextpdf.text.Image; |
16 |
import com.itextpdf.text.PageSize; |
import com.itextpdf.text.PageSize; |
17 |
import com.itextpdf.text.Paragraph; |
import com.itextpdf.text.Paragraph; |
|
import com.itextpdf.text.Rectangle; |
|
18 |
import com.itextpdf.text.pdf.PdfWriter; |
import com.itextpdf.text.pdf.PdfWriter; |
19 |
|
|
20 |
/** |
/** |
26 |
|
|
27 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
28 |
// Custom size Rectangle pageSize = new Rectangle(200f, 200f); |
// Custom size Rectangle pageSize = new Rectangle(200f, 200f); |
29 |
|
// PageSize.A4.rotate(); //A4 in landscape mode |
30 |
Document document = new Document(PageSize.A4); |
Document document = new Document(PageSize.A4); |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
ByteArrayOutputStream out = new ByteArrayOutputStream(32000); |
ByteArrayOutputStream out = new ByteArrayOutputStream(32000); |
36 |
try { |
try { |
37 |
PdfWriter.getInstance(document,out); |
PdfWriter writer = PdfWriter.getInstance(document,out); |
38 |
document.open(); |
document.open(); |
39 |
document.add(new Paragraph("Hello World")); |
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) { |
} catch (Exception e) { |
49 |
// handle exception |
// handle exception |
50 |
} |
} |
56 |
response.setHeader("Cache-Control", "no-cache"); |
response.setHeader("Cache-Control", "no-cache"); |
57 |
response.setDateHeader("Expires", 0); |
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); |
response.setContentLength(rawDocument.length); |
64 |
response.getOutputStream().write( rawDocument ); |
response.getOutputStream().write( rawDocument ); |
65 |
} |
} |