Using API to generate Document of Record in AEM Forms
Last update: March 25, 2025
- Topics:
- Adaptive Forms
CREATED FOR:
- Experienced
- Developer
Generate Document Of Record (DOR) programmatically
This article illustrates the use of the com.adobe.aemds.guide.addon.dor.DoRService API
to generate Document of Record programmatically. Document of Record is a PDF version of the data captured in Adaptive Form.
- The following is the code snippet. The first line gets the DOR Service.
- Set the DoROptions.
- Invoke the render method of the DoRService and pass the DoROptions object to the render method
String dataXml = request.getParameter("data");
System.out.println("Got " + dataXml);
Session session;
com.adobe.aemds.guide.addon.dor.DoRService dorService = sling.getService(com.adobe.aemds.guide.addon.dor.DoRService.class);
System.out.println("Got ... DOR Service");
com.mergeandfuse.getserviceuserresolver.GetResolver aemDemoListings = sling.getService(com.mergeandfuse.getserviceuserresolver.GetResolver.class);
System.out.println("Got aem DemoListings");
resourceResolver = aemDemoListings.getFormsServiceResolver();
session = resourceResolver.adaptTo(Session.class);
resource = resourceResolver.getResource("/content/forms/af/sandbox/1201-borrower-payments");
com.adobe.aemds.guide.addon.dor.DoROptions dorOptions = new com.adobe.aemds.guide.addon.dor.DoROptions();
dorOptions.setData(dataXml);
dorOptions.setFormResource(resource);
java.util.Locale locale = new java.util.Locale("en");
dorOptions.setLocale(locale);
com.adobe.aemds.guide.addon.dor.DoRResult dorResult = dorService.render(dorOptions);
byte[] fileBytes = dorResult.getContent();
com.adobe.aemfd.docmanager.Document dorDocument = new com.adobe.aemfd.docmanager.Document(fileBytes);
resource = resourceResolver.getResource("/content/usergenerated/content/aemformsenablement");
Node paydotgov = resource.adaptTo(Node.class);
java.util.Random r = new java.util.Random();
String nodeName = Long.toString(Math.abs(r.nextLong()), 36);
Node fileNode = paydotgov.addNode(nodeName + ".pdf", "nt:file");
System.out.println("Created file Node...." + fileNode.getPath());
Node contentNode = fileNode.addNode("jcr:content", "nt:resource");
Binary binary = session.getValueFactory().createBinary(dorDocument.getInputStream());
contentNode.setProperty("jcr:data", binary);
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("filePath");
writer.value(fileNode.getPath());
writer.endObject();
session.save();
To try this on your local system, please follow the following steps
- Download and install the article assets using package manager
- Make sure you have installed and started the DevelopingWithServiceUser bundle provided as part of Create Service User article
- Login to configMgr
- Search for Apache Sling Service User Mapper Service
- Make sure you the following entry DevelopingWithServiceUser.core:getformsresourceresolver=fd-service in the Service Mappings section
- Open the form
- Fill out the form and click on ’ View PDF ’
- You should see DOR in new tab in your browser
Troubleshooting Tips
PDF isn’t displayed in new browser tab:
- Make sure you are not blocking popups in your browser
- Make sure you are starting AEM server as an administrator(at least on windows)
- Make sure the ‘DevelopingWithServiceUser’ bundle is in active state
- Make sure the system user ’ fd-service’ has Read, Modify, and Create permissions on the following node
/content/usergenerated/content/aemformsenablement
recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e