Using HTML to create PDF Reports (Part 1)

by Oct 5, 2022

HTML can be a simple way of formatting data for output reports. In this 3-part series, we first illustrate how you compose HTML reports, then show a simple way to turn these HTML reports into PDF documents.

PowerShell comes with the ConvertTo-Html cmdlet which makes it easy to save output to HTML tables:

$path = "$env:temp\report.html"

# get data from any cmdlet you wish
$data = Get-Service | Sort-Object -Property Status, Name

# output to HTML
$data | ConvertTo-Html | Set-Content -Path $path -Encoding UTF8

Invoke-Item -Path $path

The resulting report may still be ugly but adding a HTML style sheet can beautify the report easily and add your corporate design:

$path = "$env:temp\report.html"

# get data from any cmdlet you wish
$data = Get-Service | Sort-Object -Property Status, Name

# compose style sheet
$stylesheet = "
<style>
body { background-color:#AAEEEE;
font-family:Monospace;
font-size:10pt; }
table,td, th { border:1px solid blue;}
th { color:#00008B;
background-color:#EEEEAA; 
font-size: 12pt;}
table { margin-left:30px; }
h2 {
font-family:Tahoma;
color:#6D7B8D;
}
h1{color:#DC143C;}
h5{color:#DC143C;}
</style>
"

# output to HTML
$data | ConvertTo-Html -Title Report -Head $stylesheet | Set-Content -Path $path -Encoding UTF8

Invoke-Item -Path $path

Twitter This Tip! ReTweet this Tip!