Bestway to send Excel-sheet as downloadable link to external-system(Angular JS)
What are the best options to send a excel attachment as a downloadable link to external system(angular JS) from Pega7.?
Anugular JS has a Export2Excel Icon, when the user click on the export button, excel file needs to download and data needs to be send from pega.Currently i am sending one report results via service-REST as JSON response, AnjularJS showing these report results on that portalUI but not able to generate excel file.Then when user click on that export2excel icon same results need send as excel downloadable file.
Please let me know your thoughts to build this scenario.
So if I understand the requirement here ..... you have a (separate?) Web App which is built on the AngularJS framework.
You have implemented a (back-end?) call from your Web App which initiates a HTTP (REST) call to PRPC: PRPC responds with JSON data.
You are able to display/format this JSON data on your Web App with no issue.
Instead of (or I suspect: as well as) displaying the data on the Web App : you need to convert JSON to an EXCEL format.....
PRPC has several ways of creating data suitable for EXCEL : the simplest (used in the 'Export To Excel' button on PRPC Reports) is to provide HTML; but it is possible to generate native binary EXCEL formats in PRPC (using the Apache POI library) and also it is fairly easy to generate XML-based EXCEL formats using XSLT (or other XML transformation techniques).
Do you need JSON *and* an EXCEL format to be fetched from PRPC ? Or do you need a way of transforming JSON to an EXCEL format ?
I'm not quite sure of your setup here....but I can think of two basic variations here; can you let us know which (if any) fit your setup ?
1. Using PRPC as a BACKEND for the Web App - the Browser talks ONLY to the Web App - not PRPC
2. 'Mashing-up' Data from PRPC and the Web App - where the browser is fetching data from both PRPC and the Web App : possibly the Web App is also contacting PRPC for content as well ?
For the purposes of demonstration about the 'simplest' EXCEL format (which is actually HTML); you can do this by hand.
Create a simple HTML page, which contains a single TABLE like this:
Now here's the trick: re-save this with the extension '.xls'.
You can now double-click the file to launch in Excel - it will complain (because 'xls' doesn't actually reflect the right format, but whatever) - it will say:
"The file format and extension of 'table.xls' don't match. The file could be corrupted or unsafe. Unless you trust it's source, don't open it. Do you want to open it anyway ?"
But if you click 'Yes', it will open and display it in Excel:
The OOTB 'export-to-excel' in PRPC's reporting mechanism actually does some fancier formatting of data formats and styles than shown here - but it's the same principle.
So you *might* be able to get PRPC to build you two 'views' of your data : one in JSON (that is already working), and one (using the same input data, but just formatting differently) in HTML (EXCEL) format here - dunno.....
Then we have an Activity that performs a 'Show-Stream' on this rule - but before doing this, we have to set up HTTP headers to ensure the correct MIME type and also (in the case of accessing this from a BROWSER at least) sets the 'disposition' to 'attachment':
The 'ContextPage' is simply set to the same class as the where the ACTIVITY and the HTML file live:
This works from the Designer Studio:
But how do we access this directly over a single URL ?
This is what I tried:
I used the "Designer Studio > Integration > Services > Service Wizard" menu item from the Designer Studio:
And then selected the options:
Service Purpose= Invoke existing activity rules
Service Type = HTTP
Then chose my Activity Class:
Then chose my Activity:
Since in my case there are no parameters, I have skipped the non-interesting screenshots here....
Then I selected 'Configure a new service package':
Then on the review screen, switched OFF 'Requires Authentication?' and 'Suppress Show-HTML?'.
Switching off Authentication just makes testing easier: but it's not a secure thing to do (unless you have other systems in place to prevent people accessing your service) - so you will need to keep this switched on when doing this for real.
Then we create the rules involved.
Now (again, for Production Use, you wouldn't want to do this step) - we have to configure the Activity to allow it to run without Authentication:
Now to test this: we can string together a URL (this is documented in the help for HTTP Services) on the 'more' Tab) to call this HTTP Service, which looks like this for my example:
Rather than use Show-Stream, I would recommend using Property-Set-Stream to generate the Excel contents, then use the sendFile() method from the Public API to send the content to the browser. You can set your Content Type header as part of the aHTTPHeaders parameter in sendFile(). See Code-Pega-PDF.View for an example of how this works.