How to generate pdf file in webMethods

Overview :


Generating PDF files in webMethods is a valuable skill that enhances document management and facilitates seamless information sharing in diverse formats. In this article, we will delve into the intricacies of generating PDF files within the webMethods integration platform. In webMethods, there are no built-in services available for creating PDFs from scratch. Therefore, we’ll need to utilize Java services along with libraries to generate the PDF. We will utilize the Flying Saucer library along with the iText library to generate PDFs from HTML. This approach offers a convenient method for dynamically generating PDFs from HTML content. Creating HTML in webMethods can be achieved using XSLT. If you haven’t explored XSLT yet, you can find more information by clicking here.

Prerequisites :


Libraries used :


  • Flying Saucer (FlyingSaucer):
    • Flying Saucer is a Java library that converts HTML and CSS files into PDF documents. It is particularly useful for rendering HTML content accurately in PDF format.
  • iText:
    • iText is another Java library commonly used for working with PDF documents. It provides various functionalities for creating, manipulating, and editing PDF files programmatically.

Code Area :


Before you begin coding, ensure that you have downloaded the Flying Saucer PDF, Flying Saucer Core, and iText JAR files if you wish to follow along with the article and code. Alternatively, you can find the package along with the necessary JARs on GitHub.

Package set up:


  • Create a package with the below structure in your designer.
    • HG_PDFUtil
      • v1
        • util
  • Inside util create a java service and give it a proper name.
  • Copy all three JAR files and paste them into the “jar” directory of your package.
    Sample path: C:SoftwareAGIntegrationServerinstancesdefaultpackagesHG_PDFUtilcodejars, note this path can differ based on your installation directory.
  • Restart the integration server so that the jars can be loaded, If you’re unsure about the steps to restart the IS, please click here to review the instructions.
  • In Designer, navigate to the Java perspective and add these JAR files to the package build path. This is necessary because, during compile time, the system requires the JAR file locations; otherwise, you will encounter a “class not found” error in the Java service.

Write Java service:


  • Define three string inputs in the Java service and name them ‘htmlContent‘, ‘fileName‘, and ‘path‘.
  • Generate the code for the inputs using the generate code functionality in Designer and paste it into the Java service.
  • Import the below classes in your Java service.
    import org.xhtmlrenderer.pdf.ITextRenderer;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
  • In the share area of the java service write the below function which will help you to generate the PDF.
    public static void convertHtmlToPdf(String htmlContent, String outputPath) throws ServiceException {
        try {
            // Create the PDF file
            OutputStream os = new FileOutputStream(outputPath);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(htmlContent);
            renderer.layout();
            renderer.createPDF(os);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
    
    • Method Declaration:
      • public static void convertHtmlToPdf: Declares a static method named convertHtmlToPdf that takes two parameters (htmlContent and outputPath) of type String. The method has a void return type, and it declares that it may throw a ServiceException.
    • Try Block Start:
      • try: This keyword signifies the start of a block where you place code that might throw exceptions.
    • Creating OutputStream:
      • OutputStream os = new FileOutputStream(outputPath);: This line creates an OutputStream named os that writes to the specified file (outputPath). It’s used to prepare the destination for the PDF content.
    • Creating ITextRenderer:
      • ITextRenderer renderer = new ITextRenderer();: An instance of the ITextRenderer class from the iText library is created. This class is responsible for rendering HTML content into a PDF.
    • Setting HTML Content:
      • renderer.setDocumentFromString(htmlContent);: The HTML content (passed as a parameter htmlContent) is set to the ITextRenderer instance, preparing it for rendering.
    • Layout Calculation:
      • renderer.layout();: This line triggers the layout calculation, ensuring proper formatting and positioning of elements within the PDF.
    • Creating PDF:
      • renderer.createPDF(os);: The PDF is generated using the createPDF method of the ITextRenderer class. The output is directed to the previously created OutputStream
    • Catch Block Start:
      • catch (Exception e): This block handles exceptions of type Exception or any of its subclasses.
    • Throwing ServiceException:
      • throw new ServiceException(e);: If an exception occurs, it is caught, and a new ServiceException is thrown, wrapping the original exception. This helps propagate the exception up the call stack while providing a more specific exception type (ServiceException in this case).
  • Insert the below code in your java service to call the method that we have created with the parameters.
    convertHtmlToPdf(htmlContent, path+"/"+fileName);
  • We are done with the code. If you’ve followed each step, your Java service should now look like the following.
    package HG_PDFUtil.v1.util;
    
    import com.wm.data.*;
    import com.wm.util.Values;
    import com.wm.app.b2b.server.Service;
    import com.wm.app.b2b.server.ServiceException;
    import org.xhtmlrenderer.pdf.ITextRenderer;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    
    public final class writePDF_SVC
    
    {
    
    	/** 
    	 * The primary method for the Java service
    	 *
    	 * @param pipeline
    	 *            The IData pipeline
    	 * @throws ServiceException
    	 */
    	public static final void writePDF(IData pipeline) throws ServiceException {
    		// pipeline
    		IDataCursor pipelineCursor = pipeline.getCursor();
    			String	htmlContent = IDataUtil.getString( pipelineCursor, "htmlContent" );
    			String	fileName = IDataUtil.getString( pipelineCursor, "fileName" );
    			String	path = IDataUtil.getString( pipelineCursor, "path" );
    		pipelineCursor.destroy();
    		
    		// pipeline
    		convertHtmlToPdf(htmlContent, path+"/"+fileName);
    	}
    	
    	// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---
    	
    	public static void convertHtmlToPdf(String htmlContent, String outputPath)throws ServiceException {
    	       try {
    	           // Create the PDF file
    	               OutputStream os = new FileOutputStream(outputPath);
    	               ITextRenderer renderer = new ITextRenderer();
    	               renderer.setDocumentFromString(htmlContent);
    	               renderer.layout();
    	               renderer.createPDF(os);
    	               System.out.println("PDF file generated successfully: " + outputPath);
    	       } catch (Exception e) {
    	    	   throw new ServiceException(e);
    	       }
    	   }
    	
    	// --- <<IS-END-SHARED-SOURCE-AREA>> ---
    
    	final static writePDF_SVC _instance = new writePDF_SVC();
    
    	static writePDF_SVC _newInstance() { return new writePDF_SVC(); }
    
    	static writePDF_SVC _cast(Object o) { return (writePDF_SVC)o; }
    
    }

Test Java service:


  • Run the Java service with the given HTML content as htmlContent, and specify a file name of your choice for the PDF as fileName, along with the desired file path.
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>Invoice</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #f4f4f4;
                margin: 0;
                padding: 0;
            }
    
            .invoice {
                width: 80%;
                margin: 20px auto;
                padding: 20px;
                border: 1px solid #ddd;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                background-color: #fff;
            }
    
            .invoice-header {
                text-align: center;
                background-color: #3498db;
                color: #fff;
                padding: 10px;
            }
    
            .invoice-details {
                margin-top: 20px;
            }
    
            .invoice-items {
                margin-top: 20px;
                width: 100%;
                border-collapse: collapse;
            }
    
            .invoice-items, .invoice-items th, .invoice-items td {
                border: 1px solid #ddd;
            }
    
            .invoice-items th, .invoice-items td {
                padding: 10px;
                text-align: left;
            }
    
            .invoice-items th {
                background-color: #3498db;
                color: #fff;
            }
    
            .invoice-items td {
                background-color: #ecf0f1;
            }
    
            .invoice-details p {
                margin: 5px 0;
            }
        </style>
    </head>
    <body>
    
        <div class="invoice">
            <div class="invoice-header">
                <h1>Invoice</h1>
                <p>Date: March 2, 2024</p>
            </div>
    
            <div class="invoice-details">
                <p><strong>Bill To:</strong> John Doe</p>
                <p><strong>Address:</strong> 123 Main Street, Cityville</p>
            </div>
    
            <table class="invoice-items">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th>Quantity</th>
                        <th>Price</th>
                        <th>Total</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Product 1</td>
                        <td>2</td>
                        <td>$10.00</td>
                        <td>$20.00</td>
                    </tr>
                    <tr>
                        <td>Product 2</td>
                        <td>1</td>
                        <td>$15.00</td>
                        <td>$15.00</td>
                    </tr>
                </tbody>
            </table>
    
            <div class="invoice-details">
                <p><strong>Subtotal:</strong> $35.00</p>
                <p><strong>Tax (10%):</strong> $3.50</p>
                <p><strong>Total:</strong> $38.50</p>
            </div>
        </div>
    
    </body>
    </html>
    
  • Once you have successfully executed the service, you will be able to find the generated PDF file in your specified directory. Upon opening the file, you should be able to view the invoice.

Download source code from Git Hub.
GitHub

Hope you have enjoyed this article, Cheers!

 

Leave a Comment