Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Generating a PDF Document From an Adaptive Form Part II

garyT
Level 2
Level 2

Here is a sample that renders an adaptive form, loads some information into a hidden field on the form via JSON to use as a timestamp and save location. Once the form submits, it will submit to a custom JSP that saves the data, generates a PDF, and then renders that PDF back to the user so they can sign or print this as a document of record.

Attached is the package. Below is the JSP to handle the custom submit

Dropbox - Return DOR Custom Submit.zip

<%--

    ~ Demo Sample: Gary Thain 01/12/2015

    ~ Returns a PDF DOR from Adaptive Form

    ~ Uses the data on the form to save Original data,

  ~ XDP data and generated PDF to CRX

    ~ Requires:

  a. User Mapper be set

  b. XDP is selected as Document of Record Template

    ~ Still need to add function so user has permissions to node

    ~ where PDF is saved. Currently only working with Admin permissions

--%>

<%@include file="/libs/fd/af/components/guidesglobal.jsp"%>

<%@page import="org.slf4j.Logger"%>

<%@page import="org.slf4j.LoggerFactory"%>

<%@page import="java.util.Calendar"%>

<%@page import="java.util.Map"%>

<%@page import="java.io.InputStream"%>

<%@page import="org.apache.commons.io.IOUtils"%>

<%@page import="org.apache.sling.jcr.api.SlingRepository"%>

<%@page import="org.apache.sling.api.request.RequestParameter"%>

<%@page import="org.apache.sling.api.request.RequestParameterMap"%>

<%@page import="org.apache.sling.api.resource.ResourceUtil"%>

<%@page import="org.w3c.dom.Document"%>

<%@page import="javax.jcr.Session"%>

<%@page import="javax.xml.parsers.DocumentBuilder"%>

<%@page import="javax.xml.parsers.DocumentBuilderFactory"%>

<%@page import="org.apache.jackrabbit.commons.JcrUtils"%>

<%@page import="com.adobe.aemds.guide.utils.XMLUtils"%>

<%@page import="com.adobe.fd.output.api.OutputService"%>

<%@page import="com.adobe.fd.output.api.PDFOutputOptions"%>

<%@page import="com.adobe.aemds.guide.servlet.GuideSubmitServlet" %>

<%!private final Logger log = LoggerFactory.getLogger(getClass());%>

<%

  final String NODE_TYPE = "sling:Folder";

  final String XMLSUBMITTED_SAVE_LOCATION = "XMLDataLocAF";

  final String XMLXDP_SAVE_LOCATION = "XMLDataLocXDP";

  final String PDF_SAVE_LOCATION = "PDFDataLoc";

  final String MIME_TYPE_XML = "text/xml";

  final String MIME_TYPE_PDF = "application/pdf";

  final String FOLDER_LOCATION = "/content/usergenerated/content/aemforms-training-demo1";

  final ValueMap valueMap = ResourceUtil.getValueMap(resource);

  String sPath1 = "content/usergenerated/content/";

  String sPath2 = "aemforms-training-demo1/";

  String xmlDataAF = "";

  String sRefID = "";

  String sUserID = "";

  String sSubmitID = "";

  String sTemplateXDP = "";

  Boolean bDebug = true;

  Document docXMLAF = null;

  Document docXMLPDF = null;

  Session adminSession = null;

  com.adobe.aemfd.docmanager.Document docTemplate = null;

  com.adobe.aemfd.docmanager.Document docXMLXDP = null;

  com.adobe.aemfd.docmanager.Document docPDF = null;

  PDFOutputOptions pdfOptions = null;

  javax.jcr.Node nodeJCRRoot = null;

  javax.jcr.Node nodeJCRPath = null;

  javax.jcr.Node nodeSubmittedXML = null;

  javax.jcr.Node nodeXDPXDML = null;

  javax.jcr.Node nodePDF = null;

  Calendar calendarSubmitted = Calendar.getInstance();

  if (bDebug == true) {

  java.util.Iterator it = valueMap.entrySet().iterator();

  while (it.hasNext()) {

  Map.Entry pair = (Map.Entry) it.next();

  log("ValueMap Key  " + pair.getKey() + " and value "+ pair.getValue());

  }

  }

  //Get access to the repository

  final SlingRepository repository = sling.getService(SlingRepository.class);

  adminSession = repository.loginService(null,repository.getDefaultWorkspace());

  nodeJCRRoot = adminSession.getRootNode();

  //Get DOR reference from submitted data and load into Adobe Document Object

  sTemplateXDP = (valueMap.get("dorTemplateRef").toString());

  docTemplate = new com.adobe.aemfd.docmanager.Document(sTemplateXDP);

  //Read the submitted data and see what the Ref ID is. This will makeup the location to save this submissin

  if (slingRequest.getParameter("jcr:data") != null) {

  RequestParameterMap reqMap = slingRequest.getRequestParameterMap();

  RequestParameter reqData = reqMap.getValue("jcr:data");

  xmlDataAF = reqData.toString();

  InputStream inStreamOriginalXML = IOUtils.toInputStream(xmlDataAF, "UTF-8");

  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

  docFactory.setNamespaceAware(true);

  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

  docXMLAF = docBuilder.parse(inStreamOriginalXML);

  docXMLAF.getDocumentElement().normalize();

  sRefID = docXMLAF.getElementsByTagName("refID").item(0).getTextContent();

  if (sRefID != null) {

  String[] sArray = sRefID.split("/");

  sUserID = sArray[0];

  sSubmitID = sArray[1];

  }

  }

  //Adaptive Form data has additional stucture. Get just the XDP data and load into Adobe Document object

  String xmlDataXDP = XMLUtils.getBoundDataXmlPart(docXMLAF);

  InputStream inStreamXDPXML = IOUtils.toInputStream(xmlDataXDP,"UTF-8");

  docXMLXDP = new com.adobe.aemfd.docmanager.Document(inStreamXDPXML);

  if (bDebug == true) {

  log(xmlDataXDP);

  }

  //Set some PDF options and generate the PDF for the client

  pdfOptions = new PDFOutputOptions();

  pdfOptions.setAcrobatVersion(com.adobe.fd.output.api.AcrobatVersion.Acrobat_10);

  pdfOptions.setLinearizedPDF(true);

  OutputService outputService = sling.getService(OutputService.class);

  try {

  docPDF = outputService.generatePDFOutput(docTemplate,docXMLXDP, pdfOptions);

  } catch (Exception e) {

  log("ERROR : " + e.getMessage());

  }

  //Store everything in CRX

  nodeJCRRoot = adminSession.getRootNode();

  nodeJCRPath = nodeJCRRoot.getNode(sPath1);

  nodeJCRPath = JcrUtils.getOrAddNode(nodeJCRPath, sPath2, NODE_TYPE);

  nodeJCRPath = JcrUtils.getOrAddNode(nodeJCRPath, sUserID, NODE_TYPE);

  nodeJCRPath = JcrUtils.getOrAddNode(nodeJCRPath, sSubmitID, NODE_TYPE);

  nodeSubmittedXML = JcrUtils.putFile(nodeJCRPath,XMLSUBMITTED_SAVE_LOCATION, MIME_TYPE_XML,IOUtils.toInputStream(xmlDataAF, "UTF-8"),calendarSubmitted);

  nodeXDPXDML = JcrUtils.putFile(nodeJCRPath, XMLXDP_SAVE_LOCATION,MIME_TYPE_XML, docXMLXDP.getInputStream(), calendarSubmitted);

  nodePDF = JcrUtils.putFile(nodeJCRPath, PDF_SAVE_LOCATION,MIME_TYPE_PDF, docPDF.getInputStream(), calendarSubmitted);

  adminSession.save();

  //Use the GuideServlet Redirect function to send the browser to the new PDF

  GuideSubmitServlet.setRedirectUrl(slingRequest, nodePDF.getPath());

  adminSession.logout();

%>

0 Replies