How to generate PDF documents in IBM BPM / BAW

Sameh Ahmed
Sameh Nasef
Published in
4 min readApr 24, 2020

Introduction

One of the common requirements from BPM customers is generating a consumer document like an invoice or legal document PDFs based on IBM BPM process data and store the it within the content/Document management system.

There are several options , this tutorial will guide you toward the simplest way to do it.

1- Print a coach to PDF

This may be a direct solution that works for many situations, you can simply use your browser's built-in functionality to turn your coach into a PDF or have some script or control to print the coach . But I don’t find this a preferred solution as it requires to design and style your coach in a printer friendly format , It also requires maintenance and update of the process application if you need to change the document format or look and feel.

2- Generate a PDF based on a template

If you are creating a well-defined document such as an invoice, contract or a letter document, For sure the best way will be generating your PDF using a template. A key factor here is to find an easy way to visually design the template and to easily generate the document from the template within process flow.

In this tutorial, we will explore a quick and easy way using PDFGeneratorAPI.PDFGeneratorAPI.com allows you to design templates in the cloud and make a REST API call to create and retrieve documents.

You can get full implementation here https://ibm.box.com/v/baw-generate-pdf

A- Prepare the data object and the PDF template

After implementing your process/coach, prepare a json format for your process data that you will pass to the template for generating the variable . the easiest way is print out the json form of your BPM Process variable to console using client script

console.log(JSON.stringify(tw.local.MissionRequestInfo));

Sample output below , This sample will be used for designing the template

{"Subject": "Trip","Missions": [{"MissioninArabic": "Sameh","MissioninEnglish": "Sameh","StartDate": "2020-03-25","EndDate": "2020-03-30","Country": "FR","City": "Paris","Note": "Note"}],"Delegates": [{"PlayerName": "Sameh","PlayerNumber": "22","ArabicName": "sameh","others": ""}],"FederationNote": "","Status": ""}

Use pdfgeneratorapi.com to create your template, It will ask for the JSON file or json data object . use the sample object to assign data to different forms in the template

B- Implement a service flow to generate the document and store it to the ECM system

First step to have an service to call the pdfgenerator API to generate the pdf file. Unfortunately BPM/BAW discovery service is unable to discover the APIs published by pdfgeneratorapi (compatibility issues) , so I used swagger inspector to call the API and built API definition that can be discovered using BAW external service wizard. Please use the yaml file here or export it directly from swaggerhub https://app.swaggerhub.com/apis/samehn/GeneratePDF/0.1

Create an external service and let BAW discover the API and generate data objects

  • Call the external service as first step in the service flow. It will require your PDFGenerator apiKey, workspace , and signature . check here how to get you apikey and create your signature. https://docs.pdfgeneratorapi.com/#auth . Last parameter is pass the process variable object has the values you want to pass for the template to be generated. The response is the pdf file as base64 stream.
  • Create content stream object from the pdf base64 string
tw.local.contentStream = new tw.object.ECMContentStream();tw.local.contentStream.content = tw.local.data.response;tw.local.contentStream.contentLength =tw.local.data.response.length;tw.local.contentStream.mimeType = ‘application/pdf’;
  • Create a document in the ECM system using content integration task set the operation to Create Document and map the content stream object and document name. store the document ecmID in local variable
  • We will retrieve the document object to get the direct URL in the ECM system. So create a content integration task with operation get document and map the document ecmID returned in the previous step
  • use a server script Retrieve URL to extract the document URL
tw.local.urlOfPDF = tw.local.document.contentURL;

You can see a sample run of the scenario here

Conclusion

there are several approaches that can be used to solve this problem

Next tutorial, we will explore generating the PDF document using HTML template and a PDF libraries. There are several third-party libraries out there, including, PDFKit, and Apache PDFBox , iText

If you need assistance with any of these approaches, feel free to reach out for more information.

--

--