How to print pdf file without opening PDF viewer print prompt in python
Question:
I’d like to print a PDF file by Python, without opening any PDF viewer program (ex. adobe).
I have tried some code. It pop up the adobe program before printing.
first code
import win32print
import win32api
currentprinter = win32print.GetDefaultPrinter()
win32api.ShellExecute(0, "print", 'report.pdf', currentprinter, ".", 0)
second code
import os
os.startfile("report.pdf", "print")
Any code I have to rewrite or any recommend program to use instead of adobe?
Answers:
-
download PDFtoPrinter.exe and move it into folder.
-
code it.
import subprocess
def command_print(event = None):
command = "{} {}".format('PDFtoPrinter.exe','report.pdf')
subprocess.call(command,shell=True)
command_print()
I’d like to print a PDF file by Python, without opening any PDF viewer program (ex. adobe).
I have tried some code. It pop up the adobe program before printing.
first code
import win32print
import win32api
currentprinter = win32print.GetDefaultPrinter()
win32api.ShellExecute(0, "print", 'report.pdf', currentprinter, ".", 0)
second code
import os
os.startfile("report.pdf", "print")
Any code I have to rewrite or any recommend program to use instead of adobe?
-
download PDFtoPrinter.exe and move it into folder.
-
code it.
import subprocess def command_print(event = None): command = "{} {}".format('PDFtoPrinter.exe','report.pdf') subprocess.call(command,shell=True) command_print()