Export or Print Outlook Emails to PDF
Looks like there are no built-in methods, but if you're willing to use third-party binary, wkhtmltopdf can be used.
- Get precompiled binary (use MinGW 32-bit for maximum compatibility).
- Install or extract installer with 7Zip and copy
wkhtmltopdf.exe
to your script directory. It has no external dependencies and can be redistributed with your script, so you don't have to install PDF printer on all PCs. - Use HTMLBody property of
MailItem
object in your script for PDF conversion.
Here is an example:
# Get path to wkhtmltopdf.exe
$ExePath = Join-Path -Path (
Split-Path -Path $Script:MyInvocation.MyCommand.Path
) -ChildPath 'wkhtmltopdf.exe'
# Set PDF path
$OutFile = Join-Path -Path 'c:\path\to\emails' -ChildPath ($Email.Subject + '.pdf')
# Convert HTML string to PDF file
$ret = $Email.HTMLBody | & $ExePath @('--quiet', '-', $OutFile) 2>&1
# Check for errors
if ($LASTEXITCODE) {
Write-Error $ret
}
Please note, that I've no experience with Outlook and used MSDN to get relevant properties for object, so the code might need some tweaking.