Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
rahulsharma
Participant
1,411

Introduction:

In this blog post, we will walk through how to generate a PDF certificate using SAP Cloud Platform Integration (CPI) and send it as an email attachment. This approach can be used for various applications, including generating certificates of completion, awards, or any other document requiring dynamic content. We will be using Groovy scripts to dynamically create PDFs with personalized content.

Prerequisites:

  • Access to SAP CPI (Cloud Platform Integration).
  • Basic knowledge of Groovy scripting.
  • Basic understanding of HTML and PDF generation libraries (like PDFBox or Flying Saucer).
  • Mail sender configuration in SAP CPI.

Step 1: Setting Up the Integration Flow

  1. Create an Integration Flow in SAP CPI.
  2. Add the necessary artifacts, such as Groovy scripts, to handle the PDF generation and email sending.

I have created a normal iFlow just for testing, as this blog is on how to create certificate as a PDF so I assume you are having an idea on how to create an iFlow.

iFlow.png

In my Iflow I have added a Content Modifier which is containing many different fields as properties which I am going to use in my Groovy Script as well as in Mail adapter.

Properties.png

Step 2: Creating the Groovy Script to Generate the PDF

You will need to use Groovy to:

  • Generate dynamic content (such as name, date, and other certificate details).
  • Convert the HTML content into a PDF.

Before writing a Groovy Scripts you need to include specific external libraries because in SAP CPI, Groovy scripts don’t natively support PDF generation or manipulation or doesn't have built-in functionality for HTML-to-PDF conversion and many more reasons which you can learn on internet why do you have to use the following libraries.

1. flying-saucer-core-9.1.20

2. flying-saucer-core-9.1.20-javadoc

3. flying-saucer-core-9.1.20-sources

4. flying-saucer-pdf-9.1.20

5. fontbox-2.0.32

6. itext-2.1.7

7. pdfbox-2.0.32

8. pdfbox-io-3.0.3

Libraries.png

After adding all the libraries, you cann work on writing a Groovy Script. 

I am attaching a sample groovy script which I have used to creating a normal certificate, which you can edit as per your requirements.

 

 

 

 

import com.sap.gateway.ip.core.customdev.util.Message
import org.xhtmlrenderer.pdf.ITextRenderer
import java.io.ByteArrayOutputStream
import javax.mail.util.ByteArrayDataSource
import org.apache.camel.impl.DefaultAttachment

def Message processData(Message message) {
    // Fetch properties from the message
    String fullName = message.getProperty("FullName")
    String completionDate = message.getProperty("Date")
    String base64Image = message.getProperty("Image")

    // HTML structure for the certificate with dynamic content and embedded image at the top
    String certificateHTML = """
        <html>
        <head>
            <style>
                @font-face {
                    font-family: 'Noto Sans Hebrew';
                    src: url('data:font/woff2;base64,YOUR_BASE64_FONT_DATA') format('woff2');
                    font-weight: normal;
                    font-style: normal;
                }
                body {
                    font-family: 'Noto Sans Hebrew', sans-serif;
                    direction: ltr; /* Left-to-right for English */
                }
                .container {
                    width: 80%;
                    margin: 0 auto;
                    border: 1px solid #e1e1e1;
                    padding: 20px;
                    text-align: center;
                }
                .header {
                    font-size: 18px;
                    font-weight: bold;
                    margin-bottom: 10px;
                }
                .content {
                    text-align: left;
                    font-size: 16px;
                }
                .signature {
                    margin-top: 50px;
                    text-align: left;
                }
                .date {
                    text-align: right;
                    margin-top: 30px;
                    font-size: 14px;
                }
                .footer {
                    margin-top: 20px;
                    font-size: 12px;
                    color: #808080;
                    text-align: center;
                }
                .image-container {
                    margin-bottom: 20px; /* Add space below the image */
                    text-align: center;
                }
            </style>
        </head>
        <body>
            <div class="container">
                <div class="image-container">
                    <img src="data&colon;image/png;base64,${base64Image}" alt="Certificate Logo" width="100" height="100" />
                </div>
                <div class="header">
                    Certificate of Completion
                </div>
                <div class="content">
                    This is to certify that
                    <br /><br />
                    <b>${fullName}</b>
                    <br />
                    has successfully completed the following training:
                    <br />
                    Decision on Granting Ardalin
                </div>
                <div class="date">
                    Training completed on ${completionDate}
                </div>
                <div class="signature">
                    Approved by System Admin
                </div>
                <div class="footer">
                    Generated by System Admin
                </div>
            </div>
        </body>
        </html>
    """

    // Convert HTML to PDF using Flying Saucer with iText
    ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream()
    ITextRenderer renderer = new ITextRenderer()

    // Set the HTML content to the renderer
    renderer.setDocumentFromString(certificateHTML)
    renderer.layout()

    // Generate the PDF
    renderer.createPDF(pdfOutputStream)

    // Close the stream
    pdfOutputStream.close()

    // Create attachment for email
    def dataSource = new ByteArrayDataSource(pdfOutputStream.toByteArray(), "application/pdf")
    def attachment = new DefaultAttachment(dataSource)
    message.addAttachmentObject("certificate.pdf", attachment)

    return message
}

 

 

 

 

After adding the Script in your process, configure your mail adapter.

I am only showing the last part of it as I assume you know how to configure Mail Adapter.

Mail Adapter.png

Step 3: Testing and Deploying

  • Test the integration flow by triggering the process and verifying that the PDF certificate is correctly attached to the email.
  • Deploy the Integration Flow in SAP CPI.

In my case I have received a Mail and a certifacte like given in below images.

Mail.pngcertificatee.png

Conclusion:

By following these steps, you can generate a personalized certificate in PDF format and send it via email from SAP CPI. This technique can be expanded for various document generation and delivery use cases.

I hope you have learnt something new from this blog post. If you have any question, feel free to comment.

1 Comment
Labels in this area