Introduction
This tutorial describes how to generate PDF files in webMethods.io using Microsoft Azure Function and html-pdf Node JS library. The use-case is to showcase how we can integrate webMethods.io with Microsoft Azure function. Html-pdf node js is one example, there are multiple other libraries in different programming languages to generate PDF files. You can use the right one as per your requirement.
Prerequisite
• Have access to webMethods.io cloud instance
• Access to Microsoft Azure with valid subscription – https://azure.microsoft.com
• Knowledge on Html-PDF node js library - html-pdf-node - npm
Create Microsoft Azure Function App
• Azure function helps accelerate and simplify application development with serverless compute. Read more - https://azure.microsoft.com/en-in/services/functions/
• Login to https://portal.azure.com/
• Search for Function App
• Click New to start creating a new Function App
• Select Appropriate Subscription, Resource Group, App name.
- Select runtime stack as Node.js
- Version 10 LTS
- Region as per your choice
• Click Next to setup Hosting. It is important to note that, to execute html-pdf node.js library, you need to select minimum Basic B1 App service plan. Consumption plan will not work.
• Create the Azure Function App
Setup the created Microsoft Azure Function App
After successful deployment of the Azure function, you need to setup the node js code.
• Navigate to the newly created function App and create new function. Select HTTP trigger so that this function can be invoked from webMethods.io
• Open Console, navigate to the newly created function and install html-pdf library
• Open developer console and use the below node JS code
var pdf = require('html-pdf');
module.exports = async function (context, req) {
const html=(req.query.name || (req.body && req.body.name));
var data = await returnHtmlAsPdf(html);
var data2 = []
data2.push(data);
context.res = {
setEncoding: 'binary',
// status: 200, /* Defaults to 200 */
body: Buffer.concat(data2)
};
context.done();
};
async function returnHtmlAsPdf(html) {
return new Promise((resolve, reject) => {
pdf.create(html).toBuffer(function(err, buffer){
if(err){
reject(err);
}
resolve(buffer);
})
});
}
• You will also need to include package.json file in root directory. Sample below
{
"name": "PdfFunc",
"version": "",
"description": "",
"scripts": {
"test": "echo \"No tests yet...\""
},
"author": "",
"dependencies": {
"html-pdf": "^2.2.0"
}
}
• This is how your final project structure looks like
• Copy the URL to invoke this function
webMethods.io Workflow
In this example, I am considering a use case where we have JSON input.
• The first step is to convert JSON input to HTML
• Use HTTP Request Connector to invoke the Azure Function App by passing the HTML as input
• The response from the Azure function App will be PDF byte array
• Write the response to file and send the file in Email as attachment.
HTTP connector settings
Please note to set response encoding to binary
Also write the file with binary encoding