[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4874
  • Last Modified:

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

  • 3
  • 2
1 Solution
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?
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

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

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


Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now