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:

  1. download PDFtoPrinter.exe and move it into folder.

  2. code it.

    import subprocess
    
    def command_print(event = None):
        command = "{} {}".format('PDFtoPrinter.exe','report.pdf')
        subprocess.call(command,shell=True)
    
    command_print()
    
Answered By: Vittawat Laorungroj
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.