Python Forum
Trying to send file to printer with no results.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to send file to printer with no results.
#1
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.
Larz60+ write Dec-18-2022, 06:03 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
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.
chob_thomas likes this post
Reply
#3
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()
Gribouillis and chob_thomas like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating sharepoint excel file odd results cubangt 1 756 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,307 Sep-27-2022, 01:38 PM
Last Post: buran
  How do you marshal the default printer setup to print? hammer 0 1,251 May-29-2022, 02:54 PM
Last Post: hammer
  Can you print a string variable to printer hammer 2 1,894 Apr-30-2022, 11:48 PM
Last Post: hammer
  [split] Results of this program in an excel file eisamabodian 1 1,543 Feb-11-2022, 03:18 PM
Last Post: snippsat
  How to save some results in .txt file with Python? Melcu54 4 7,243 May-26-2021, 08:15 AM
Last Post: snippsat
  Running A Parser In VSCode - And Write The Results Into A Csv-File apollo 5 4,541 Jan-14-2021, 08:58 PM
Last Post: snippsat
  Writing unit test results into a text file ateestructural 3 4,654 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  capture pytest results to a file maiya 2 5,753 Oct-17-2020, 03:42 AM
Last Post: maiya
  Printing to a printer connected to pi 4 alan 2 2,382 Oct-04-2020, 10:08 PM
Last Post: alan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020