At the organization I work for, different printers are set up at various locations. All are mainly used to print A4-sized documents, so the defaults are set up accordingly.
We are also using a bunch of custom-sized forms which people have up to now been filling in by hand. Recently, I was tasked with setting up print-automation onto the said forms from our central database.
I'm using reportlab to create temporary pdf files which I am then trying to send to the default printer. All is relatively simple, save for getting the printers to register a custom paper size.
I got as far as the following code snippet, but I'm really stuck.
import tempfile
import win32api
import win32print
pdf_file = tempfile.mktemp(".pdf")
#CREATION OF PDF FILE WITH REPORTLAB
printer = win32print.GetDefaultPrinter()
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter(printer, PRINTER_DEFAULTS)
level = 2
properties = win32print.GetPrinter(pHandle, level)
pDevModeObj = properties["pDevMode"]
pDevModeObj.PaperSize = 0
pDevModeObj.PaperLength = 2200 #SIZE IN 1/10 mm
pDevModeObj.PaperWidth = 1000 #SIZE IN 1/10 mm
properties["pDevMode"]=pDevModeObj
win32print.SetPrinter(pHandle,level,properties,0)
#OPTION ONE
#win32api.ShellExecute(0, "print", pdf_file, None, ".", 0)
#OPTION TWO
win32api.ShellExecute (0,"printto",pdf_file,'"%s"' % printer,".",0)
win32print.ClosePrinter(pHandle)
It just does not work. Printers do not report a "paper size mismatch", like they should when a non-A4 document is being sent to them. And when I try printing to a PDF printer, it also defaults to A4.
When calling
print(pDevModeObj.PaperSize)
print(pDevModeObj.PaperLength)
print(pDevModeObj.PaperWidth)
everything seems to be in order, so I'm guessing I don't know how to send those paper size values back to the printer settings.
I'd greatly appreciate any insights, suggestions, ... Thank you all.
I apologize in advance if I am posting a question that has already been answered. If it has been, I really must have been handed an extra chunk of bad luck and missed the answer. So before anyone decides to downvote, here is a list of all the resources I checked out (examples not all in python, and a few are not using the win32api), and couldn't get the thing to work properly:
- Programmatically Print a PDF File - Specifying Printer
- Python's win32api only printing to default printer
- https://mail.python.org/pipermail/python-win32/2005-August/003683.html
- https://docs.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-setprinter-api
- Print PDF file in duplex mode via Python
- https://www.thinbug.com/q/39249360
- Saving / Restoring Printer DevModes - wxPython / win32print
- pywin32: how do I get a pyDEVMODE object?
- https://docs.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-documentproperties
- How to change printer preference settings using python
- Print file to continuous paper using win32print Python
- python win32print can't set custom page size
- http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html
- https://newcenturycomputers.net/projects/pythonicwindowsprinting.html
- Printing a file and configure printer settings
- Change printer default paper size
- https://grokbase.com/t/python/python-win32/085x5hdbtd/how-to-change-paper-size-while-printing
- openpyxl - set custom paper size for printing
- Python win32print changing advanced printer options
- Printing PDF files with Python
- Python silent print PDF to specific printer
- https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printerconfiguration
- Printing PDF's using Python,win32api, and Acrobat Reader 9
- Python print pdf file with win32print
- How to chose Paper Format when printing a PDF File with Python?
- Access denied when attempting to remove printer
- https://www.programcreek.com/python/example/24860/win32api.ShellExecute
- https://opensource.gonnerman.org/?p=192
- Python27 - on windows 10 how can i tell printing paper size is 50.8mm x 25.4mm?
- https://mail.python.org/pipermail/python-win32/2008-May/007640.html
- http://timgolden.me.uk/python/win32_how_do_i/print.html