pythonpdfprintingghostscript

How do I fix Python Ghostscript Program printing a blank pdf file?


I am trying to print a pdf file from a folder to a hard physical copy for user use. The pdf file is good, simple label sheet(screenshot attached), the program runs good, but if I print the pdf to a file, output file is a blank pdf, if I print it to OneNote, it gives me the below message. I have downloaded and am using 32 version of gswin32 and gsprint, but for fun, I did try the 64 versions as well, which did not work. Any and all help, suggestions, guidance, directions would be greatly appreciated:

import os
import win32print
import win32api

currentprinter = win32print.GetDefaultPrinter()
print(currentprinter)
gspath = "gswin32.exe"
gsp_path = "gsprint.exe"

for i in os.listdir(r"C:\guis\Temppdffolder"):
    print(i)
    path = r"C:\guis\Temppdffolderf"+"\%s"%(i)
    win32api.ShellExecute(0, 'open', gsp_path, '-ghostscript "'+gspath+'" -printer "'+currentprinter+'" "%s"'%(path), '.', 0)

OneNote Message

simple pdf label sheet file

Edit: So after research(thanks @KJ), and some trial and error, the below works to print a pdf for me in reference to above script. Wanted to add it in case it might help someone:

import os

os.startfile("downtime_labels.pdf", "print")

Solution

  • GSprint is not part of ghostscript it was a very old commercial application to be used with a spin off viewer called GhostGum GSview which was abandoned many moons past at Version5 and cannot work with more recently secured versions of Ghostscript.

    For printing securely to modern printers you will ideally be using (at this time version 10.02.0) of non commercial AGPL (or commercial license) Ghostscript or GhostPCL GhostXPS or the overarching GhostPDL

    For Windows the programming syntax will be either GS or gswin##c.exe (where ## is 32 or 64)

    So for example "c:\path to\gswin32c.exe" [switches] [file1.ps file2.ps ...]

    Note emphasis is on PS as intended input, but PDF is also handled exceptionally well.
    Input formats: PostScript PostScriptLevel1 PostScriptLevel2 PostScriptLevel3 PDF

    Many switches for programming file output can be replaced by -oc:\path\out.pdf or similar for images.

    For printing to a device there are 2 methods one is direct to a printer -sDEVICE=???? other is via MS system -sDEVICE=mswinpr2 possibly that is the one you should be using when using a system printer name?

    Thus your attempt to use Ghostscript programmatically is incorrect. So you need to review https://ghostscript.readthedocs.io/en/latest/Use.html

    Side note you seem to be using a variation on Avery5195EasyPeelReturnAddressLabels.pdf and Avery have a very good templating system for printing from Word based mail merge docx templates.