How do I set printer settings without admin permissions of a printer job that prints a pdf or ps file?

I have been stuck on a problem for a while. My problem is in python but if you have example code for a solution in other languages I’m happy too.

This is one of my last attempts to get the pDevMode printer settings into the printer before printing it

import win32print

printer_name = win32print.GetDefaultPrinter()

hprinter = win32print.OpenPrinter(printer_name, None)
info = win32print.GetPrinter(hprinter, 2)["pDevMode"]
print(info.Orientation)
win32print.DocumentProperties(0, hprinter, printer_name, info, info, 5)
win32print.ClosePrinter(hprinter)
print(info.Orientation)
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ACCESS_USE, "pDevMode":info}
bprinter = win32print.OpenPrinter(printer_name, PRINTER_DEFAULTS)
fullinfo = win32print.GetPrinter(bprinter, 2)
print(fullinfo["pDevMode"].Orientation)
win32print.ClosePrinter(bprinter)

I know it looks weird but the idea is to get the pDevMode from the printer, saving the new setting to “info” and then opening a new printer handle where the pDevMode is set directly into the Printer_Defaults as I don’t require any admin perms for that. However, “fullinfo[“pDevMode”]” shows that it didn’t save the new settings set with DocumentProperties

Another attempt looked something like this

import win32print


print(win32print.GetDefaultPrinter())
file_path="test.ps"
printer_name = win32print.GetDefaultPrinter()
file_handle = open(file_path, 'rb')
printer_handle = win32print.OpenPrinter(win32print.GetDefaultPrinter())
info = win32print.GetPrinter(printer_handle, 2)["pDevMode"]


if (win32print.DocumentProperties(0, printer_handle, printer_name, info, info, 5) == 1):
    job = win32print.StartDocPrinter(printer_handle,1,("postscripttest",None,"RAW"))
    print(win32print.GetJob(printer_handle, job))
    win32print.WritePrinter(printer_handle,file_handle.read())
    win32print.EndDocPrinter(printer_handle)
    win32print.ClosePrinter(printer_handle)
else:
    print("Process Cancled")

This was an earlier attempt but it doesn’t really use “info” at all. I was just hoping that it would set the settings with DocumentProperties and it does include them into the “info” variable but doesn’t do anything for printing it.

I have been looking up this problem for over a week now and am completely lost at how to solve it. I still can’t believe that it’s hard since there are hundreds of applications out there that use printing with settings without any admin permissions. If you know a solution python specifically that would be awesome but any example code that shows how to use api calls accordingly to change the settings without admin perms is fine in any language if you have that. Any help is highly appreciated!!

  • using native scripts most tasks will be possible so in windows dir look for the vbs files for printer controls however setting forms is a different set of customisation and for set and unset printers there is a command line printui /? can be run using exe commands

    – 




Leave a Comment