def print_document(filename, printer_name): print(printer_name) # printer_name = win32print.GetDefaultPrinter() if sys.version_info >= (3, ): fp = open("./static/UPLOADFOLDER/" + filename, "rb") raw_data = bytes("This is a test", "utf-8") else: fp = open("./static/UPLOADFOLDER/" + filename, "r") raw_data = "This is a test" data = fp.read() print(printer_name) hPrinter = win32print.OpenPrinter(printer_name) try: hJob = win32print.StartDocPrinter(hPrinter, 1, (filename, None, "RAW")) try: jobInfo = win32print.GetJob(hPrinter, hJob, 1) win32print.SetJob(hPrinter, hJob, 1, jobInfo, win32print.JOB_CONTROL_SENT_TO_PRINTER) win32print.StartPagePrinter(hPrinter) win32print.WritePrinter(hPrinter, data) win32print.EndPagePrinter(hPrinter) finally: win32print.EndDocPrinter(hPrinter) finally: win32print.ClosePrinter(hPrinter) # os.startfile("H:/Projects/College/LanPrint/static/UPLOADFOLDER/"+filename, "print") return Response("Printing Document", status=200, mimetype='text/plain')
def clearJobsQueue(): printer = win32print.OpenPrinter('PDFCreator') jobs = win32print.EnumJobs(printer, 0, 100, 2) while len(jobs) > 0: for currentJob in jobs: print '\nDeleting print job with id [' + str(currentJob['JobId']) + ']' win32print.SetJob(printer, currentJob['JobId'], 0, None, win32print.JOB_CONTROL_DELETE) win32print.ClosePrinter(printer) time.sleep(.5) printer = win32print.OpenPrinter('PDFCreator') jobs = win32print.EnumJobs(printer, 0, 100, 2) win32print.ClosePrinter(printer)
def cancelPrint(printerName, docName): phandle = win32print.OpenPrinter(printerName) print_jobs = win32print.EnumJobs(phandle, 0, -1, 1) for job in print_jobs: if docName in job["pDocument"]: document = job["pDocument"] print("Canceling Print: " + document) win32print.SetJob(phandle, job["JobId"], 0, None, win32print.JOB_CONTROL_DELETE) win32print.ClosePrinter(phandle) time.sleep(1)
def cancelPrint(): jobs = [1] while jobs: jobs = [] for p in win32print.EnumPrinters(win32print.PRINTER_ENUM_CONNECTIONS, None, 1): flags, desc, name, comment = p phandle = win32print.OpenPrinter(name) print_jobs = win32print.EnumJobs(phandle, 0, -1, 1) if print_jobs: jobs.extend(list(print_jobs)) for job in print_jobs: win32print.SetJob(phandle, win32print.AddJob(phandle)[1], 0, None, win32print.JOB_CONTROL_DELETE) document = job["pDocument"] print("Canceling Print: " + document) win32print.ClosePrinter(phandle) time.sleep(5)
def SetJobCmd(self, printer, jobID, JobInfo, RCmd): phandle = win32print.OpenPrinter(printer) win32print.SetJob(phandle, jobID, 1, JobInfo, Cmds[RCmd]) win32print.ClosePrinter(phandle)