To generate this PDF we will use the iText Java PDF library. We will first create a document of the correct size set some metadata and finally add some content. At the end we will show you how to return a PDF (in this case our newly created PDF) to the users browser.
To start we will create the document object, leaving the parameters blank will create an A4 page size. It is possible to specify the page size if you require in the form of a Java Rectangle specifying the width by the height. Following on from there we create an OutputStream in this case it is a ByteArrayOutputStream. These are required to generate the PdfWriter object. Passing the document and baos to PrintWriter.getInstance() gets us the writer object that we will use later to put our direct content into the PDF.
Document document = new Document(); //Document document = new Document(new Rectangle(x,y)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos);
Having set up the document we will set some metadata values, below we set the title, subject and author. These inputs are fairly self explanatory.
document.addTitle('TomRed How To');
document.addSubject('How To Tutorials');
document.addAuthor('support[@]tomred.net');
document.addCreator('tomred.net');
document.addKeywords('tomred, tutorials, java, pdf');
It is also possible to set a background image for the document. This is not necessary but I have noted that clients are often happy to see their logo included or a simple watermark on the document. This is achieved by loading the image into an Image object. I want this image to sit in the top left corner and use the full width of the page. I have also set the image to have no border and the document to have no margins. Once the values are set we open the document and add the using the .add() method we add the image.
Image jpg = Image.getInstance("background.jpg");
jpg.setBorder(Image.NO_BORDER);
jpg.setBorderWidth(0);
jpg.setWidthPercentage(100);
document.setMargins(0, 0, 0, 0);
document.open();
document.add(jpg);
Now that we are ready to start entering text we access the content. We use the write to gain access to the content using the getDirectContent method with returns a PdfContentByte object. This object (cb) is then used to set the font and font size, start the editing and entering text. To enter the text in the case below we use showTextAlign which allows us to align the text as you would with a WYSIWYG editor to the left, right or center. We then enter the text we want to see along with its x and y coordinates. The final value allows us rotate the text if needs be. Once entry is finished we endText and we can if we want start a new text area. When you are finished with all the text you use document.close to finish. That is it you have a PDF... in memory. Next we will cover saving it.
PdfContentByte cb = writer.getDirectContent(); cb.setFontAndSize(font_bold, 17.5f); cb.beginText(); cb.showTextAligned(Element.ALIGN_LEFT, "TomRed How To", 11.0f + x, 40f + y, 0f); cb.endText(); cb.setFontAndSize(font_bold, 6.6f); cb.beginText(); cb.showTextAligned(Element.ALIGN_LEFT, "How To create a PDF with Java", 11.8f + x, 31f + y, 0f); cb.endText(); document.close();
Once you have a PDF generated you need to do something with it. It is common to require the PDF to be given to a client. In this case I am providing it for download. I have been often asked by clients to show the PDF in browser. This is possibly by setting the Content-Disposition to inline instead of attachment. This will offer the PDF for display inline but will not force the browser to show it as such. In many cases these days browsers will offer the PDF for download in either case.
To offer the PDF to the browser we set the Headers for cache control the set the content type and length to ensure that the browser understands what to do with the file. We then offer the filename set the Servlet out, write it, flush it and finally close it. Once you have flushed and closed you will not be able to alter the response any further. Make sure you leave this to the last.
response.setHeader("Cache-Control", "max-age=60");
response.setHeader("Pragma", "public");
// Set appropriate HTTP response headers to ensure the output
// will be presented as a file for download..
response.setContentType("application/pdf");
response.setContentLength(baos.size());
response.setHeader("Content-Disposition", "attachment; filename=\"some-info.pdf\"");
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
I hope you have found this useful if you have any questions, leave a comment or email us at support[@]tomred.net and we'll get back to you.


