Generating PDF documents on the fly isn't exactly rocket science and - with the help of tools like iText - is easily done in Java and something I've talked about before.
The trouble is that creating them is a cumbersome and often tedious task if what you want is anything other than a set of plain text paragraphs added in series. Unlike creating HTML everything in PDF-land is of absolute size and position. I find that getting the result you want is often a case of repetitive, pixel by pixel tweaking to get it right. You need a lot of patience.
If you had a complicated document to create with logos and background images then you're in for a rough ride. What would make more sense is to use a GUI tool to author a base template and then just have your code add the text bits where necessary. This is the situation I found myself in recently and after a bit of digging I worked out how to take an existing PDF and add to it. PDF templating if you will.
Creating a Template
First thing to do, assuming you've not already been provided with a template is to create one. Let's look at creating a letter head. I did this in Word, as you can see below and simply pasted in some images, moved them about and added some company details to the footer in an off grey colour.
Once I was happy with it I used the File - Save As - PDF menu option to save the file in PDF format.
You can see the resulting PDF below:
What I did with the PDF is attached it to a document in the same Notes database as we're generating the PDFs from. In reality I had been provided with a PDF template by my client's design agency, so that's what I attached. I could skip the bit above about creating a template, but went through it here in case you need help or inspiration.
The idea behind adding it as an attachment to a document is that the client has control over the template and can change it as and when they please. That being another benefit of this approach - if they happened to change the design of the letterhead then I'd have to go back to messing with the code each time. With this approach there should be no need to.
All we need to do now is add the actual content. Here's an example of what the generated PDF looks like once we've done adding to it with our own code:
Let's look at how we'd do that.
Adding To The Template
The code below is the Java needed to create a new PDF document, import the existing template and then add our own text atop of it.
//Setup a new PDF Document Document document = new Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, os); document.open(); document.newPage(); //Get the template PDF as the starting point! View files = db.getView("files"); lotus.domino.Document filestore = settings.getDocumentByKey("pdf_template.pdf", true); String filename = filestore.getItemValueString("FileName"); EmbeddedObject file = filestore.getAttachment(filesname); InputStream is = file.getInputStream(); //Here's the key part. Let's turn the template in to //usable PDF object PdfReader reader = new PdfReader(is); PdfImportedPage page = writer.getImportedPage(reader, 1); //Now, add it to the blank PDF document we've opened PdfContentByte cb = writer.getDirectContent(); cb.addTemplate(page, 0, 0); //Now do the usual addition of text atop the template document.add( new Paragraph("Here some text added to the template") ); //Etc, etc //Done! document.close(); //Tidy up is.close(); file.recycle(); //Make SURE you do this!!! //Attach the resuling PDF to the Notes document Stream stream = session.createStream(); stream.write(os.toByteArray()); MIMEEntity m = doc.createMIMEEntity("Files"); MIMEHeader header = m.createHeader("content-disposition"); header.setHeaderVal("attachment;filename=\""+filename+"\""); m.setContentFromBytes(stream, "application/pdf", MIMEEntity.ENC_IDENTITY_BINARY); m.decodeContent();
Code's boring though. What you want is a demo and a download, no?
The working download will be in form of DEXT 20091123, which will be in the Sandbox in the next few minutes.