Quick Response Code (QR Code) is a two dimensional (2D) bar code that is used to send simple text, URLs and encrypted code as an image and then scan it later to retrieve the information in the QR code. In this post, we will see how to generate a QR code in pega.
Steps to Generate QR Code in Pega
1. Download Google Zxing Barcode Java Library by clicking on this url : https://repo1.maven.org/maven2/com/google/zxing/core/2.2/core-2.2.jar
2. Import the downloaded Java library.
- In Pega, Click on Configure option on the top in Dev Studio starting from Pega 8 version or click on Designer Studio on top in Pega 7 to get all the configurable options in Pega.
- Go to Application –> Distribution –> Import and click on Import.
- Select the local file option and browse to the downloaded JAR file in your system and import it.
- Proceed to further steps by clicking on Next button
- The import of Google Zxing Core 2.2 Java library is successful
3. Shutdown and restart the Pega Server
4. Create a library in Pega (not always required) to store all the custom functions under one dedicated library for code usage.
- Go to Records –> Technical –> Library
- Create a new Library with “HR App Lib” in your application ruleset
- Click on save to create the library
5. Create a new function in pega which takes text as input and returns the base64 code for the QR Code.
- Go to Records –> Technical –> Function
- Create a new function with the name “Text to QRCode Base64” (you can rename as your wish) and select the library “HRAppLib” that we have just created in the previous step.
- Add a parameter name “text” with java type as “String”.
- Click on Create and Open button to create the function.
- Add Java code in the Java tab of the function
- Click on Save.
- Click on Checkout to make some changes in the function.
- In the Output section of the Parameters tab of the function, add Java Output type as “String” and Pega type as “Text”.
- In the Java tab of the function, Check the “Function ready to be compiled” checkbox and check in the function.
- Click on “Generate function” after the check in.
- Click on “Test Function Compilation” button.
6. Call the function in the data transform / activity by using the function name like @TextToQRCodeBase64(.pyLabel)
7. Display the base64 as an image using HTML section
<div>
<img src='data:image/png;base64, <pega:reference name=".ImageB64Code"/>' />
</div>