下面是使用 pycups 的脚本,我可以使用自定义页面尺寸进行打印,但我需要设置边距以及如何做到这一点。
import cups
def print_to_printer(printer_name, file_path, custom_width=720, custom_length=432):
conn = cups.Connection()
# Get the printer information
printer_info = conn.getPrinterAttributes(printer_name)
# Convert inches to points (1 inch = 72 points)
custom_width_points = str(int(custom_width * 72))
custom_length_points = str(int(custom_length * 72))
# Create the page size string
page_size_str = f"{custom_width_points}x{custom_length_points}"
# Start the print job with custom page size
print_options = {'PageSize': page_size_str}
print_job_id = conn.printFile(printer_name, file_path, "Print Job", print_options)
print(f"Print job {print_job_id} sent to {printer_name} with custom page size ({custom_width}x{custom_length} inches)")
# Example usage
printer_name = "TVS_MSP-250CH-TVS-original" # Replace with your actual printer name
file_to_print = "/home/tk/Documents/bill.txt" # Replace with the path to your file
custom_width = 10 # Replace with your desired width in inches
custom_length = 6 # Replace with your desired length in inches
print_to_printer(printer_name, file_to_print, custom_width, custom_length)
创建一个新的
cups.Page
对象。
设置打印页面的边距。 传递给
setMargins
的值以磅(1/72 英寸)为单位。
这里我们将所有四个边距(上、下、左、右)设置为 72 点,相当于 1 英寸。
page = cups.Page(...)
page.setMargins(72, 72, 72, 72)
conn.printFile(printer_name, page, "My Print Job", {})
上面编写的代码片段创建一个新的页面对象并设置其边距,然后将打印作业发送到指定的打印机。