Rendering Interactive PDF using Forms Services in AEM Forms

Using Forms Service API in AEM Forms to render interactive PDF

In this article we will take a look at the following service

  • FormsService - This is a very versatile service which allows you to export/import data from and into PDF file and also generate interactive pdf by merging xml data into xdp template

The official javadoc for AEM Forms API is listed here

The following code snippet renders interactive pdf using the renderPDFForm operation of the FormsService. The schengen.xdp is template that is being used to merge the xml data.

String uri = "crx:///content/dam/formsanddocuments";
PDFFormRenderOptions renderOptions = new PDFFormRenderOptions();
renderOptions.setAcrobatVersion(AcrobatVersion.Acrobat_11);
renderOptions.setContentRoot(uri);
Document interactivePDF = null;
try {
interactivePDF = formsService.renderPDFForm("schengen.xdp", xmlData, renderOptions);
} catch (FormsServiceException e) {
 e.printStackTrace();
}
return interactivePDF;

Line 1: Location of the folder which contains the xdp template

Line2-4: Create PDFFormRenderOptions and set its properties

Line 7: Generate Interactive PDF using the renderPDFForm service operation of FormsService

Line 11: Returns the generated interactive pdf to the calling application

To test the sample package on your system

  1. Download and install DevelopingWithServiceUserBundle

  2. Download and install the DocumentServices Sample Bundle using the Felix Web Console

  3. Download and install the package using the AEM package manager

  4. Login to configMgr

  5. Search for Adobe Granite CSRF Filter

  6. Add the following path in the excluded sections and save

  7. /bin/generateinteractivepdf

  8. Search for Apache Sling Service User Mapper Service and click to open the properties

    1. Click the + icon (plus) to add the following Service Mapping
      • DevelopingWithServiceUser.core:getformsresourceresolver=fd-service
    2. Click ’ Save ’
  9. Open the mobile form

  10. Fill in a couple of fields and then click the Download and fill … button

  11. The interactive pdf should be downloaded to your local system

The sample package contains the custom profile that is associated with the Mobile Form. Please explore the customtoolbar.jsp file. This jsp extracts the data from the mobile form and makes a POST request to servlet mounted on /bin/generateinteractivepdf path. The servlet return the interactive pdf to the calling application. The code in the customtoolbar.jsp then downloads the file to your local system

recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e