Create PDF from Sql with Powershell

Saving the results of a query to PDF would be a neat feature to have in SSMS – but second best is to have a small Powershell script to assist you.

I came across this article which contains a Powershell Module by Dr. Tobias Weltner that nicely wraps  PdfSharp-WPF.dll and then it was jus a matter of processing my own Sql (in this case a list of tables/columns) like so:

Import-Module E:\PowerShell\PDF\PDFTools

$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=LC7207SQL;Database=Watson; Integrated Security=true;”)
$query =
    “
    SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
    FROM INFORMATION_SCHEMA.COLUMNS
    ORDER BY TABLE_NAME, ORDINAL_POSITION
    ”
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter ($query, $connection)
$table = New-Object System.Data.DataTable
$adapter.Fill($table) | out-null

@($table) | Out-PTSPDF -Path $env:temp\report.pdf -Open -A -GroupBy TABLE_NAME

There are a number of parameters sto play with on the Out-PTSPDF module, and if required you can also pipe your data through a formatter, like Format-List, before PDF’ing it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s