Forked from tmsz/MSPrintToPDF_AllowCustomSizes.ps1
Last active
March 31, 2019 17:15
-
-
Save ner00/245a702ac9e4d48566aab336eb9e2463 to your computer and use it in GitHub Desktop.
A script that allows adding custom paper sizes in (forms) through Printer Server Manager for Microsoft Print To PDF virtual printer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adds custom paper sizes to Windows 10 'Microsoft print to PDF' virtual printer. | |
# Based on instructions from: | |
# https://answers.microsoft.com/en-us/windows/forum/windows_10-hardware/microsoft-print-to-pdf-custom-paper-sizes-possible/90ed3d48-1ece-4ca5-8d3b-ff0af24a7b37?auth=1 | |
# https://franklinheath.co.uk/2015/08/29/custom-page-sizes-for-microsoft-print-to-pdf/ | |
# https://onedrive.live.com/?authkey=%21ABebdrLzeOY7sa4&cid=A34A14B10D9BB9A1&id=A34A14B10D9BB9A1%21854&parId=A34A14B10D9BB9A1%21838&o=OneUp | |
# This allows to use custom size forms. | |
# https://technet.microsoft.com/en-us/library/dd759110(v=ws.11).aspx | |
$GPDCustomSizeMatch = '*DefaultOption: LETTER' | |
$GPDCustomSizeInsert = ( | |
'*Option: CustomSize | |
{ | |
*rcNameID: =USER_DEFINED_SIZE_DISPLAY | |
*PrintSchemaKeywordMap: "CustomSize" | |
*PrintableOrigin: PAIR(0, 0) | |
*PrintableArea: PAIR(30276000, 42804000) | |
}') | |
function InsertText { | |
# Function for inserting content into text files. | |
# 'file' is path to file | |
# 'match' is a line to add text after | |
# 'conent' text to add | |
Param ($filepath, $match, $content) | |
$NewContent = Get-Content -Path $filepath | | |
ForEach-Object { | |
if($_ -match ('^' + $match)) | |
{ | |
$_ | |
$content | |
} | |
else | |
{ | |
$_ | |
} | |
} | |
$NewContent | Out-File -FilePath $filepath -Encoding Default -Force | |
} | |
# Gets the location of the printer configuration folder. | |
$printerKey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\Microsoft Print to PDF' | |
$printerGuid = (Get-ItemProperty -Path $printerKey -Name 'PrintQueueV4DriverDirectory').PrintQueueV4DriverDirectory | |
$printerPath = $env:windir + "\System32\spool\V4Dirs\" + $printerGuid | |
# Creates a backup of all files in folder. | |
$date = Get-Date -Format FileDateTime | |
$backupDestination = $printerPath + " " + $date +".zip" | |
Add-Type -assembly "system.io.compression.filesystem" | |
[io.compression.zipfile]::CreateFromDirectory($printerPath, $backupDestination) | |
# Folder clean up. | |
Remove-Item ($printerPath + "\merged.gpd") -ErrorAction SilentlyContinue | |
Remove-Item ($printerPath + "\*.bud") -ErrorAction SilentlyContinue | |
Remove-Item ($printerPath + "\gpc.xml") -ErrorAction SilentlyContinue | |
# Gets the GPD printer configuration file. | |
$GPDKey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\Microsoft Print to PDF\PrinterDriverData' | |
$configFileName = (Get-ItemProperty -Path $GPDKey -Name 'V4_Merged_ConfigFile_Name').V4_Merged_ConfigFile_Name | |
$GPDFilepath = $printerPath + '\' + $configFileName | |
InsertText $GPDFilepath $GPDCustomSizeMatch $GPDCustomSizeInsert |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment