ドキュメントAEMAEM チュートリアルAEM Forms のチュートリアル

使用権限を持つ PDF への XDP のレンダリング

最終更新日: 2025年4月2日
  • 適用対象:
  • Experience Manager 6.4
  • Experience Manager 6.5
  • トピック:

作成対象:

  • 経験者
  • 開発者

一般的なユースケースは、XDP を PDF にレンダリングし、レンダリングされた PDF に Reader Extensions を適用することです。

例えば、AEM Forms のフォームポータルで、ユーザーが XDP をクリックすると、XDP を PDF としてレンダリングし、Reader が PDF を拡張できます。

このユースケースを実現するには、次の手順を実行する必要があります。

  • Reader Extensions 証明書を「fd-service」ユーザーに追加します。Reader Extensions 証明書を追加する手順については、こちらを参照してください

  • また、Reader Extensions 資格情報の設定については、ビデオを参照してください。

  • レンダリングして使用権限を適用するカスタム OSGi サービスを作成します。これを行うコードを以下に示します。

XDP のレンダリングおよび使用権限の適用

  • 行 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 "";

}

サーブレットを作成して PDF をストリーミングします

次の手順では、GET メソッドを使用してサーブレットを作成し、Reader 用の拡張 PDF をユーザーに返します。この場合、ユーザーはファイルシステムに PDF を保存するように求められます。 これは PDF が動的 PDF としてレンダリングされ、ブラウザーに付属している PDF ビューアが動的 PDF を処理しないためです。

次にサーブレットのコードを示します。CRX リポジトリ内の XDP のパスをこのサーブレットに渡します。

次に、com.aemformssamples.documentservices.core.DocumentServices の renderAndExtendXdp メソッドを呼び出します。

その後、Reader 用の拡張 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();
  }

 }

}

ローカルサーバーでこれをテストするには、次の手順に従ってください

  1. DevelopingWithServiceUser バンドルをダウンロードしてインストールします

  2. AEMFormsDocumentServices バンドルをダウンロードしてインストールします

  3. カスタムポータルテンプレートの html をダウンロードします

  4. パッケージマネージャーを使用して、この記事に関連するアセットをダウンロードし、AEM に読み込みます

    • このパッケージには、サンプルポータルと XDP ファイルが含まれています
  5. Reader Extensions 証明書を「fd-service」ユーザーに追加します

  6. ブラウザーでポータル web ページを表示します

  7. PDF アイコンをクリックして、使用権限が適用された PDF ファイルとして XDP をレンダリングします。

recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e