How to create an invoice template

Viewed 44

Not sure if this is even possible, but what I'm looking to do create an invoice template via some SQL output and send them out as emails.

What I have tried so far is run a piece of SQL that contains all the details of an invoice, parse it as JSON, then create a csv table and lastly send them out via email with a CSV as an attachment.

The issue that I'm facing is that it's pretty basic; I would like to create a template of sorts and later converted to a PDF.

I'm moving away from SSRS and trying to use a logic app for the first time so not sure if this is something even achievable.

Appreciate any tips/help. Thanks in advance.

Regards

1 Answers

One way to achieve this is to use to Create CSV table with Custom Headers from Sql connector Get rows (V2) action and then convert it to Excel using 3rd party connector Plumsail's Csv to Excel action to transform the results of it into pdf using Convert Excel to PDF action of Adobe PDF services Connector.

Below is the flow of my logic app where I receive a sorted csv's pdf in my email attachments.

enter image description here

RESULTS:

TABLE IN SQL DATABASE

enter image description here

RESULT IN LOGIC APP

enter image description here

RESULT IN MAIL

enter image description here

To reproduce the same in your logic app, you can use below code view

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Convert_Excel_to_PDF": {
                "inputs": {
                    "body": {
                        "$content-type": "multipart/form-data",
                        "$multipart": [
                            {
                                "body": "EXCEL_TO_PDF",
                                "headers": {
                                    "Content-Disposition": "form-data; name=\"intent\""
                                }
                            },
                            {
                                "body": "pdfFile.xlsx",
                                "headers": {
                                    "Content-Disposition": "form-data; name=\"inputFileName\""
                                }
                            },
                            {
                                "body": "@body('Csv_to_Excel')",
                                "headers": {
                                    "Content-Disposition": "form-data; name=\"InputFile0\""
                                }
                            }
                        ]
                    },
                    "headers": {
                        "x-api-key": "PowerAutomate"
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['adobepdftools']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/operation/v1/createPDFFromExcel"
                },
                "runAfter": {
                    "Csv_to_Excel": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            },
            "Create_CSV_table": {
                "inputs": {
                    "columns": [
                        {
                            "header": "BillNo",
                            "value": "@item()?['billNo']"
                        },
                        {
                            "header": "OrderedDate",
                            "value": "@item()?['orderedDate']"
                        },
                        {
                            "header": "OrderValue",
                            "value": "@item()?['orderValue']"
                        }
                    ],
                    "format": "CSV",
                    "from": "@body('Get_rows_(V2)')?['value']"
                },
                "runAfter": {
                    "Get_rows_(V2)": [
                        "Succeeded"
                    ]
                },
                "type": "Table"
            },
            "Csv_to_Excel": {
                "inputs": {
                    "body": {
                        "content": "@{base64(body('Create_CSV_table'))}",
                        "hasHeaderRecords": true
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['plumsail_3']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/flow/v1/Documents/jobs/Csv2Xlsx"
                },
                "runAfter": {
                    "Create_CSV_table": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            },
            "Get_rows_(V2)": {
                "inputs": {
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['sql']['connectionId']"
                        }
                    },
                    "method": "get",
                    "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('default'))},@{encodeURIComponent(encodeURIComponent('default'))}/tables/@{encodeURIComponent(encodeURIComponent('[dbo].[Invoices]'))}/items",
                    "queries": {
                        "$orderby": "billNo asc"
                    }
                },
                "runAfter": {},
                "type": "ApiConnection"
            },
            "Send_an_email_(V2)": {
                "inputs": {
                    "body": {
                        "Attachments": [
                            {
                                "ContentBytes": "@{body('Convert_Excel_to_PDF')?['fileContent']}",
                                "Name": "@body('Convert_Excel_to_PDF')?['fileName']"
                            }
                        ],
                        "Body": "<p>This is a sample email</p>",
                        "Importance": "Normal",
                        "Subject": "Sample",
                        "To": "<TO_MAIL_ID>"
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['office365_1']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/v2/Mail"
                },
                "runAfter": {
                    "Convert_Excel_to_PDF": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Minute",
                    "interval": 3
                },
                "recurrence": {
                    "frequency": "Minute",
                    "interval": 3
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "adobepdftools": {
                    "connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/adobepdftools-4",
                    "connectionName": "adobepdftools-4",
                    "id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/adobepdftools"
                },
                "office365_1": {
                    "connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/office365-1",
                    "connectionName": "office365-1",
                    "id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/office365"
                },
                "plumsail_3": {
                    "connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/plumsail-3",
                    "connectionName": "plumsail-3",
                    "id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/plumsail"
                },
                "sql": {
                    "connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/sql",
                    "connectionName": "sql",
                    "id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/sql"
                }
            }
        }
    }
}

Related