Thursday 20 July 2023

Microsoft Print To PDF - Setting the Paper Size Preference using PowerShell

Introduction

A client's users would often use the Microsoft Print to PDF printer driver to create a PDF file of a document.  The user would then create a Word document and send this to a printer application.  The printer application GUI would allow the user to attach the PDF so that both the Word Document and the PDF document were sent to the printer application.  Both documents might would then be physically printed and sent to the customer.

In a number of cases this failed.  On further examination we could see the issue's cause by firstly double clicking on the Microsoft Print to PDF printer, in Control Panel\Devices and Printers, and then selecting Printing Preferences.


Secondly, clicking on Advanced in the Microsoft Print to PDF Preferences window.



We could then see that the Paper Size was set to Letter.  Changing this to A4 resolved the printing issue.


This fix worked nicely, however we could not apply it manually in this way to hundreds or thousands of devices.  How could we change the paper size preference on hundreds of devices in an automated manner?  A search of GPO options did not look promising.  And so I explored a number of scripting options.  

The Set-PrintConfiguration CmdLet Option

The following command should have corrected the issue for us but it did not.

set-printconfiguration -printername 'Microsoft Print to PDF' -papersize 'A4'


The printer's paper size preference was still set to Letter.

The PrintUI.exe Tool option

Another approach I tried was to manually set the paper size preference to A4 and then use the PrintUI.exe tool to create a migration export file. I could then use this export file to import the new paper size preference on the other devices.  The command to create the printer export file was as follows:

printui.exe /Ss /n "Microsoft Print to PDF" /a c:\MicrosoftPrintToPDF.printerexport

This created the named .printerexport file.  I was then able to import this file onto other devices and it did appear to work.  Here is the command used to import the settings onto another device.


printui.exe /Sr /n "Microsoft Print to PDF" /a c:\temp\MicrosoftPrintToPDF.printerexport u

 
I did have some success with this however testing showed it had to be executed under the logged on user context.  And then it would only work when the user had elevated administrator rights.  Normally I could script around this requirement using the execute-processasuser cmdlet from the PSAppDeployToolkit.  For instance:

execute-processasuser -path 'printui.exe' -parameters '/Sr /n "Microsoft Print to PDF" /a c:\temp\MicrosoftPrintToPDF.printerexport u'

It still would not work when deployed via Conifguration Manager to a user with non admin permissions.

The Dism Option

The method that worked reliably for this issue was the Dism Option.  Using the Dism I could uninstall the Microsoft To PDF printer and then re-install it.  This would result in the papersize preference being set to to the A4.  Here is the command to uninstall the printer:

Dism /Online /Disable-Feature /FeatureName:"Printing-PrintToPDFServices-Features"

And here is the command to install the printer once more.

dism /Online /Enable-Feature /FeatureName:"Printing-PrintToPDFServices-Features"

For those who like to use the PSApp Deployment Toolkit for their packaging wrapper, here is the section in the deploy-application.ps1 script that performs the fix.


The Exceptions

In most cases this all worked nicely.  However in some cases the paper size preference was still set to Letter after the printer was removed and then re-added.  In these cases I noticed that the language for non-unicode programs was set incorrectly.  This was set to English (United States) rather than English (United Kingdom).  This setting can be found in Control Panel\Region and the clicking on the Administrative tab.



It can be changed here by clicking on Change System Locale, but only if the user has admin rights.  A reboot is then required.  Obviously this also needed automation and so I found this was possible using the following PowerShell command

Set-winsystemlocale –systemlocale en-gb

Again, a reboot is required and Configuration Manager can be configured to force this.  Once this is done, then the dism commands can be used to correct the paper size preference.

Conclusion

This is a great example of how a simple requirement can often be quite complex to achieve.  But with some tenacity and patience, we can eventually find a way through and keep our client satisfied.  I hope you enjoyed reading this blog and wish you the same success with your printer configuration requirements.




No comments:

Post a Comment

Recover your corrupted Linux HP ThinPro Thin Client Device

Introduction While testing some functionality in my HP  T530 Thin Client's Linux based ThinPro 8 operating system, I found myself with a...