If you want to provide a download of customized PDF files, you can go the full-fledged way and buy into the Adobe world or build a slim custom solution which brings no additional cost.
This post shows a simple usecase where a PDF form is uploaded to DAM and later filled inside an OSGi service and delivered to the user by a Servlet.
Dependencies: Apache PDFBox
There are multiple open source PDF libraries we could use, but in the end Apache PDFBox1 is the weapon of choice. It is well documented, many examples are available and it is compatible with OSGi out of the box (no need for any wrapper etc).
In older versions of CQ, Adobe provided PDFBox as a default dependency but since CQ5.6 the complete PDF handling (extract metadata, generate thumbnails etc) is by a custom library (Gibson).
You need to include two dependencies to your pom.xml and make sure, they are deployed to AEM:
As we want a reusable solution, we will build all logic into a OSGi Service instead of the Servlet or some other class. You’ll be able to provide multiple implementations of the Service if required and generate different PDFs in each one.
The following snippet shows the required methods in our PDF Service:
Load PDF Document from DAM
Fill form fields
Glue it all together
Now as all logic is encapsulated in the Service, the Servlet can simply call the service and hand over the response’s OutputStream. If you need to pass any parameters (e.g. Query-Parameters) from the Request to the Service, you need to extend the Servlet accordingly.