Python silent print PDF to specific printer

前端 未结 5 1029
花落未央
花落未央 2020-12-08 06:02

I have a PDF document and I want to print it with my python app.

I have tried the solution in here (Print PDF document with python\'s win32print module?) but when I

5条回答
  •  臣服心动
    2020-12-08 06:14

    Here's a way to silently print a pdf in the same directory as your python script without gsprint and without win32api. It allows for more GhostScript customization like choosing width/height, etc.

    import os
    import subprocess
    import sys
    
    if sys.platform == 'win32':
        args = '"C:\\\\Program Files\\\\gs\\\\gs9.23\\\\bin\\\\gswin64c" ' \
               '-sDEVICE=mswinpr2 ' \
               '-dBATCH ' \
               '-dNOPAUSE ' \
               '-dFitPage ' \
               '-sOutputFile="%printer%myPrinterName" '
        ghostscript = args + os.path.join(os.getcwd(), 'myFile.pdf').replace('\\', '\\\\')
        subprocess.call(ghostscript, shell=True)
    

    If you're using the 32 bit version of GhostScript then you would use gswin32c

提交回复
热议问题