0

I have installed ghostscript and gsview. After the installation there is available gsprint.exe command which enables print a pdf file programatically as follows:

from win32 import win32print
import subprocess

if __name__ == "__main__":

    printer_name = "Microsoft Print to PDF"

    print("Printer: %s" % (printer_name))

    win32print.SetDefaultPrinter(printer_name)


    p = subprocess.Popen([r"C:\Program Files\Ghostgum\gsview\gsprint.exe", r"C:\Users\xralf\Desktop\to_print\document1.pdf"],
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    print (stdout)
    print (stderr)

But it's impossible to use it, because ghostview needs registration number and registration numbers are no longer available (according to their page). Do you know some alternative how to print pdf file programmatically in Windows 10?

The answer from here is obsolete now.

xralf
  • 3,312
  • 45
  • 129
  • 200

1 Answers1

1

Ghostview doesn't need a registration number, it just pops up a nag screen if you don't have one. gsprint.exe doesn't even do that.

I just tried this locally. I have no registration number for gsview and gsprint.exe works just fine for me. What makes you think you need a registration number ?

I should note that with recent versions of Ghostscript gsprint.exe does not work, apparently because it has Ghostscript read or write a temporary file, and recent security changes mean that this approach won't work. You can work around it by setting -dNOSAFER, which I do not reccomend as it disables the security.

You could instead use Ghostscript directly, selecting the mswinpr2 device, which prints to Windows printers by first creating a device context suitable for the selected pritner, rendering the content to a bitmap, blitting the bitmap to the device context, and then telling the device context to print (I believe that this is, essentially, what gsprint.exe does anyway)

Other than that you could install CUPS on WSL I should think.

Also the gsprint sources are still available as part of gsview 5.0 from here so you could always modify them yourself. You could even extend it to add the temporary file to the read/write list (--permit_file_read and --permit_file_write) in Ghostscript so that you don't need to use -dNOSAFER.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • >> What makes you think you need a registration number? Because of [this](https://superuser.com/questions/1529654/print-pdf-with-gsprint-exe-error) and when I open a file with ghostview it prints similar error message. – xralf Mar 03 '20 at 16:01
  • 1
    That's nothing to do with needing a registration number. That's caused by the changes in the security model in Ghostscript, as I mentioned in paragraph three of my answer. You cannot open PDF files using Ghostview since those security changes, not even with -dNOSAFER. Sorry, that's what happens when a program is obselete and doesn't get updated to match its components. I've given you the same advice as you got on Super User, except to note that you **can** use gsprint, but you need to add -dNOSAFER which I do **not* reccomend. Better to use Ghostscript's mswinpr2 device if you must do this. – KenS Mar 03 '20 at 16:23