Skip to content

Instantly share code, notes, and snippets.

@signalwarrant
Created August 9, 2019 20:17
Show Gist options
  • Select an option

  • Save signalwarrant/dc6a7c30a6ae3a4886a27ad0976ee7cc to your computer and use it in GitHub Desktop.

Select an option

Save signalwarrant/dc6a7c30a6ae3a4886a27ad0976ee7cc to your computer and use it in GitHub Desktop.
Convert text files to PDF
Function ConvertTo-PDF {
<#
.DESCRIPTION
Convert 1 or many files to PDFs
.PARAMETER filePath
-filePath: The path to the folder that contains all your text files
.PARAMETER dllPath
-dllPath: The Path to the iTextSharp.DLL file
.EXAMPLE
ConverTTo-PDF -filePath 'C:\help' -filetype 'txt' -dllPath 'C:\itextsharp.dll'
.REQUIREMENTS
- iTextSharp 5.5.10 .NET Library
- You may have to Set execution policy to less restrictive policy
.LINK
iTextSharp .NET library: https://github.com/itext/itextsharp/releases/tag/5.5.11
#>
Param(
[Parameter(
Mandatory=$True,
HelpMessage='Add the path you want to save help to EX. c:\help'
)][string]$filePath,
[Parameter(
Mandatory=$True,HelpMessage='What file type to convert'
)][string]$filetype,
[Parameter(
Mandatory=$True,HelpMessage='path to the itextsharp.dll file EX. c:\itextsharp.dll'
)][string]$dllPath
)
Begin{
Try{
Add-Type -Path $dllPath -ErrorAction Stop
}
Catch{
Throw "Could not load iTextSharp DLL from $($dllPath).`nPlease check that the dll is located at that path."
}
}
Process{
$txtFiles = Get-ChildItem -Path $filePath -Recurse -Filter "*.$filetype"
ForEach ($txtFile in $txtFiles){
$path = "$($txtFile.DirectoryName)\$($txtFile.BaseName).pdf"
$doc = New-Object -TypeName iTextSharp.text.Document
$fileStream = New-Object -TypeName IO.FileStream -ArgumentList ($path, [System.IO.FileMode]::Create)
[iTextSharp.text.pdf.PdfWriter]::GetInstance($doc, $filestream)
[iTextSharp.text.FontFactory]::RegisterDirectories()
$paragraph = New-Object -TypeName iTextSharp.text.Paragraph
$paragraph.add(( Get-Content -Path $($txtFile.FullName) |
ForEach-Object {
"$_`n"
})) | Out-Null
$doc.open()
$doc.add($paragraph) | Out-Null
$doc.close()
}
}
}
Function Export-PShelp {
<#
.SYNOPSIS
Exports the -full help for each CMDlet available on your computer.
.DESCRIPTION
Gets all the Modules available on the computer, loops through those Modules to retrieve each CMDlet name as well as create a folder for each module. Loops through each CMDlet in each module and exports the -full help for each to the $filepath\$modulename
.PARAMETER filePath
-filePath: The folder that you're exporting all the help files to on your local hard drive.
.EXAMPLE
Export-PShelp -filePath 'c:\helpfiles'
.NOTES
.LINK
#>
Param(
[Parameter(
Mandatory=$True,HelpMessage='Path where you want the helpfiles to be exported',
Position=1
)][string]$filePath
)
If(!(Test-Path -Path "$filePath\")){
New-Item -Path $Filepath -Name Help -ItemType Directory
}
# You get some errors if a module has no help so I just
# turned error reporting off.
$ErrorActionPreference = 'silentlycontinue'
# Get each module name and loop through each to retrieve cmdlet names
$modules = Get-Module -ListAvailable |
Select-Object -ExpandProperty Name
ForEach ($module in $modules){
# Creates a folder for each Module
If (!(Get-Item -Path "$filePath\$($module)")){
New-Item -ItemType Directory -Path "$filePath\$($module)"
}
# Get the CMDLet names for each CMDlet in the Module
$modulecmdlets = Get-Command -Module $module |
Select-Object -ExpandProperty name
ForEach ($modulecmdlet in $modulecmdlets){
Get-Help -Name $($modulecmdlet) -Full |
Out-File -FilePath "$filePath\$($module)\$($modulecmdlet).txt"
}
}
}
##############################################################
# Merges all the PDF files for each Module in to 1 PDF file per
# module called Help_<moduleName>.pdf in $filepath
##############################################################
$folders = Get-ChildItem -Path $filePath -Directory
$ErrorActionPreference = 'silentlycontinue'
foreach ($folder in $folders){
$pdfs = Get-ChildItem -Path $folder.fullname -recurse -Filter '*.pdf'
[void] [System.Reflection.Assembly]::LoadFrom(
[System.IO.Path]::Combine($filePath, $dllPath)
)
$output = [System.IO.Path]::Combine($filePath, "Help_$($folder[0].Name).pdf")
$fileStream = New-Object -TypeName System.IO.FileStream -ArgumentList ($output, [System.IO.FileMode]::OpenOrCreate)
$document = New-Object -TypeName iTextSharp.text.Document
$pdfCopy = New-Object -TypeName iTextSharp.text.pdf.PdfCopy -ArgumentList ($document, $fileStream)
$document.Open()
foreach ($pdf in $pdfs) {
$reader = New-Object -TypeName iTextSharp.text.pdf.PdfReader -ArgumentList ($pdf.FullName)
$pdfCopy.AddDocument($reader)
$reader.Dispose()
}
$pdfCopy.Dispose()
$document.Dispose()
$fileStream.Dispose()
}
Function Merge-PDFs{
<#
.SYNOPSIS
Merges PDF files into 1 PDF file.
.PARAMETER filePath
-filePath: Any PDF file in the filepath will be combined into 1 PDF file named All_PowerShell_Help.pdf.
.PARAMETER dllPath
-dllPath: The Path to the iTextSharp.DLL file
.EXAMPLE
Merge-PDFs -filePath 'c:\scripts\help' -dllPath 'c:\scripts\itextsharp.dll'
Describe what this call does
.NOTES
Modified Code from here
http://geekswithblogs.net/burncsharp/archive/2007/04/13/111629.aspx
#>
Param(
[Parameter(
Mandatory=$True,
HelpMessage='Add the path you want to save help to EX. c:\help'
)][string]$filePath,
[Parameter(
ValueFromPipelinebyPropertyName=$true
)][string]$dllPath
)
Begin{
Try{
Add-Type -Path $dllPath -ErrorAction Stop
}
Catch{
Throw "Could not load iTextSharp DLL from $($dllPath).`nPlease check that the dll is located at that path."
}
}
Process{
##############################################################
# Merges all the PDF files for each Module in to 1 PDF file
# called All_PowerShell_Help.pdf in $filepath
# 21,000 pages to maybe a second, iTextSharp is fast.
##############################################################
$pdfs = Get-ChildItem -Path $filePath -Recurse -Filter '*.pdf'
$ErrorActionPreference = 'silentlycontinue'
[void] [System.Reflection.Assembly]::LoadFrom(
[System.IO.Path]::Combine($filePath, $dllPath)
)
$output = [System.IO.Path]::Combine($filePath, 'All_PowerShell_Help.pdf')
$fileStream = New-Object -TypeName System.IO.FileStream -ArgumentList ($output,[System.IO.FileMode]::OpenOrCreate)
$document = New-Object -TypeName iTextSharp.text.Document
$pdfCopy = New-Object -TypeName iTextSharp.text.pdf.PdfCopy -ArgumentList ($document, $fileStream)
$document.Open()
foreach ($pdf in $pdfs) {
$reader = New-Object -TypeName iTextSharp.text.pdf.PdfReader -ArgumentList ($pdf.FullName)
$pdfCopy.AddDocument($reader)
$reader.Dispose()
}
$pdfCopy.Dispose()
$document.Dispose()
$fileStream.Dispose()
#############################################################
# Find all directories in $filepath that are empty and delete
# them. Some directories created for DSC Resources will be empty
#############################################################
(Get-ChildItem -Path $filePath -Recurse |
Where-Object {$_.PSIsContainer -eq $True}) |
Where-Object {$_.GetFiles().Count -eq 0} |
Remove-Item
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment