Powershell Output to PDF

OverSeer
OverSeer used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Where does it break for you at the moment?

Just downloading it's required components.

Chris
Chris DentPowerShell Developer
Top Expert 2010

Commented:

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 )

Open in new window

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?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Chris DentPowerShell Developer
Top Expert 2010

Commented:

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
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

Top Expert 2010

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

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial