Blog
MuleSoft – Convert HTML, URLs, and Office Docs to PDF with MuleSoft
This blog post is a walk-through on how to generate PDFs from HTML, URLs, and Office Docs (word, excel, powerpoint, etc) from MuleSoft with the help of Api2Pdf.com. MuleSoft and it’s Anypoint Platform technology allows you to write API specifications in order to connect different applications together. Think of it as a tool that designs integrations.
One of those things you may need to integrate into your application is the generation of PDFs. For example, maybe you are a law firm that needs to batch generate massive amounts of PDFs when a certain trigger occurs in another system. Or maybe you are a point of purchase technology that needs to generate a receipt, invoice, or purchase order in PDF form. Either way, MuleSoft might be involved and it would help to have a PDF generated on the fly.
I created a video tutorial which you can watch here below, or skip to the step-by-step instructions.
Intro
I assume you already know how to use MuleSoft, and so the following guide starts with a blank Mule Palette. I drag over a Listener and configure it respond to any base path of /api/* at localhost:8082. This is just to demonstrate a fake API, but you will likely already have an API you are working with.
Get your API key
In order to generate PDFs with Api2Pdf.com, you need to acquire an API key. Go to https://portal.api2pdf.com to get your API key. It is free to get started.
Add the Request
Drag over a Request trigger from your list of Mule Connectors and attach it to your listener. We will use this Request to make the call to the Api2Pdf.com API to generate the PDF.
Configure the Request
It is time to configure the request to hit the Api2Pdf.com API. But in order to do that, we have to visit the Api2Pdf.com Documentation. There are 4 main endpoints available. Wkhtmltopdf, Headless Chrome, LibreOffice, and Merge PDFs. For this guide, we are going to generate a PDF from raw HTML to using wkhtmltopdf.
The endpoint for that is a POST call to https://v2018.api2pdf.com/wkhtmltopdf/html with a JSON body that requires at least an “html” attribute.
Additionally, the API key is passed through the Headers as “Authorization: YOUR-API-KEY”.
Let’s set this up in MuleSoft. See screenshot below.
We will leave the payload in the body as it will be passed through from when we make our call from a REST client like POSTMan or ARC. Tab over to Headers and add the authorization header. Make sure “Authorization” and the API KEY have quotes around them as demonstrated in the screenshot below.
Don’t worry, the API Key used in this example is burned.
Run the Request
Open your favorite REST client like POSTMan or ARC and call your fake MuleSoft API endpoint with a raw input JSON Body as the following:
{ “html”: “<p>Hello World from MuleSoft!</p>” }
It should fire off and generate the PDF and come back as a 200 OK.
Response
Going back to the documentation, you should receive a JSON response from the API. On a successful response, you will find the “pdf” attribute in the JSON body points to a URL that will be active for 24 hours. From there, you can download the PDF and store it locally or on some FTP server, or do anything else you need to do. That’s the magic of MuleSoft.
Cheers