2017-06-19 26 views
3

我有HP LJ P1005打印機,它是非PCL(基於主機)打印機。它有一個專用的驅動程序link to the driver。它不支持HP支持PCL5的通用打印驅動程序(HP-UPD)。 A list of supported UPD printers我可以將PCL5轉義序列打印到非PCL基於主機的打印機嗎?

我的問題是如何在打印機上使用PCL5轉義序列,或者甚至有可能?這是否意味着如果它是基於主機的,則通過打印機驅動程序的主機PC必須解釋PCL5命令,否則PCL5根本無法使用?如何知道一個驅動程序是否與PCL兼容?如果主機PC必須解釋PCL5,那麼打印處理器設置應該如下所示:RA​​W,IMF,EMF,winprint TEXT?

回答

2

好吧,現在我知道我可以用PCL轉義序列創建一個文本文件並解釋它並在非PCL基於主機的打印機上打印它。

首先,我發現here一種方式如何從txt文件創建一個pcl文件。創建Windows中的PCL打印機後,從Python中,我使用的外部PROGRAMM GhostPCL,這是編譯的Ghostscript解釋器從PCL文件here創建一個PDF文件是這樣的:

gpcl6win32.exe -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=print.pdf print.pcl 

dNOPAUSEdBATCH是沉默和無交互轉換。 最後,我安裝了gsviewer,並從Python靜靜地將該pdf打印到我的默認非PCL基於主機的打印機。我所有的PCL代碼都被解釋並打印到我便宜的打印機上。它在打印時不會打開任何窗口,因此對於客戶端來說似乎一切正常。

我不是一個程序員,所以代碼是不是你見過的最好的,但它的工作原理:

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 

""" 
Autor: hrvooje 
Last edit: June 2017 

'pip install pypiwin32 --> for installing win32api' 
In python 2: 'python -m pip install pypiwin32' 
io module for io.open in Python2, the same as 'open' in Python3 
First command line argument is for file name which we want to print: 
'python print_rawpcl.py my_pcl_text_file.txt' 
""" 

import os, sys, io, win32print, win32api, subprocess 


def remove_silently(file1): 
    """Removing silently files from last time if there are any left""" 
    try: 
     os.remove(file1) 
    except OSError: 
     pass 

# Asign your printers and variables 
first_default_printer = win32print.GetDefaultPrinter() 
tmp_printer = "local_pcl" 
my_pcl_file = "print.pcl" 
my_output_pdf = "print.pdf" 

# Remove files if they already exist 
remove_silently(my_output_pdf) 
remove_silently(my_pcl_file) 

# If there is command line argument, the first one is our file_to_print 
if len(sys.argv) > 1: 
    file_to_print = sys.argv[1] 
else: 
    file_to_print = "RACUN.TXT" 

# Searching for supported PCL printers as default printer 
pcl_supported = False 
supported_printers = ["2035", "1320", "KONICA", "DIREKT"] 
for item in supported_printers: 
    if item.lower() in first_default_printer.lower(): 
     pcl_supported = True 
     break 
    else: 
     is_supported = False 

if pcl_supported == False: 
     win32print.SetDefaultPrinter(tmp_printer) 

# Printing RAW data to the virtual 'local_pcl' printer or to the 'HP LJ P2035' 
try: 
    # rb --> 'read, bytes', string is 'bytes' type, not unicode (Python3) 
    with io.open(file_to_print, 'rb') as f: 
     raw_data = f.read() 
     hPrinter = win32print.OpenPrinter(win32print.GetDefaultPrinter()) 
     try: 
      hJob = win32print.StartDocPrinter(hPrinter, 1, (
        "print_rawpcl.py data", None, "RAW")) 
      try: 
       win32print.StartPagePrinter(hPrinter) 
       win32print.WritePrinter(hPrinter, raw_data) 
       win32print.EndPagePrinter(hPrinter) 
      finally: 
       win32print.EndDocPrinter(hPrinter) 
     finally: 
      win32print.ClosePrinter(hPrinter) 
except OSError as e: 
    print("Failed: {}".format(e)) 

# Convert a pcl file to pdf with GhostPCL (Ghostscript) 
# if the default printer is local_pcl 
converter_app = 'C:/Python34/ghostpcl-9.21-win32/gpcl6win32.exe' 
if win32print.GetDefaultPrinter() == "local_pcl": 
    subprocess.call(
     [converter_app, '-dNOPAUSE', '-dBATCH', '-sDEVICE=pdfwrite', 
     '-sOutputFile=print.pdf', 'print.pcl']) 

    # return default printer to the printer that was default at the start 
    win32print.SetDefaultPrinter(first_default_printer) 

    # Finally, print that print.pdf to your first default printer silently 
    gsprint_app = "C:\\Program Files\\Ghostgum\\gsview\\gsprint.exe" 
    p = subprocess.Popen(
      [gsprint_app, my_output_pdf], stdout=subprocess.PIPE, 
      stderr=subprocess.PIPE) 
    # Waits for the gs process to end 
    stdout, stderr = p.communicate() 
    # Remove print.pcl and print.pdf file 
    remove_silently(my_output_pdf) 
    remove_silently(my_pcl_file) 

# Removes that first txt file 
remove_silently(file_to_print) 
相關問題