How to generate PDF invoice from HTML template for HTML to PDF API in PowerShell using Web API

How to generate PDF invoice from HTML template for HTML to PDF API in PowerShell: Step By Step Tutorial

We’ve created and updating regularly our sample code library so you may quickly learn HTML to PDF API and the step-by-step process in PowerShell. Web API was made to help with HTML to PDF API in PowerShell. Web API is the flexible Web API that includes full set of functions from e-signature requests to data extraction, OCR, images recognition, pdf splitting and pdf splitting. Can also generate barcodes and read barcodes from images, scans and pdf.

PowerShell code snippet like this for Web API works best when you need to quickly implement HTML to PDF API in your PowerShell application. Open your PowerShell project and simply copy & paste the code and then run your app! You can use these PowerShell sample examples in one or many applications.

Our website provides free trial version of Web API that includes source code samples to help with your PowerShell project.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)


# The authentication key (API Key). # Get your own by registering at $API_KEY = "***********************************" # HTML template $Template = [IO.File]::ReadAllText(".\invoice_template.html") # Data to fill the template $TemplateData = [IO.File]::ReadAllText(".\invoice_data.json") # Destination PDF file name $DestinationFile = ".\result.pdf" # Prepare URL for HTML to PDF API call $query = "$(Split-Path $DestinationFile -Leaf)" $query = [System.Uri]::EscapeUriString($query) # Prepare request body (will be auto-converted to JSON by Invoke-RestMethod) $body = @{ "html" = $Template "templateData" = $TemplateData } | ConvertTo-Json try { # Execute request $response = Invoke-WebRequest -Method Post -Headers @{ "x-api-key" = $API_KEY; "Content-Type" = "application/json" } -Body $body -Uri $query if ($response.StatusCode -eq 200) { $jsonResponse = $response.Content | ConvertFrom-Json if ($jsonResponse.error -eq $false) { # Get URL of generated PDF file $resultFileUrl = $jsonResponse.url; # Download PDF file Invoke-WebRequest -Headers @{ "x-api-key" = $API_KEY } -OutFile $DestinationFile -Uri $resultFileUrl Write-Host "Generated PDF file saved as `"$($DestinationFile)`" file." } else { # Display service reported error Write-Host $jsonResponse.message } } else { # Display request error status Write-Host $response.StatusCode + " " + $response.StatusDescription } } catch { # Display request error Write-Host $_.Exception }


{ "number": "1234567", "date": "April 30, 2016", "from": "Acme Inc., City, Street 3rd , +1 888 123-456,", "to": "Food Delivery Inc., New York, Some Street, 42", "lines": [{ "title": "Setting up new web-site", "quantity": 3, "price": 50 }, { "title": "Configuring mail server and mailboxes", "quantity": 5, "price": 50 }] }


@echo off powershell -NoProfile -ExecutionPolicy Bypass -Command "& .\GeneratePdfInvoiceFromHtmlTemplate.ps1" echo Script finished with errorlevel=%errorlevel% pause



Get 60 Day Free Trial

See also:


Get Your API Key

See also: