This article will demonstrate the ability to merge data from adaptive form submission with acroform and stream the generated pdf back to the submitter. The following steps are required to accomplish this use case

Create AEM Form from Acroform

At a very high-level Acroform's are created using Acrobat. You can also convert forms created in Microsoft Word to Acroform using Acrobat.  Please follow this link to create an Adaptive Form from acroform.

A sample Adaptive Form is provided as part of this article's assets.

Please visit the AEM Forms samples page for a link to a live demo of this capability.

Servlet to merge submitted data with acroform

Create a custom servlet to merge the submitted data with the acroform. Once the pdf is created, we stream the pdf back to the user. The following code extracts the submitted data from the request object and merges the data with the acroform

The code also appends the jpg's added as form attachments to the PDF that is generated.

package com.aemforms.acroform.core.servlets;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import com.aemforms.acroform.core.AcroformToAemform;
import com.aemformssamples.documentservices.core.DocumentServices;

/**
 * Servlet that writes some sample content into the response. It is mounted for
 * all resources of a specific Sling resource type. The
 * {@link SlingSafeMethodsServlet} shall be used for HTTP methods that are
 * idempotent. For write operations use the {@link SlingAllMethodsServlet}.
 */
@Component(service = Servlet.class, property = {

		"sling.servlet.methods=get", "sling.servlet.methods=post",

		"sling.servlet.paths=/bin/mergedata"

})
public class MergeDataServlet extends SlingAllMethodsServlet {
	private static final Logger log = LoggerFactory.getLogger(MergeDataServlet.class);
	private static final long serialVersionUID = 1L;

	@Reference
	AcroformToAemform acroForms;
	@Reference
	DocumentServices documentServices;

	@Override
	protected void doPost(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
			throws ServletException, IOException {
		log.debug("####The data I got was#### " + req.getParameter("jcr:data"));
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;
		com.adobe.aemfd.docmanager.Document documentToReturn = null;
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
			org.w3c.dom.Document submittedData = docBuilder
					.parse(new org.xml.sax.InputSource(new StringReader(req.getParameter("jcr:data"))));
			documentToReturn = acroForms.getMergedDocument(submittedData);
			if (submittedData.getElementsByTagName("voidimagepath").getLength() > 0) {
				log.debug(submittedData.getElementsByTagName("voidimagepath").item(0).getTextContent());
				com.adobe.aemfd.docmanager.Document voidImage = new com.adobe.aemfd.docmanager.Document(
						submittedData.getElementsByTagName("voidimagepath").item(0).getTextContent());
				Map<String, Object> mapOfDocuments = new HashMap<String, Object>();
				mapOfDocuments.put("form", documentToReturn);
				mapOfDocuments.put("check", voidImage);
				com.adobe.aemfd.docmanager.Document ddxDocument = documentServices.createSimpleDDX(mapOfDocuments);
				documentToReturn = documentServices.assembleDocuments(mapOfDocuments, ddxDocument);

			}

			InputStream fileInputStream = documentToReturn.getInputStream();
			resp.setContentType("application/pdf");
			resp.addHeader("Content-Disposition", "attachment; filename=AemFormsRocks.pdf");
			resp.setContentLength((int) fileInputStream.available());
			OutputStream responseOutputStream = resp.getOutputStream();
			int bytes;
			while ((bytes = fileInputStream.read()) != -1) {
				responseOutputStream.write(bytes);
			}
			responseOutputStream.flush();
			responseOutputStream.close();

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

Create Custom Submit

The next step is to create the custom submit action to handle the adaptive form submission. This step is necessary for us if we want to stream the generated PDF back to the user. 

To learn how to create a custom submit please follow this link. A sample custom submit is provided to you as part of this article's assets.

The following code in post.POST.jsp forwards the request to the servlet mapped to /bin/mergedata. This servlet's code is listed above

<%@include file="/libs/foundation/global.jsp"%>
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0"%>
<%@ page import="org.apache.sling.api.request.RequestParameter,com.day.cq.wcm.api.WCMMode" %>

<%@page session="false" %><%
%><%

	com.adobe.aemds.guide.utils.GuideSubmitUtils.setForwardPath(slingRequest,"/bin/mergedata",null,null);


%>

To test this on your system, please follow the steps mentioned below

  1. Download the zip file
  2. Import the package using the package manager
  3. Create a system user as mentioned in this document
  4. Make sure you provide read access to the following nodes to the system user(data) created in step 3
    1. /content/dam/acroforms
    2. /tmp/fd/af(Needed for attachments added through Adaptive Form)
  5. Open the Direct Deposit Form
  6. Fill in all the fields and attach void check image(any jpg for that matter) and submit the form
  7. You should get back PDF merged with the data filled in Adaptive Form

Download

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