Using Output and Forms Service API in AEM Forms

In this article we will take a look at the following

  • Output Service - Typically this service is used to merge xml data with xdp template or pdf to generate flattened pdf
  • FormsService - This is a very versatile service which allows you to export/import data from and into PDF file

The official javadoc for AEM Forms API is listed here

The following code snippet exports data from PDF file 

 

javax.servlet.http.Part pdfPart = request.getPart("pdffile");
String filePath = request.getParameter("saveLocation");
java.io.InputStream pdfIS = pdfPart.getInputStream();
com.adobe.aemfd.docmanager.Document pdfDocument = new com.adobe.aemfd.docmanager.Document(pdfIS);
com.adobe.fd.forms.api.FormsService formsservice = sling.getService(com.adobe.fd.forms.api.FormsService.class);
com.adobe.aemfd.docmanager.Document xmlDocument = formsservice.exportData(pdfDocument,com.adobe.fd.forms.api.DataFormat.Auto);

Line 1 extracts pdfifle from the request

Line2 extracts the saveLocation from the request

Line 5 gets hold of FormsService

Line 6 exports the xmlData from the PDF File

To test the sample package

Download and install the package using the AEM package manager

After you install the package you will have to whitelist the following URLs in Adobe Granite CSRF Filter.

  1. Please follow the steps mentioned below to whitelist the paths mentioned above.
  2. Login to http://localhost:4502/system/console/configMgr
  3. Search for Adobe Granite CSRF Filter
  4. Add the following 3 paths in the excluded sections and save
  5. /content/AemFormsSamples/mergedata
  6. /content/AemFormsSamples/exportdata
  7. /content/AemFormsSamples/outputservice
  8. Search for "Sling Referrer filter"
  9. Check the "Allow Empty" check box. (This setting should be for testing purposes only)

 

Download

There are a number of ways to test the sample code. The quickest and easiest is to use Postman chrome plugin. Postman allows you to make POST requests to your server. Install Postman chrome plugin. Launch the plugin and enter the following URL to test the export data API

Make sure you have selected "POST" from the drop down list

http://localhost:4502/content/AemFormsSamples/exportdata.html

Make sure you specify "Authorization" as "Basic Auth". Specify the AEM Server username and password

Navigate to the "Body" tab and specify the request parameters as shon in the image below

Then click "Send" button

PostExport

The package contains 3 samples. The following paragraphs explains when to use the output service or Forms Service, the url of the service ,input parameters that each service expects

Merge Data and Flatten Output:

  • Use Output Service to merge data with xdp or pdf document to generate flattened pdf
  • POST URL - http://localhost:4502/content/AemFormsSamples/outputservice.html
  • Request Parameters -
    •  xdp_or_pdf_file : The xdp or pdf file that you want to merge data with
    • xmlfile: The xml data file that will be merged with xdp_or_pdf_file
    • saveLocation: The location to save the rendered document on your file system

Import Data into  PDF File:

  • Use FormsService to import data into PDF file
  • POST URL - http://localhost:4502/content/AemFormsSamples/mergedata.html
  • Request Parameters:
    • pdffile : The pdf file that you want to merge data with
    • xmlfile: The xml data file that will be merged with pdf file
    • saveLocation: The location to save the rendered document on your file system. For example c:\\outputsample.pdf.

Export Data from PDF File

  • Use FormsService to export data from PDF File
  • POST URL - http://localhost:4502/content/AemFormsSamples/mergedata.html
  • Request Parameters:
    • pdffile : The pdf file that you want to merge data with
    • xmlfile: The xml data file that will be merged with pdf file
    • saveLocation: The location to save the rendered document on your file system

 

  • Request Parameters -

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy