import PIL.Image
import PIL.ImageWin
import visitor
import win32print
import win32ui
PRINTING_NAME = "win32"""" The printing name """
PRINTING_SCALE = 4""" The printing scale """
TEST_TITLE = "colony_test_document"""" The test title """
TEST_TEXT = "Hello world from Hive Colony"""" The test text """
HORIZONTAL_RESOLUTION = 8""" The horizontal resolution """
VERTICAL_RESOLUTION = 10""" The vertical resolution """
LOG_PIXELS_X = 88""" The number of dots per inch in the x axis """
LOG_PIXELS_Y = 90""" The number of dots per inch in the y axis """
PHYSICAL_WIDTH = 110""" The physical width area """
PHYSICAL_HEIGHT = 111""" The physical height area """
PHYSICAL_OFFSET_X = 112""" The physical offset x, the left margin """
PHYSICAL_OFFSET_Y = 113""" The physical offset y, the top margin """
INCH = 1440""" The number of inches """defget_printer_handler(printing_options):"""
Retrieves a new printer handler for the
given printing options.
@type printing_options: Dictionary
@param printing_options: The printing options to be used
to create the printer handler.
@rtype: Tuple
@return: The tuple containing the printer handler.
"""# retrieves the printer name
printer_name = printing_options.get("printer_name", win32print.GetDefaultPrinter())
# creates a new win32 device context and retrieves the handler
handler_device_context = win32ui.CreateDC()
# creates a printer device context using the handler
handler_device_context.CreatePrinterDC(printer_name)
# retrieves the printer size
printer_size = printing_options.get("printer_size", get_default_printer_size(handler_device_context))
# retrieves the printable area
printable_area = printing_options.get("printable_area", get_default_printable_area(handler_device_context))
# retrieves the printer margins
printer_margins = printing_options.get("printer_margins", get_default_printer_margins(handler_device_context))
# returns the printer handler tuplereturn (
handler_device_context,
printable_area,
printer_size,
printer_margins
)
defclose_printer_handler(printer_handler_context):"""
Closes the printer handler for the given printer
handler context.
@type handler_device_context: PyCDC
@param handler_device_context: The handler to the device (printer) context
to be closed.
"""# releases the resource
printer_handler_context.DeleteDC()
defget_default_printable_area(handler_device_context):"""
Retrieves the default printable area from the given
device context handler.
@type handler_device_context: PyCDC
@param handler_device_context: The handler to the device context.
@rtype: Tuple
@return: A tuple containing the printable area.
"""return handler_device_context.GetDeviceCaps(HORIZONTAL_RESOLUTION), handler_device_context.GetDeviceCaps(
VERTICAL_RESOLUTION)
defget_default_printer_size(handler_device_context):return handler_device_context.GetDeviceCaps(PHYSICAL_WIDTH), handler_device_context.GetDeviceCaps(
PHYSICAL_HEIGHT)
defget_default_printer_margins(handler_device_context):return handler_device_context.GetDeviceCaps(PHYSICAL_OFFSET_X), handler_device_context.GetDeviceCaps(
PHYSICAL_OFFSET_Y)
defprint_test_image(image_path, printing_options={}):# retrieves the printer handler
handler_device_context, _printable_area, printer_size, printer_margins = get_printer_handler(printing_options)
# opens the bitmap image
bitmap_image = PIL.Image.open(image_path)
# retrieves the bitmap image width and height
bitmap_image_width, bitmap_image_height = bitmap_image.size
# starts the document
handler_device_context.StartDoc(TEST_TITLE)
# starts the first page
handler_device_context.StartPage()
# creates the dib image from the original# bitmap image, created with PIL
dib_image = PIL.ImageWin.Dib(bitmap_image)
# retrieves the output for the handler device context
handler_device_context_output = handler_device_context.GetHandleOutput()
# draws the image in the output for the handler device context
dib_image.draw(handler_device_context_output, (5, 5, 300, 230))
# ends the current page
handler_device_context.EndPage()
# ends the document
handler_device_context.EndDoc()
# closes the printer handler
close_printer_handler(handler_device_context)
if __name__ == '__main__':
print_test_image('qr-code.png')