Skip to content
Advertisement

Redirecting the output of a virtual printer to a Python script

My intention is to achieve the following scenario:

  • A Windows or Linux user gives the command to print a document (using Ctrl+P) and views a dummy printer (virtual printer) in the list of available printers

  • The user selects the printer and gives the print command

  • Instead of actually printing, the dummy printer automatically sends the file, along with the selected paper size, format and so on, to a python script as input [optionally after converting it to PDF].

  • The Python script uploads the file to the cloud or some remote storage from where it can be retrieved and printed later on a physical printer when required.

I have already referred the answer to a similar existing question on StackOverflow and looked up CUPS and CUPS-PDF. CUPS_PDF is a virtual printer so that satisfies one criteria for me, but it doesn’t let me handle the files sent for printing. I could monitor the directory in which the converted PDFs are placed and call the python script every time the directory changes, but that is far from an ideal solution.

I tried creating something similar in Windows by using win32 API in the form of a FILE printer, but again it doesn’t let me handle the file to be printed.

I have also stumbled across Tea4CUPS which is kind of a programmable backend for the CUPS printer, but I am not really sure how to leverage that. Another alternative I encountered was to write a Virtual printer driver using Windows Driver Kit (WDK), but that seems overly complicated especially given the fact that I need the Virtual Printer to just redirect the file to be printed to my python script.

How can I achieve my desired scenario? Though I have Python experience, I have never worked on Virtual printers before this.

Advertisement

Answer

Use RedMon. It is an open-source windows printer port that simply redirects all printer output to a specific program (such as a python script).

The following procedure is valid for up to windows 10 released 2015:

  1. Download and install the RedMon software using the installer.
  2. Under devices and printers, run the “Add Printer” wizard as an administrator (or use win32 API)
  3. Add the printer using a postscript driver (I personally prefer installing the Xerox Global Print Driver PS, but other PS drivers are already installed) and create a new “Redirected Port.”
  4. You can configure the options under the “Configure Port” to direct the output to your script.
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement