Generate PDF documents using output service
[AEM Forms as a Cloud Service]{class="badge informative"}
The Output service is an OSGi service that is part of AEM Document Services. It supports various output formats and design features of AEM Forms Designer. The Output service converts XFA templates and XML data to generate print documents in different formats.
The Output service in AEM Forms as a Cloud Service closely resembles the one in AEM Forms 6.5, so if you’re familiar with using the Output service in AEM Forms 6.5, transitioning to AEM Forms as a Cloud Service should be straightforward.
With the Output service, you can create applications that allow you to:
- Generate final form documents by populating template files with XML data.
- Generate output forms in various formats, including non-interactive PDF, PostScript, PCL, and ZPL print streams.
- Generate print PDFs from XFA form PDFs.
- Generate PDF, PostScript, PCL, and ZPL documents in bulk by merging multiple sets of data with supplied templates.
This service is designed to be used within the context of an AEM Forms as a Cloud Service instance. The following code snippet generates a PDF document in a servlet using the OutputService
.
import com.adobe.fd.output.api.OutputService;
import com.adobe.fd.output.api.PDFOutputOptions;
import com.adobe.fd.output.api.AcrobatVersion;
import com.adobe.aemfd.docmanager.Document;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingServletPaths;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@Component(service = Servlet.class,
property = {
"sling.servlet.methods=" + HttpConstants.METHOD_POST,
"sling.servlet.paths=/bin/generateStatement"
})
public class GenerateStatementServlet extends SlingAllMethodsServlet {
@Reference
private OutputService outputService;
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
// Access the submitted form data
String formData = request.getParameter("formData");
// Define the XDP template document
String templateName = "/content/dam/formsanddocuments/adobe/statement.xdp";
Document xdpDocument = new Document(templateName);
// Set the PDF output options
PDFOutputOptions pdfOutputOptions = new PDFOutputOptions();
pdfOutputOptions.setAcrobatVersion(AcrobatVersion.Acrobat_10);
// Create the submitted data document from the form data
InputStream inputStream = new ByteArrayInputStream(formData.getBytes(StandardCharsets.UTF_8));
Document submittedData = new Document(inputStream);
// Use the output service to generate the PDF output
Document generatedPDF = outputService.generatePDFOutput(xdpDocument, submittedData, pdfOutputOptions);
// Process the generated PDF as per your use case
}
}