pythonlinuxprintingdebiancups

Getting error in CUPS "The page setup information was not valid." with printing image from python script


I try to print an image from python script on Debian 10 using cups:

import cups

def printImageLinux(image_name):
    conn = cups.Connection()
    printer = conn.getDefault()
    conn.printFile(printer, image_name, 'suo_ticket', {})

Finally, the image went to print but I see canceling of printing and error in Cups user interface (localhost:631) with a message: "The page setup information was not valid."

CUPS message screenshot

I suppose that I should make some preparation of image sending to print, but I didn't find any information about it. I can print the same image from Windows using win32print module and code:

import os, sys
from win32 import win32api, win32print
from PIL import Image, ImageWin

def printImage(image_name):
    # Constants for GetDeviceCaps
    # HORZRES / VERTRES = printable area
    HORZRES = 8
    VERTRES = 10

    # LOGPIXELS = dots per inch
    LOGPIXELSX = 88
    LOGPIXELSY = 90

    # PHYSICALWIDTH/HEIGHT = total area
    PHYSICALWIDTH = 110
    PHYSICALHEIGHT = 111

    # PHYSICALOFFSETX/Y = left / top margin
    PHYSICALOFFSETX = 112
    PHYSICALOFFSETY = 113

    printer_name = win32print.GetDefaultPrinter ()
    file_name = image_name

    #
    # You can only write a Device-independent bitmap
    #  directly to a Windows device context; therefore
    #  we need (for ease) to use the Python Imaging
    #  Library to manipulate the image.
    #
    # Create a device context from a named printer
    #  and assess the printable size of the paper.
    #
    hDC = win32ui.CreateDC ()
    hDC.CreatePrinterDC (printer_name)
    printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
    printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
    printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)

    #
    # Open the image, rotate it if it's wider than
    #  it is high, and work out how much to multiply
    #  each pixel by to get it as big as possible on
    #  the page without distorting.
    #
    bmp = Image.open (file_name)
    if bmp.size[0] > bmp.size[1]:
      bmp = bmp.rotate (90)

    ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
    scale = min (ratios)

    #
    # Start the print job, and draw the bitmap to
    #  the printer device at the scaled size.
    #
    hDC.StartDoc (file_name)
    hDC.StartPage ()

    dib = ImageWin.Dib (bmp)
    scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
    x1 = int ((printer_size[0] - scaled_width) / 2)
    y1 = int ((printer_size[1] - scaled_height) / 2)
    x2 = x1 + scaled_width
    y2 = y1 + scaled_height
    dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2))

    hDC.EndPage ()
    hDC.EndDoc ()
    hDC.DeleteDC ()

It works correctly.

Can I make the same operations with the image in Debian?

Thank you for any help!


Solution

  • Problem was solved with using PIL module. Working code:

    import cups
    from PIL import Image
    
    def printImageLinux(image_name):
        conn = cups.Connection()
        printer = conn.getDefault()
    
        image1 = Image.open(image_name)
        im1 = image1.convert('RGB')
        im1.save('temp_image.pdf')
        conn.printFile(printer, 'temp_image.pdf', 'suo_ticket', {'fit-to-page':'True'})