- Create a HTML template for PDF generation, including variables and loops in Mustache format.
- Upload the template with a selected name via the REST interface.
- If you want to correct your template later, change it via the REST interface.
- Upload all required images and stylesheets as uploads in the AppServer.
- In the App:
- Generation of dynamic data.
- Upload the generated dynamic data under a unique session ID in the key-value store (via the REST interface of the key-value store).
The payload needs a unique key and the dynamic data you want to save.var payloadKeyValueStore = { key: "sessiondata/myTestApp/" + sap.ui.getCore().getModel().getData().sessionKey, content: btoa(JSON.stringify(sap.ui.getCore().getModel().getData())) ); this.callPlugin("keyValueStorePlugin", "puthttp", payloadKeyValueStore, callbackStoreSession, true, false, onKeyValueStoreFail)
- Optional: Uploading additional documents for merging into the key-value store (via the REST interface of the key-value store).
- Optional: Uploading a list of these document keys as a merge list in the key-value store (via the REST interface of the key-value store).
- Start of the PDF generation with the template name, the session ID and (optional) configuration data for the PDF generation via the REST interface.
var callbackStoreSession = function(data) { if(data.success) { var payloadGeneratePdf = { "template": "templatename", "session": "myTestApp/" + sap.ui.getCore().getModel().getData(). sessionKey, //"config": "(\"orientation\" : \"Portrait", \"page-size\" : \"A4\", \"margin-top\" : \"1in\", \"margin-bottom\" : \"1in\", //\"margin-left\" : \"1in\", \"margin-right\" : \"1in\", \"footer-center\" : \"[page] / [toPage]\"}\"}" }; busyDialog.setText("Generates requested PDF. Kindly have some patience."); $this.callPlugin("pdfPlugin", "generatePdf", payloadGeneratePdf, callbackGeneratePdf, true, false, onGeneratePdfFail); } };
- Remember the jobID.
- Asynchronous execution of the PDF generation (background operation).
- Call the Template.
- Call the dynamic data.
- Evaluate the data in the template.
- Call the static assets (images, stylesheets) from the AppServer.
- Convert the finished HTML-file in a PDF.
- Get the merge list and the merge binaries.
- Merge the created PDF with the merge files.
- Save the finished PDF in the key-value store.
- Call the key-value store via the REST interface with the restrained jobID to see, if the PDF has already been finished.
var jobId; var callbackGeneratePdf = function(data) { if (data.success) { jobId = { key: "pdf/"+data.value.jobId+".pdf" ); //timeout for demo purpose setTimeout(function() { $this.callPlugin("keyValueStorePlugin", "gethttp", jobId, callbackFetchPdf, true, false, onKeyValueStoreFail); },10000); } }; var callbackFetchPdf = function(data) { if(data.success) { var pdfData = data.result; window.open("data:application/pdf;base64,"pdfData); } };