Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching Printer Trays

Tags:

python

winapi

I know this question has been asked before, but there was no clear answer.

How do I change the printer tray programmatically?

I am trying to use python to batch print some PDFs. I need to print different pages from different trays. The printer is a Ricoh 2232C. Is there a way to do it through and Acrobat Reader command line parameter? I am able to use the Win32 api to find out which bins correspond to which binnames, but that is about it. Any advice/shortcuts/etc?

like image 684
jle Avatar asked Feb 13 '09 06:02

jle


2 Answers

Ok, I figured this out. The answer is:

1. you need a local printer (if you need to print to a network printer, download the drivers and add it as a local printer)
2. use win32print to get and set default printer
3. also using win32print, use the following code:

import win32print
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter('RICOH-LOCAL', PRINTER_DEFAULTS)
properties = win32print.GetPrinter(pHandle, 2) #get the properties
pDevModeObj = properties["pDevMode"] #get the devmode
automaticTray = 7
tray_one = 1
tray_two = 3
tray_three = 2
printer_tray = []
pDevModeObj.DefaultSource = tray_three #set the tray
properties["pDevMode"]=pDevModeObj #write the devmode back to properties
win32print.SetPrinter(pHandle,2,properties,0) #save the properties to the printer
  1. that's it, the tray has been changed
  2. printing is accomplished using internet explorer (from Graham King's blog)

    from win32com import client
        import time
        ie = client.Dispatch("InternetExplorer.Application")
        def printPDFDocument(filename):
            ie.Navigate(filename)
            if ie.Busy:
                time.sleep(1)
            ie.Document.printAll()
        ie.Quit()
    

Done

like image 79
jle Avatar answered Sep 22 '22 19:09

jle


That's not possible using plain PDF, as you have create new print job for any particular bin and tray combination (and not all printers allow you to do that, Xerox 4x and DP Series allows you to do such things).

My best bet would be juggling with PostScript: convert PDF to PostScript, where you have access to individual pages, then extract the pages you need and for each such page (or pages) create new print job (eg. using Windows program lpr). To ease the task, I'd create print queue for any combination of bin and tray you have to print to, then use these queues as printers.

like image 31
zgoda Avatar answered Sep 23 '22 19:09

zgoda