Python Forum

Full Version: Trying to send file to printer with no results.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've tried about 5 different ways to make this work but the file just won't get sent to the printer.
I had been trying to output a png file to an Epson TM-C3500 label printer and was getting gibberish printed out on very long labels I promptly ran out of paper for the printer so I switched to a regular Brother printer, and now I don't even get gibberish, just nothing being sent to the printer.
I even tried sending a simple string to be printed and got nothing.
I can print on the printers manually and the image prints fine.

this is the current code I've been trying:

import win32print
from PIL import Image

# Open the PNG file
im = Image.open("barcode.png")

# Convert the image to a Windows bitmap
bmp = im.convert("RGB").tobytes("raw", "BGR")

# Set the printer name
printer_name = win32print.GetDefaultPrinter()

# Open the printer
hPrinter = win32print.OpenPrinter(printer_name)

# Set the properties of the document
job = win32print.StartDocPrinter(hPrinter, 1, ("test document", None, "RAW"))

# Start a page
win32print.StartPagePrinter(hPrinter)

# Write the image data to the printer
win32print.WritePrinter(hPrinter, bmp)

# End the page
win32print.EndPagePrinter(hPrinter)

# End the document
win32print.EndDocPrinter(hPrinter)

# Close the printer
win32print.ClosePrinter(hPrinter)
The debugger doesn't show any errors either.....
If someone out there could tell me what on earth I'm missing I would be very grateful.
This is basically my first real program in python so I'm a little lost.
I beleive that line 11 is expecting a printer name as an attribute:
change: printer_name = win32print.GetDefaultPrinter()

see this: https://stackoverflow.com/a/60573860

It's been some time since I've done any coding on ms windows, so the posted reference may help as it appears to be a complete writeup on the win32 printing package.
Most printers can handle pdfs no trouble I think.

Why don't you make a pdf, then print that? Quick and easy!!

Also, if they are not throwaway barcodes, you can print them again and again!

#! /usr/bin/python3
from reportlab.pdfgen import canvas # need this here
from reportlab.lib.pagesizes import A4  # need this here
from reportlab.pdfbase.ttfonts import TTFont # need this here
from reportlab.pdfbase import pdfmetrics # need this here
from reportlab.lib.units import mm
import glob
# to help you locate objects on the page
# a reportlab page is in points of 1/72nd inch
print('1mm is', mm, 'page points')
# 2.834645669291339
# the sizes in points of an A4 page
print('An A4 page is', A4, 'width by height in points.')
fontpath = '/home/pedro/.local/share/fonts/'
path2pdf = '/home/pedro/pdfs/'
path2QRcodes = '/home/pedro/temp/QRcodes/'
mypdf = 'mysizePDF.pdf'

# this is the name of the pdf window
title = 'A custom size pdf'
# for Chinese
ttfFile = os.path.join(fontpath, 'DroidSansFallbackFull.ttf')
pdfmetrics.registerFont(TTFont("Droid", ttfFile))

qrcodes = glob.glob(path2QRcodes + '*.png')
# insert the QR code
def putQRcode(qrcode, myCanvas):    
    # put the qr code on the page    
    myCanvas.drawImage(qrcode, 350, 350, 100, 100, mask='auto')    
    return myCanvas

# bottom left corner is x=0, y=0
# c.showPage() puts a page break
# set any custom pagesize with pagesize(x, y)
def makePDF():
    c = canvas.Canvas(path2pdf + mypdf, pagesize=(500, 500))
    c.setTitle(title)
    # for Chinese
    #c.setFont('Droid', 18)
    c.setFont('Times-Roman', 14)
    for q in qrcodes:    
        c.drawString(50, 400, "Welcome to Reportlab!")
        c.drawString(50, 380, "This is my custom pdf size")
        c = putQRcode(q, c) 
        c.showPage()    
    c.save()

if __name__ == '__main__':
    makePDF()