Printing in Python without printing to a specific printer

I have a PDF document and I want to print it using my python application.

I tried the solution in here (print a PDF document using python win32print module?) , But when I install Ghostscript 9.15, which is the actual version, it does not have gsprint.exe

The way I use this work is the os.startfile('PDFfile.pdf', "print") command os.startfile('PDFfile.pdf', "print") , but it opens the default viewer (mine is Adobe Reader), and after printing has been opened, try kill the process with os.system("TASKKILL /F /IM AcroRD32.exe") kills other open windows, but I don’t want this.

At the next command, it also prints, but it also allows you to open Adobe Reader

 currentprinter = win32print.GetDefaultPrinter() win32api.ShellExecute(0, "print", 'PDFfile.pdf', '/d:"%s"' % currentprinter, ".", 0) 

I saw this answer , but they recommend using gsprint.exe again

Does anyone have a gsprint.exe file or any other solution?

NOTE. . When I used another default program to open PDF files, such as Chrome or Windows Reader, I always get an exception when executing the commands above '(31, 'ShellExecute', 'A device attached to the system is not functioning.')' Or [Error 1155] No application is associated with the specified file for this operation: 'PDFfile.pdf' using the startfile command

+12
source share
4 answers

Finally, after hours and hours of searching for the right files, I found the answer to my problem.

You can download GSPRINT HERE

You can download the Ghostscript GPL HERE

Using these extracted files on your PC (Windows), you can print the PDF using this command

 GHOSTSCRIPT_PATH = "C:\\path\\to\\GHOSTSCRIPT\\bin\\gswin32.exe" GSPRINT_PATH = "C:\\path\\to\\GSPRINT\\gsprint.exe" # YOU CAN PUT HERE THE NAME OF YOUR SPECIFIC PRINTER INSTEAD OF DEFAULT currentprinter = win32print.GetDefaultPrinter() win32api.ShellExecute(0, 'open', GSPRINT_PATH, '-ghostscript "'+GHOSTSCRIPT_PATH+'" -printer "'+currentprinter+'" "PDFFile.pdf"', '.', 0) 

GhostScript can also be found on the official page HERE

I found gsprint.exe for 64 bit HERE

Hope this helps.

+12
source

Here you can quietly print pdf in the same directory as your python script without gsprint and without win32api . This allows you to customize GhostScript more, for example, to select the 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 are using the 32 bit version of GhostScript, you can use gswin32c

0
source

If you want to print specific pages and some other parameters, you must specify them in the gsprint parameters as follows:

 import win32print import win32api GHOSTSCRIPT_PATH = "C:\\path\\to\\GHOSTSCRIPT\\bin\\gswin32.exe" GSPRINT_PATH = "C:\\path\\to\\GSPRINT\\gsprint.exe" params = '-ghostscript "'+ GHOSTSCRIPT_PATH +'" -printer "'+currentprinter+'" -from 1 -to 3 -landscape -copies 1 "1.pdf "' print(params) win32api.ShellExecute(0, 'open', GSPRINT_PATH, params, '.',0) 
0
source

The following code will lock the current task

 for i in range(10): currentprinter = win32print.GetDefaultPrinter() win32api.ShellExecute(0, "print", 'PDFfile.pdf', '/d:"%s"' % currentprinter, ".", 0) 

and killing the reader after printing the help will not block the current job

 os.system("TASKKILL /F /IM AcroRD32.exe") 

but it will close other pdf files as well.

If you cannot use gsprint, use the acrobat command

 import win32print import subprocess import time pdf_file = 'D:\d1\d1.pdf' acrobat = 'C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe' name = win32print.GetDefaultPrinter() cmd = '"{}" /n /h /s /o /t "{}" "{}"'.format(acrobat, pdf_file, name) for i in range(10)): proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 

It will not block the current task and will not close other PDF files.

0
source

Source: https://habr.com/ru/post/978851/


All Articles