使用权限将XDP呈现为PDF rendering-xdp-into-pdf-with-usage-rights
一个常见用例是将xdp渲染到PDF并将Reader扩展应用于渲染的PDF。
例如,在AEM Forms的Forms Portal中,当用户单击XDP时,我们可以呈现XDP作为PDF,读者可以扩展PDF。
要完成此用例,我们需要执行以下操作。
-
将Reader扩展证书添加到“fd-service”用户。 列出了添加Reader扩展凭据的步骤 此处
-
您也可以参考以下视频: 配置Reader扩展凭据
-
创建可呈现和应用使用权限的自定义OSGi服务。 下面列出了完成此任务的代码
呈现XDP并应用使用权限 render-xdp-and-apply-usage-rights
-
第7行:我们使用FormsService的renderPDFForm从XDP生成PDF。
-
第8-14行:设置了适当的使用权限。 将从OSGi配置设置获取这些使用权限。
-
第20行:使用与服务用户fd-service关联的resourceresolver
-
第24行: DocumentAssuranceService的secureDocument方法用于应用使用权限
public Document renderAndExtendXdp(String xdpPath) {
// TODO Auto-generated method stub
log.debug("In renderAndExtend xdp the alias is " + docConfig.ReaderExtensionAlias());
PDFFormRenderOptions renderOptions = new PDFFormRenderOptions();
renderOptions.setAcrobatVersion(AcrobatVersion.Acrobat_11);
try {
Document xdpRenderedAsPDF = formsService.renderPDFForm("crx://" + xdpPath, null, renderOptions);
UsageRights usageRights = new UsageRights();
usageRights.setEnabledBarcodeDecoding(docConfig.BarcodeDecoding());
usageRights.setEnabledFormFillIn(docConfig.FormFill());
usageRights.setEnabledComments(docConfig.Commenting());
usageRights.setEnabledEmbeddedFiles(docConfig.EmbeddingFiles());
usageRights.setEnabledDigitalSignatures(docConfig.DigitialSignatures());
usageRights.setEnabledFormDataImportExport(docConfig.FormDataExportImport());
ReaderExtensionsOptionSpec reOptionsSpec = new ReaderExtensionsOptionSpec(usageRights, "Sample ARES");
UnlockOptions unlockOptions = null;
ReaderExtensionOptions reOptions = ReaderExtensionOptions.getInstance();
reOptions.setCredentialAlias(docConfig.ReaderExtensionAlias());
log.debug("set the credential");
reOptions.setResourceResolver(getResolver.getFormsServiceResolver());
reOptions.setReOptions(reOptionsSpec);
log.debug("set the resourceResolver and re spec");
xdpRenderedAsPDF = docAssuranceService.secureDocument(xdpRenderedAsPDF, null, null, reOptions,
unlockOptions);
return xdpRenderedAsPDF;
} catch (FormsServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
以下屏幕截图显示了公开的配置属性。 大多数常见使用权限均通过此配置公开。
以下代码显示了用于构建OSGi配置设置的代码
package com.aemformssamples.configuration;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@ObjectClassDefinition(name = "AEM Forms Samples Doc Services Configuration", description = "AEM Forms Samples Doc Services Configuration")
public @interface DocSvcConfiguration {
@AttributeDefinition(name = "Allow Form Fill", description = "Allow Form Fill", type = AttributeType.BOOLEAN)
boolean FormFill() default false;
@AttributeDefinition(name = "Allow BarCode Decoding", description = "Allow BarCode Decoding", type = AttributeType.BOOLEAN)
boolean BarcodeDecoding() default false;
@AttributeDefinition(name = "Allow File Embedding", description = "Allow File Embedding", type = AttributeType.BOOLEAN)
boolean EmbeddingFiles() default false;
@AttributeDefinition(name = "Allow Commenting", description = "Allow Commenting", type = AttributeType.BOOLEAN)
boolean Commenting() default false;
@AttributeDefinition(name = "Allow DigitialSignatures", description = "Allow File DigitialSignatures", type = AttributeType.BOOLEAN)
boolean DigitialSignatures() default false;
@AttributeDefinition(name = "Allow FormDataExportImport", description = "Allow FormDataExportImport", type = AttributeType.BOOLEAN)
boolean FormDataExportImport() default false;
@AttributeDefinition(name = "Reader Extension Alias", description = "Alias of your Reader Extension")
String ReaderExtensionAlias() default "";
}
创建Servlet以流式传输PDF create-servlet-to-stream-the-pdf
下一步是使用GET方法创建一个servlet以将读取器扩展PDF返回给用户。 在这种情况下,要求用户将PDF保存到其文件系统。 这是因为PDF呈现为动态PDF,并且浏览器附带的PDF查看器无法处理动态PDF。
以下是servlet的代码。 我们将CRX存储库中XDP的路径传递到此servlet。
然后调用com.aemformssamples.documentservices.core.DocumentServices的renderAndExtendXdp方法。
然后,读取器扩展PDF被流式传输到调用应用程序
package com.aemformssamples.documentservices.core.servlets;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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 com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.api.FormsService;
import com.aemformssamples.documentservices.core.DocumentServices;
@Component(service = Servlet.class, property = {
"sling.servlet.methods=get",
"sling.servlet.paths=/bin/renderandextend"
})
public class RenderAndReaderExtend extends SlingSafeMethodsServlet {
@Reference
FormsService formsService;
@Reference
DocumentServices documentServices;
private static final Logger log = LoggerFactory.getLogger(RenderAndReaderExtend.class);
private static final long serialVersionUID = 1L;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
log.debug("The path of the XDP I got was " + request.getParameter("xdpPath"));
Document renderedPDF = documentServices.renderAndExtendXdp(request.getParameter("xdpPath"));
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=AemFormsRocks.pdf");
try {
response.setContentLength((int) renderedPDF.length());
InputStream fileInputStream = null;
fileInputStream = renderedPDF.getInputStream();
OutputStream responseOutputStream = null;
responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
{
responseOutputStream.write(bytes);
}
}
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
要在本地服务器上对此进行测试,请执行以下步骤
-
- 此软件包包含示例门户和xdp文件
-
将Reader扩展证书添加到“fd-service”用户
-
将浏览器指向 门户网页
-
单击pdf图标可将xdp渲染为应用了使用权限的pdf文件。