How to generate PDF invoice from HTML template for HTML to PDF API in PowerShell using PDF.co 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. PDF.co Web API was made to help with HTML to PDF API in PowerShell. PDF.co 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 PDF.co 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 PDF.co 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)

GeneratePdfInvoiceFromHtmlTemplate.ps1

      
# The authentication key (API Key). # Get your own by registering at https://app.pdf.co/documentation/api $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 = "https://api.pdf.co/v1/pdf/convert/from/html?name=$(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 }

invoice_data.json

      
{ "number": "1234567", "date": "April 30, 2016", "from": "Acme Inc., City, Street 3rd , +1 888 123-456, support@example.com", "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 }] }

run.bat

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

VIDEO

ON-PREMISE OFFLINE SDK

Get 60 Day Free Trial

See also:

ON-DEMAND REST WEB API

Get Your API Key

See also: