# 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  LVL 5 Asked: ###### 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. PowerShell DeveloperCommented: Where does it break for you at the moment? Just downloading it's required components. Chris 0 PowerShell 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. Chris [Reflection.Assembly]::LoadFrom("$($PWD.Path)\Assemblies\PdfSharp.dll") # 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$GFX.DrawString(
$Text,$Font,
[PdfSharp.Drawing.XBrushes]::Black,
(New-Object PdfSharp.Drawing.XRect(0, 0, $Page.Width,$Page.Height)),
[PdfSharp.Drawing.XStringFormats]::Center)

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

0
Author 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?
0
PowerShell 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.

Chris
0
Author 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  0 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. Commented: If you're still interested in a solution... this works for me. It requires using txt2pdf (free): http://www.verypdf.com/txt2pdf/index.htm Thanks, Joe $servername="FS1"
$cred="FS1\administrator"$TXT2PDF="c:\Program Files\Text2PDF v1.5\txt2pdf.exe"
$TxtFile="c:\in.txt"$PdfFile="c:\out.pdf"
$FontSize=8$decimalTextColor=255

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

0
###### 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
Powershell

From novice to tech pro — start learning today.