Modulo adattivo basato su schema XSD
Se l’xdp non è basato su XSD, segui i seguenti passaggi per creare XSD(schema)su cui baserai il modulo adattivo
Genera dati di esempio per XDP
- Apri XDP in AEM Forms Designer.
- Fai clic su File | Proprietà modulo | Anteprima
- Fai clic su Genera dati di anteprima
- Fai clic su Genera
- Fornisci un nome file significativo, ad esempio "form-data.xml"
Generare XSD dai dati xml
Puoi utilizzare uno degli strumenti online gratuiti per generare XSD dai dati xml generati nel passaggio precedente.
Creare un modulo adattivo
Crea un modulo adattivo basato sull’XSD del passaggio precedente. Associa il modulo per utilizzare la libreria client "irs". Questa libreria client ha il codice per effettuare una chiamata POST al servlet che restituisce il PDF all’applicazione chiamante.
Il seguente codice viene attivato quando si fa clic su Scarica PDF
$(document).ready(function() {
$(".downloadPDF").click(function() {
window.guideBridge.getDataXML({
success: function(guideResultObject) {
var req = new XMLHttpRequest();
req.open("POST", "/bin/generateinteractivedor", true);
req.responseType = "blob";
var postParameters = new FormData();
postParameters.append("dataXml", guideResultObject.data);
postParameters.append("xdpName","f8918-r14e_redo-barcode_3 2.xdp")
postParameters.append("formBasedOnSchema", "true");
postParameters.append("dataNodeToExtract","afData/afBoundData/topmostSubform");
console.log(guideResultObject.data);
req.send(postParameters);
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
download(this.response, "report.pdf", "application/pdf");
}
}
}
});
});
});
Creare un servlet personalizzato
Crea un servlet personalizzato che unirà i dati con il modello XDP e restituirà il pdf. Il codice per eseguire questa operazione è elencato di seguito. Il servlet personalizzato fa parte del bundle AEMFormsDocumentServices.core-1.0-SNAPSHOT).
public class GenerateIInteractiveDor extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1 L;
@Reference
DocumentServices documentServices;
@Reference
FormsService formsService;
private static final Logger log = LoggerFactory.getLogger(GenerateIInteractiveDor.class);
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
doPost(request, response);
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
String xdpName = request.getParameter("xdpName");
boolean formBasedOnXSD = Boolean.parseBoolean(request.getParameter("formBasedOnSchema"));
XPathFactory xfact = XPathFactory.newInstance();
XPath xpath = xfact.newXPath();
String dataXml = request.getParameter("dataXml");
log.debug("The data xml is " + dataXml);
org.w3c.dom.Document xmlDataDoc = documentServices.w3cDocumentFromStrng(dataXml);
Document renderedPDF = null;
try {
if (!formBasedOnXSD) {
String xfaRootElement = request.getParameter("xfaRootElement");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
org.w3c.dom.Document newXMLDocument = dBuilder.newDocument();
Element rootElement = newXMLDocument.createElement(xfaRootElement);
String unboundData = "afData/afUnboundData/data";
Node dataNode = (Node) xpath.evaluate(unboundData, xmlDataDoc, XPathConstants.NODE);
NodeList dataChildNodes = dataNode.getChildNodes();
for (int i = 0; i<dataChildNodes.getLength(); i++) {
Node childNode = dataChildNodes.item(i);
if (childNode.getNodeType() == 1) {
Element newElement = newXMLDocument.createElement(childNode.getNodeName());
newElement.setTextContent(childNode.getTextContent());
rootElement.appendChild(newElement);
log.debug("the node name is " + childNode.getNodeName() + " and its value is " + childNode.getTextContent());
}
}
newXMLDocument.appendChild(rootElement);
Document xmlDataDocument = documentServices.orgw3cDocumentToAEMFDDocument(newXMLDocument);
String xdpTemplatePath = "crx:///content/dam/formsanddocuments";
com.adobe.fd.forms.api.PDFFormRenderOptions renderOptions = new com.adobe.fd.forms.api.PDFFormRenderOptions();
renderOptions.setAcrobatVersion(com.adobe.fd.forms.api.AcrobatVersion.Acrobat_11);
renderOptions.setContentRoot(xdpTemplatePath);
renderOptions.setRenderAtClient(com.adobe.fd.forms.api.RenderAtClient.NO);
renderedPDF = formsService.renderPDFForm(xdpName, xmlDataDocument, renderOptions);
} else {
// form is based on xsd
// get the actual xml data that needs to be merged with the template. This can be made more generic
String nodeToExtract = request.getParameter("dataNodeToExtract");
Node dataNode = (Node) xpath.evaluate(nodeToExtract, xmlDataDoc, XPathConstants.NODE);
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(dataNode), new StreamResult(writer));
String xml = writer.toString();
System.out.println(xml);
xmlDataDoc = documentServices.w3cDocumentFromStrng(xml);
Document xmlDataDocument = documentServices.orgw3cDocumentToAEMFDDocument(xmlDataDoc);
String xdpTemplatePath = "crx:///content/dam/formsanddocuments";
com.adobe.fd.forms.api.PDFFormRenderOptions renderOptions = new com.adobe.fd.forms.api.PDFFormRenderOptions();
renderOptions.setAcrobatVersion(com.adobe.fd.forms.api.AcrobatVersion.Acrobat_11);
renderOptions.setContentRoot(xdpTemplatePath);
renderOptions.setRenderAtClient(com.adobe.fd.forms.api.RenderAtClient.NO);
renderedPDF = formsService.renderPDFForm(xdpName, xmlDataDocument, renderOptions);
}
InputStream fileInputStream = renderedPDF.getInputStream();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=" + xdpName.replace("xdp", "pdf"));
response.setContentLength((int) fileInputStream.available());
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
responseOutputStream.flush();
responseOutputStream.close();
} catch (XPathExpressionException | TransformerException | FormsServiceException | IOException | ParserConfigurationException e) {
log.debug(e.getMessage());
}
}
}
In questo codice di esempio, il nome xdp e altri parametri vengono estratti dall’oggetto della richiesta. Se il modulo non è basato su un XSD, viene creato un nuovo documento XML da unire con l'XDP. Tuttavia, se il modulo è basato su XSD, il nodo pertinente viene estratto direttamente dai dati inviati del modulo adattivo e viene generato un documento XML per unirsi di conseguenza al modello xdp.