Expand my Community achievements bar.

SOLVED

PDF generation using XDP loosing Java script functionality in PDF

Avatar

Level 2

We are using XDP file and XML files to generate the PDF document.  We have check boxes in the PDF  and javascript included in the XDP file.  It is loosing the check box functionality in generated PDF.

 

 Code Snippet

 

 var file  = new java.io.File(templateFolder, pdfTemplate);
var inDoc = new com.adobe.aemfd.docmanager.Document(new java.io.FileInputStream(file));
 
    var xmlDoc = new com.adobe.aemfd.docmanager.Document(xmlDataPart.getInputStream());
var xciDoc = new com.adobe.aemfd.docmanager.Document(xciTemp);
 
    var pdfOptions  = new com.adobe.fd.output.api.PDFOutputOptions();
//Set PDF properties using XCI template
    pdfOptions.setXci(xciDoc);
 
    //Flatten PDF
    var outputService = sling.getService(Packages.com.adobe.fd.output.api.OutputService);
    var result = outputService.generatePDFOutput(inDoc, xmlDoc, pdfOptions);
1 Accepted Solution

Avatar

Correct answer by
Employee

Hi @ManjulaKo,

I can see that you are using the output service for generating the PDF. I can understand that you have some code which is executing when you click the checkboxes. However, please note that when using the output service to merge the XML with XDP, it is only designed to merge the data into the XDP and generate the PDF with the filled data. Having said that when you pass both the XML and XDP though the output service it only executes the events on the "Initialize event" and the "calculate event". If you have the scripts written on any other events it will not execute the same.

 

To resolve the problem, please move your code from the specific event to the calculate event and your issue should be resolved.

 

Thanks
Pranay

View solution in original post

15 Replies

Avatar

Correct answer by
Employee

Hi @ManjulaKo,

I can see that you are using the output service for generating the PDF. I can understand that you have some code which is executing when you click the checkboxes. However, please note that when using the output service to merge the XML with XDP, it is only designed to merge the data into the XDP and generate the PDF with the filled data. Having said that when you pass both the XML and XDP though the output service it only executes the events on the "Initialize event" and the "calculate event". If you have the scripts written on any other events it will not execute the same.

 

To resolve the problem, please move your code from the specific event to the calculate event and your issue should be resolved.

 

Thanks
Pranay

Avatar

Level 2

Thanks Pranay. I do have Events in the XDP file. Do we need to move these events code into the XML file? 

 

<event name="event__click" activity="click">
<script contentType="application/x-javascript">

form1.Page1_SP.presence = "visible";

xfa.host.currentPage = 0;
</script>

 

 Do I need to use <event name="event__click" activity="calculate">   instead of click?

Avatar

Employee

Hi @ManjulaKo,

You don't need to move the events to XML. Instead, as you mentioned, currently, you have the script written on the click event. You will need to move the code logic to calculate the event so that it is triggered at runtime.
In your XML you can simply pass the value for the checkbox as true or false and on the calculate event you just need to add a "if..else" block which checks if the checkbox is selected or not and then perform the specific action.

Thanks
Pranay

Avatar

Level 2

 Sure, I will move 'click' events to 'calculate' also, I do have 

<event activity="ready" and <event activity="change" events 

 Which events do I need to replace these two?

 Thanks

Manjula

Avatar

Employee

Hi @ManjulaKo,

If you want your events to execute when you pass the XML, you will need to write that code for the calculate event.

Thanks
Pranay

Avatar

Level 2

Thank you, Pranay. I will try 

Avatar

Level 2

It is not working I tried both the calculate and initialize events. Do you have any sample XDP file and XML file to try it out.

 

Avatar

Employee

Hi @ManjulaKo,

Sure, I have created a sample test artifact for you to test the usecase. I am attaching a sample XDP and a sample XML file. The form has a single checkbox which is used to show or hide a specific text box depending on the value of the checkbox. In the XML you should see a parameter:  <CheckBox1>1</CheckBox1> which you can select to determine the value of the checkbox.

I have also passed the same XDP and XML through the output service and attached the results as well.
Thanks
Pranay

Avatar

Level 2

Thank you, Pranay. I appreciate you sharing the sample files. I was able to set it up in my system and successful generate the PDF using the PDF generation code using AEM. However, the issue is with the generated PDF itself. When I open it, the checkbox is not selectable. I opened a generated PDF in Adobe PDF reader.

 

 Are you seeing the same behavior on your end? or is it correctly working for you?

 

 

Thanks

 

Avatar

Employee

Hi @ManjulaKo,

The PDF that i attached is the final output PDF, which is generated when you pass the XDP and the XML through the output service code[0] that you are using. Since the resultant output is a flat document you cannot make any selections on the PDF. If you want to see the dynamic version of the PDF, I have attachted the file to the case.

[0]   //Flatten PDF

    var outputService = sling.getService(Packages.com.adobe.fd.output.api.OutputService);
    var result = outputService.generatePDFOutput(inDoc, xmlDoc, pdfOptions);
 
Thanks
Pranay

Avatar

Level 2

I want to generate Dynamic version of PDF exactly that you attached.

 
 
var factory = new org.apache.commons.fileupload.disk.DiskFileItemFactory();
var pdfTemplate = request.getParameter("pdfTemplateName");
 
 
// Create a new file upload handler
var upload = new org.apache.commons.fileupload.servlet.ServletFileUpload(factory);
 
try {
 
    var xciPath = "/apps/template/embedFonts.xci";
    var resource = app.resourceResolver.getResource(xciPath);
    var node = resource.adaptTo(Packages.javax.jcr.Node);
var xciTemp = node.getNode("jcr:content").getProperty("jcr:data").getStream();
 
    var xmlDataPart = request.getPart("xml");
 
    var file  = new java.io.File(templateFolder, pdfTemplate);
var inDoc = new com.adobe.aemfd.docmanager.Document(new java.io.FileInputStream(file));
 
    var xmlDoc = new com.adobe.aemfd.docmanager.Document(xmlDataPart.getInputStream());
var xciDoc = new com.adobe.aemfd.docmanager.Document(xciTemp);
 
    var pdfOptions  = new com.adobe.fd.output.api.PDFOutputOptions();
//Set PDF properties using XCI template
    pdfOptions.setXci(xciDoc);
 
    //Flatten PDF
    var outputService = sling.getService(Packages.com.adobe.fd.output.api.OutputService);
    var result = outputService.generatePDFOutput(inDoc, xmlDoc, pdfOptions);
 
    if (result != null) {
        var inputStream = result.getInputStream();
    } else {
        inputStream = logDoc.getInputStream();
    }
 
    var outputStream = response.getOutputStream();
    org.apache.commons.io.IOUtils.copy(inputStream, outputStream);
    log = log + "copy complete";
 
inputStream.close();
    outputStream.close();
    outputStream.flush();
 
We have AEM 6.5.21 version 

Avatar

Level 2

The code I shared in the thread is not generating the Dynamic version of PDF, 

 While the one you shared appears clickable. Do you think the issue is related to the version?

Avatar

Employee

Hi @ManjulaKo,

The issue is not related to the code. The code that you have shared shows that you are using the Output Service[0] and its method to generate the PDF. When you use the output service, the resultant PDF is a flat PDF. If you want to create a dynamic PDF as a output then you should be using the Forms Service[1] which also takes the input as the XDP and XML but generates Dynamic PDF.

 

[0] 

//Flatten PDF
    var outputService = sling.getService(Packages.com.adobe.fd.output.api.OutputService);
    var result = outputService.generatePDFOutput(inDoc, xmlDoc, pdfOptions);
[1]
var formsService = sling.getService(com.adobe.fd.forms.api.FormsService.class);
interactivePDF = formsService.renderPDFForm(xdpName, xmlDoc, renderOptions);
Please find below the examples:
 
Thanks
Pranay

Avatar

Level 2

Thank you, Pranay. Appreciate your help. Let me try this solution .

Avatar

Level 2

Pranay,

 

 I've updated my code to use the FormsService to generate interactive PDF. When I setRenderAtClient to NO there are no errors in logs 

 

It is generating PDF but not interactive. I am using ECMA script in AEM version 6.5.21 with Java 11. Given that you are able to generate interactive PDF's do you think this issue could be related to AEM and java version I am using?

 

 

 

var uri = "crx:///content/dam/xdpFiles";
var pdfTemplate = request.getParameter("pdfTemplateName");
var pdfOptions  = new com.adobe.fd.forms.api.PDFFormRenderOptions();
    pdfOptions.setContentRoot(uri);
    pdfOptions.setXci(xciDoc);
pdfOptions.setRenderAtClient(com.adobe.fd.forms.api.RenderAtClient.NO;
 
//Interactive PDF
    var formsService = sling.getService(Packages.com.adobe.fd.forms.api.FormsService);
var result =   formsService.renderPDFForm(pdfTemplate, xmlDoc, pdfOptions);