Powershell Output to PDF

I'm trying to combine a few scripts I've found on the internet into something usable but I'm still REALLY new to Powershell...  Can someone help me out.  I'm trying to take the output of a simple Get-WMIObject and output that to a PDF using some tools I found on the internet.  I get SO close but I just can't seem to get it...  Included is my script and the links to the add-ons i'm trying/would like to use...

I apologize for the looks.  Like I said, I'm really new to Powershell.

http://www.pdfsharp.net/ (what I'm using right now)
http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!340.entry?sa=24470815 (another good one but it balks at the output I was sending it).
http://dougfinke.com/blog/index.php/2008/08/13/powershell-script-new-pdf-made-possible-by-pdfsharp/ (and here is where I'm referencing the "New-PDF" from...)
get-wmiobject win32_logicaldisk -filter "drivetype=3" -computer servername -credential $cred | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | sort-object -property DriveID | format-table -autosize | .\New-PDF.ps1 -file .\test.txt -fileName c:\test.pdf

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:

Where does it break for you at the moment?

Just downloading it's required components.

Chris DentPowerShell DeveloperCommented:

Hmm well so far...

I've taken apart the script, it doesn't really meet our needs as it stands although it's a fine example for a starting point.

It's writing to the PDF file, although it's writing each line on top of the last. I think we might also run into problems with the font, the output we've got is rather expecting us to have fixed-width characters.

I wonder if we might be better going to HTML first, then to PDF. Assuming PDFSharp can do something like that... Requires a bit of playing.


# Create a new PDF document
$Document = New-Object PdfSharp.Pdf.PdfDocument

# Create an empty page
$Page = $Document.AddPage()

# Get an XGraphics object for drawing
$GFX = [PdfSharp.Drawing.XGraphics]::FromPdfPage($Page)

$Options = New-Object PdfSharp.Drawing.XPdfFontOptions("Unicode", "Always")

# Create a font
$Font = New-Object PdfSharp.Drawing.XFont("Arial", 12, "Bold", $Options)

# Draw the text
  (New-Object PdfSharp.Drawing.XRect(0, 0, $Page.Width, $Page.Height)),

#  Save the document...
$Document.Save( $Filename )

Open in new window

OverSeerAuthor Commented:
And yes, you found the same problem I was having.  It just overwrites itself...  

Well, I can always save the output to HTML and then pipe that into this right?
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Chris DentPowerShell DeveloperCommented:

It doesn't look like it, this one can't handle HTML at all. Or rather, you can send it in, but all you'll get is raw HTML written to the PDF (with the same overwriting problem). For it to make sense it would have to take the HTML, interpret it and apply appropriate formatting to the PDF.

You'd have to calculate the new position to draw each line. It's no fun at all although there are a fair number of samples available for PDFSharp on their website.

I'm sorry that's not much help, I don't think it's especially hard to find something for this, just fairly time-consuming.

OverSeerAuthor Commented:
Can't really find a way and I don't have the leisure of just playing at this for days.  Here's what I have and I can print to PDF after it comes out (not what I want but I'm posing it here for others...

Not pretty but functional.

gc c:\exchange.txt | ForEach-Object { Get-WmiObject -computer $_ Win32_LogicalDisk -Credential $cred} | Where-Object {$_.DriveType -eq 3} | Select-Object SystemName, DeviceID, VolumeName, @{Name="Size(GB)";expression={[math]::round(($_.Size / 1073741824))}}, @{Name="Free(GB)";expression={[math]::round(($_.FreeSpace / 1073741824))}}, @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Size / 1073741824)) * 100),0)}} | Sort-Object SystemName, DeviceID | ConvertTo-Html -body "<h2>Exchange Server Disk Space Report</h2>" -title "Exchange Server Disk Space Report" | Out-File c:\test.htm | Invoke-Item C:\Test.htm

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If you're still interested in a solution... this works for me.

It requires using txt2pdf (free):


$TXT2PDF="c:\Program Files\Text2PDF v1.5\txt2pdf.exe"

get-wmiobject win32_logicaldisk -filter "drivetype=3" -computer $servername -credential $cred | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | sort-object -property DriveID | format-table -autosize | Out-File -FilePath $TxtFile -Encoding Ascii

$cmd="& '$TXT2PDF' '$TxtFile' '$PdfFile' -pfs$FontSize -ptc$decimalTextColor" 
invoke-expression $cmd

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.