Skip to content

Instantly share code, notes, and snippets.

@tmsz
Created January 5, 2018 18:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tmsz/1491c1716b8e556279988f84fea8b241 to your computer and use it in GitHub Desktop.
Save tmsz/1491c1716b8e556279988f84fea8b241 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
# 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
*MinSize: PAIR(180000, 180000)
*MaxSize: PAIR(30276000 , 42804000)
*MaxPrintableWidth: 30276000
}')
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
@lin-ycv
Copy link

lin-ycv commented Nov 7, 2022

tried this on windows 10, but doesn't seem to work, breaks printer preferences
any ideas why?

@tmsz
Copy link
Author

tmsz commented Nov 14, 2022

@lin-ycv: No ideas. Any errors? Can you try adding a paper size manually using steps in this script? I'm not sure if this method is even up to date, as this script is 5 years old.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment