cancel
Showing results for 
Search instead for 
Did you mean: 
antonkhrit

Create PDF document from HTML template based on dynamic data in Microsoft Flow

This article demonstrates how to generate PDF document from an HTML template with the help of Microsoft Flow.

 

I will use actions from Plumsail Documents connector, which is a part of Plumsail Actions.

 

We will firstly generate HTML document from a template. Then we will convert it to PDF. In this article, we will generate PDF invoice based on some data. This is how our final PDF file looks:

Result PDF file

Our template and result document have to be stored somewhere. Microsoft Flow has a lot of connectors for different systems. Here are just a few of them:

  • SharePoint
  • Salesforce
  • Box
  • OneDrive
  • Google Drive
  • Dropbox
  • SFTP
  • File System

You can store your files anywhere. In this example, we will store our documents in SharePoint. Our flow will use JSON object as a source data for the template, but you can get data from other sources. For example, query list items from SharePoint or from Salesforce.

 

This is how our flow looks:

Result text file

 

Here is step by step description for the flow.

 

Flow trigger

You can actually pick any trigger. For example, you can start Flow on file creation in a SharePoint document library. We use "Manually trigger a flow" trigger here to simplify the Flow.

 

Get file content

This action gets file content of the specified file from a SharePoint document library. You just specify SharePoint site URL and path to your file. We use this action to read HTML template.

You can use any other connector to get files from your system.

 

Create HTML from template

This is an action from Plumasail Documents connector, which is a part of Plumsail Actions. This action is suitable for generation of HTML and text documents.

You can find more information about this action here.

There are two parameters:

  1. Source HTML
  2. JSON

In the first parameter 'Source HTML' you can put raw HTML/text of a template or file content of a template from some other action. We specified the output of the previous action as a template. The internal file is quite large because of CSS styles. The template below is just a part of the template with a snippet for invoice items. Use this link to download the complete template.

<table>
    <thead\>
        <tr id="header_row">
            <th class="index_th">#</th>
            <th class="left details_th">Title</th>
            <th class="quantity_th">Quantity</th>
            <th class="unitprice_th">Unit Price ($)</th>
            <th class="subtotal_th">Subtotal ($)</th>
        </tr>
        </thead>
        <tfoot>
            <tr id="total_tr">
                <td colspan="2">&amp;nbsp;</td>
                <td colspan="2" class="total" id="total_currency"><span class="currency">$</span> Total </td>
                <td class="total"> ${{Total}} </td>
            </tr>
        </tfoot>
        <tbody>
            {{#each Items}} {{#with FieldValues}}
            <tr class="item">
                <td class="item_l"> {{#index}} </td>
                <td class="item_l"> {{Title}} </td>
                <td class="item_r"> {{Quantity}} </td>
                <td class="item_r"> {{UnitPrice}} </td>
                <td class="item_r"> {{SubTotal}} </td>
            </tr>
            {{/with}} {{/each}}
        </tbody>
</table>

You may see that there are placeholders like {{Total}} and {{Quantity}} in the template. There is also #{{each}} iterator for rendering invoice items. Please read template syntax description for more information.

In the second parameter, we specified data to apply to the template in JSON format. This object contains information for invoice header and for invoice items:

{
    "InvoiceDate": "10/02/2017",
    "InvoiceNum": 1,
    "Total": 1098,
    "ClientCompany": "Contoso ltd.",
    "ClientName": "John Doe",
    "ClientAddress": "55 East 52nd Street 21st",
    "ClientCity": "New York",
    "ClientRegion": "US",
    "ClientZip": "10022",
    "Items": [
        {
            "FieldValues": {
                "Quantity": 1,
                "SubTotal": 499,
                "Title": "Product Name #1",
                "UnitPrice": "499"
            }
        },
        {
            "FieldValues": {
                "Quantity": 1,
                "SubTotal": 599,
                "Title": "Product Name #2",
                "UnitPrice": 599
            }
        }
    ]
}

 

Convert HTML to PDF

This is also an action from Plumasail Documents connector.

Just put HTML file content from the output of the previous action and receive PDF file content as an output of this action. You can also use raw HTML string as a source HTML.

You can find more information about this action here.

 

Create file

Now you need to store text file somewhere. In our example, we use 'Create file' action from SharePoint connector to store the PDF document into SharePoint document library.

Select fields

You can use any other connector to store text document into your system.

 

There are more examples in Plumsail Actions documentation.

 

This post was originally posted here.

Comments
About the Author
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Read more about me and my achievements at: https://ganeshsanapblogs.wordpress.com/about MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, MSFT Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Founder of SKILLFUL SARDINE, a company focused on productivity and the Power Platform. You can find me on LinkedIn: https://linkedin.com/in/manueltgomes and twitter http://twitter.com/manueltgomes. I also write at https://www.manueltgomes.com, so if you want some Power Automate, SharePoint or Power Apps content I'm your guy 🙂
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 15 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/