使用输出服务生成PDF文档

[AEM Forms as a Cloud Service]{class="badge informative"}

Output服务是属于AEM Document Services的OSGi服务。 它支持各种AEM Forms Designer输出格式和设计功能。 输出服务转换XFA模板和XML数据以生成不同格式的打印文档。

AEM Forms as a Cloud Service中的输出服务与AEM Forms 6.5中的输出服务非常相似,因此,如果您熟悉AEM Forms 6.5中的输出服务,则过渡到AEM Forms as a Cloud Service应该非常简单。

使用Output服务,您可以创建应用程序,以便:

  • 使用 XML 数据填充模板文件来生成最终表单文档。
  • 生成各种格式的输出表单,包括非交互式PDF、PostScript、PCL和ZPL打印流。
  • 从 XFA 表单 PDF 生成打印 PDF。
  • 通过将多组数据与提供的模板合并,批量生成PDF、PostScript、PCL和ZPL文档。

此服务旨在在AEM Forms as a Cloud Service实例的上下文中使用。 以下代码片段使用OutputService在servlet中生成PDF文档。

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
    }
}
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69