Integrating Software AG webmethods.io and Microsoft Azure function app to generate PDF output files
Tags: integration, azure, tutorial
Published: 2021-09-02 14:07:49

---
title: Integrating Software AG webmethods.io and Microsoft Azure function app to generate PDF output files
published: true
date: 2021-03-02 06:55:18 UTC
tags: #integration, #azure, #tutorial
canonical_url: https://tech.forums.softwareag.com/t/integrating-software-ag-webmethods-io-and-microsoft-azure-function-app-to-generate-pdf-output-files/239332
---

## Introduction 
This tutorial describes how to generate PDF files in [webMethods.io](http://webMethods.io) using Microsoft Azure Function and html-pdf Node JS library. The use-case is to showcase how we can integrate [webMethods.io](http://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](http://webMethods.io) cloud instance  
• Access to Microsoft Azure with valid subscription – [https://azure.microsoft.com](https://azure.microsoft.com)  
• Knowledge on Html-PDF node js library - [html-pdf-node - npm](https://www.npmjs.com/package/html-pdf-node)

## Create Microsoft Azure Function App 
• Azure function helps accelerate and simplify application development with serverless compute. Read more - [Azure Functions Serverless Compute | Microsoft Azure](https://azure.microsoft.com/en-in/services/functions/)  
• Login to [https://portal.azure.com/](https://portal.azure.com/)  
• Search for Function App  
 ![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/1/1e6311f8fd07bb9f9a51ae677d54af885c90f53c.png)  
• 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

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/7/79a31701994cc8fa23500ef47a114f80512f1d4e.png)

• 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.

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/b/b1e9b3506f2e6a1d2bda9f8d438583626db351fa.png)

• 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](http://webMethods.io)

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/8/890a70f8aaee84348aacde152502636e9381c13b.png)

• Open Console, navigate to the newly created function and install html-pdf library

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/0/056831dcabab8e02fbbed31c67b93037f5eb33c7.png)

• Open developer console and use the below node JS code

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/1/18d566cec8bcb5f29da76f0b6fd280aeb1165206.png)

```
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

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/b/b76f7d24848b104f2c452b59b6bdcaf32c289a19.png)

• Copy the URL to invoke this function

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/7/72fd855161e27b2286d8130c03a99ed52a323d23.png)

## webMethods.io Workflow

In this example, I am considering a use case where we have JSON input.

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/1/1c4f08faa0c67fb9f9c8c96545be2423d1237b3d.png)

• 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

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/3/3c27581fb2936ca28ced84e884fc1431e5bf741b.png)

Please note to set response encoding to binary

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/1/1a5f76d266e2dedf2d2e5c4c2589674fef46cadc.png)

Also write the file with binary encoding

![image](https://aws1.discourse-cdn.com/techcommunity/original/2X/d/d05f9bc694b4a51d1235c6e3e3d0e0c10ef05869.png)

[Read full topic](https://tech.forums.softwareag.com/t/integrating-software-ag-webmethods-io-and-microsoft-azure-function-app-to-generate-pdf-output-files/239332)