Sunday, June 5, 2016

Oracle MCS: Creating PDF files in Custom APIs

Oracle Mobile Cloud Service allows you to implement Custom API using node.js. This is a powerful feature as it allows you to use any of the existing node.js modules in your implementation.

In this post I am going to show you how to create and download a PDF file that will contain the weather information for the provided city, that we will get calling a REST Connector.

If you don't know how to create a REST Connector check this post: Oracle Mobile Cloud Service: Create an API that calls a REST web service

First we need to create a connector. This is the URL we are going to use:

Openweathermap is a free rest api where we can get the weather forecast.
We have to create some rules as we need to pass some parameters into the url. These are default values that we can override when calling the connector.

The next step is to create a Custom API.

We are going to allow annonymous calls to make it easier.

We need an endpoint with a parameter. This enpoint will have a GET method.

The last step is to implement our Custom API.
Now we need to download the JavaScript Scaffold.
At this point you need to have node.js installed (You can donwload if from this link)
After that we need to install pdfkit module.
Before executing 'npm install pdfkit' you need to be in the folder where package.json is.

After that node_modules folder will be created.
The next step is to edit package.json file to add the dependencies.
We need to add pdfkit and also we have to add the connector in oracleMobile dependencies.

PDFKit allows us to create PDF files and add text, images vector graphics, links, etc to our pdf.
In this example I am just creating one line (line 41) and inserting text based on the values returned by the connector.

We cannot test the API in MCS tester as the file cannot be downloaded, but we can use postman to do it.

We can see that the downloaded pdf contains Munich weather information for the next 3 days.

No comments:

Post a Comment