Printing a file and configure printer settings
Question: I'm trying to code a printer automation using Python on Windows, but can't get
it done.

I'm not really understanding the topic and i'm a little surprised - a "simple"
way to get this done doesn't seem to exist..? There are so many APIs that
allow to access common things in a nice and easy way, but printing seems to be
something "special"..?



Here is what i have and what i want to do:

  * There is a PDF file. The PDF already exists, i do not want to create PDFs or any other filetype. I like to print this PDF file. One file at a time.

  * The file could be in landscape and portrait layout. The file could have one of this sizes: A4, A3, A2, A1 and A0.

  * I like to print the file using a normal, "physical" printer. The printer is a network device and connected using its IP. There are various network printers and i'd like to be able to use more than one of them. Some are just small A4-printers, some are big office devices (all-in-one scan, copy, print, ...) - and there are big plotters, too (up to A0 sized paper). 

  * I'd like to code: "Print this PDF file on this printer".

  * I like to configure the printing size. I'd like to print the PDF "as it is", in its original size - but i want to be able to print big formats on small paper sizes, too. Like, the PDF itself is an A0 size, but i want to print it on A3 paper. Or the original PDF size is A2 and i want to print it on A4. 

  * I'd like to use that on Windows 7 computers (SP1, 64bit). And i'm trying to code that in python. I'm using python 2.7, as i'm using some third-party-modules that are not available in python 3. In general, every working solution that can be triggered through a python script is welcome.



To me, that doesn't seem to be a very "complex" task to do. Doing that "by
hand" is very easy and straightforward - select the document, start printing,
select the printer, select the paper size - and print.

Doing this by code seems to be rather difficult. Here is what i've come across
until now.

  * There are various programs that could be used for command line printing, programs like "Acrobat Reader", "Foxit Reader" or similar. While it works perfect to print using the commands those programs provide, it's not possible to access the printer settings to configure the paper size.

  * There are special command line printing tools, but i couldn't find any useful freeware. I have tried the "VeryPDF" command line tool, but had some problems using it when it comes to paper sizes. While there is perfect support for a whole range of letter-formats and various other things, the paper sizes i need (A4 to A0) somehow aren't supported. There are presets for A4 and A3, those work. The tool has the option to use custom paper sizes by just passing the measurements (in/pt/mm) - but that didn't work as the examples show, it always prints to A4 when using this method.

  * I've found the win32-package for python, including win32print. I don't get this thing. The API provides functions to find, add or remove printers, list a printer queue, start and stop printer jobs and so on - but no simple possibility to print a file. It seems like this API could be used to add a printer job by creating the printing data by python coding, push some text and/or graphics in something like a "file" and send that to the printer. In a format the printer already understands.



And when using this win32print module, i can't get it to work correctly. Here
is an exmple snippet i tried to use:

    
    
    from win32print import *
    printer = GetDefaultPrinterW()
    handle = OpenPrinter(printer)
    info = GetPrinter(handle, 2)
    cap = DeviceCapabilities(info['pPrinterName'], info['pPortName'], DC_PAPERS)
    ClosePrinter(handle)
    

...as described here:

<http://timgolden.me.uk/pywin32-docs/win32print__DeviceCapabilities_meth.html>

But that just returns:

    
    
    NameError: name 'DC_PAPERS' is not defined
    

That happens whenever i'm trying to use a function that needs such constants
passed. Not a single constant is defined on my system and i don't know why.

But i don't know if i could use this API even when working correctly, all the
usage examples are just showing how to send a text-string to a printer. That's
not what i need, that's not what i want to know.



Is there any working solution to print a file and to set the printing size in
a simple and straightforward way?

Ideas and hints are welcome!


Answer: Look at the "[How To
Print](http://timgolden.me.uk/python/win32_how_do_i/print.html)" page on Tim
Golden's website. This page was the same in 2014 when you asked your question.
There's an example printing a JPG file that also manipulates the printer
settings.

That's not quite a perfect example for what you're doing but it should get you
on the right track.

DC_PAPERS is defined in win32con:

    
    
    import win32con
    x = win32con.DC_PAPERS
    

How you're supposed to know that, I have no idea. Perhaps it's "obvious" to
those already familiar with the Win32API outside of Python...

