By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Printing to printer

P: n/a
Hello,

I'm having problems sending information from a python
script to a printer. I was wondering if someone might send me
in the right direction. I wasn't able to find much by Google

TIA
Steve
Aug 12 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 8/11/05, Steve M <st***@myplace.com> wrote:
Hello,

I'm having problems sending information from a python
script to a printer. I was wondering if someone might send me
in the right direction. I wasn't able to find much by Google


Which platform? Directions will vary wildly.

--
Kristian

kristian.zoerhoff(AT)gmail.com
zoerhoff(AT)freeshell.org
Aug 12 '05 #2

P: n/a
Kristian Zoerhoff wrote:
On 8/11/05, Steve M <st***@myplace.com> wrote:
Hello,

I'm having problems sending information from a python
script to a printer. I was wondering if someone might send me
in the right direction. I wasn't able to find much by Google


Which platform? Directions will vary wildly.

Ooops, sorry forgot to mention I'm using Suse 9.0 and Python 2.3x

TIA

Steve
Aug 12 '05 #3

P: n/a
On 8/11/05, Steve M <st***@myplace.com> wrote:
Kristian Zoerhoff wrote:
On 8/11/05, Steve M <st***@myplace.com> wrote:
Hello,

I'm having problems sending information from a python
script to a printer. I was wondering if someone might send me
in the right direction. I wasn't able to find much by Google


Which platform? Directions will vary wildly.

Ooops, sorry forgot to mention I'm using Suse 9.0 and Python 2.3x


Assuming a local printer, you could just open the appropriate device
file (e.g. /dev/lp0) in write mode and write the text to it. Another
option would be to create a temp file, and then feed that to the lpr
or enscript commands via the subprocess module.

--
Kristian

kristian.zoerhoff(AT)gmail.com
zoerhoff(AT)freeshell.org
Aug 12 '05 #4

P: n/a
I adapted some code from David Boddie into a Python class to write
directly to Linux print queues. I have used it in one project and
it worked just fine. I've attached a copy for your use. You are
free to use it as you wish, with no guarantees or warranties.

Hope it helps.

Larry Bates

Steve M wrote:
Hello,

I'm having problems sending information from a python
script to a printer. I was wondering if someone might send me
in the right direction. I wasn't able to find much by Google

TIA
Steve


#! /usr/bin/python
"""
Name : lpr.py
Author : adapted from code by David Boddie
Created : Tue 08th August 2000
Last modified : Thu 06th September 2001
Purpose : Send a file to a printer queue.
"""

import os, socket, string, sys

class lprClass:
_debug=0
_trace=0

def __init__(self, host, user, server, printer):
#
# Handle empty arguments by assiging default values
#
if host: self.host=host
else: self.host=socket.gethostname()

if user: self.user=user
else:
try:
self.user=os.environ['USER']
except KeyError:

print "lprClass***Error no user passed to class and no USER env variable found"
sys.exit(2)

if server: self.server=server
else: self.server=socket.gethostbyaddr(self.host)[2][0]

if printer: self.printer=printer
else:
try:
self.printer=os.environ['PRINTER']
except KeyError:
print "lprClass***Error no printer passed to class and no PRINTER env variable found"
sys.exit(2)

if self._debug:
print "lprClass*** host=",self.host
print "lprClass*** user=",self.user
print "lprClass*** server=",self.server
print "lprClass*** printer=",self.printer

#
# Create a socket object to communcate with the LPR daemon
#
if self._trace: print "lprClass***Creating a socketobj instance"
self.socketobj=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self._trace: print "lprClass***Conecting to port 515 on server"
self.socketobj.connect((self.server, 515))
if self._trace: print "lprClass***Initializing LPR daemon for job to printer=", self.printer
self.socketobj.send("\002"+self.printer+"\012")
d=self.socketobj.recv(1024)
#
# Wait for reply and check response string
#
errormsg="lprClass***Error initializing communications with LPR daemon"
if d != "\000": self._abort(d, errormsg)
return

def _initcontrol(self, filename):
#
# Build a control string
#
control='H'+self.host[:31]+"\012"+'N'+filename[:131]+"\012"+ \
'P'+self.user[:31]+"\012"+'o'+filename[:131]+"\012"

#------------------------------------------------------------------------------------
# Send the receive control file subcommand
#
if self._trace: print "lprClass***Sending the receive control file subcommand"
self.socketobj.send("\002"+str(len(control))+" cfA000"+self.host+"\012")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Waiting for reply"
d=self.socketobj.recv(1024)
errormsg="lprClass***Error in control string initialization for LPR daemon"
if d != "\000": self._abort(d, errormsg)
#------------------------------------------------------------------------------------
# Send the control file
#
if self._trace: print "lprClass***Sending the control file command"
self.socketobj.send(control)
self.socketobj.send("\000")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Waiting for reply"
d=self.socketobj.recv(1024)
errormsg="lprClass***Error in control string for LPR daemon"
if d != "\000": self._abort(d, errormsg)
return

def _abort(self, data, errormsg):
print errormsg
print data.strip()
self.socketobj.send("\001\012")
self.socketobj.close()
sys.exit(0)

def _closelpr(self):
self.socketobj.send("\000")
#
# Wait for reply
# print "Wait for reply"
d=self.socketobj.recv(1024)
errormsg="lprClass***Error in _closelpr"
if d != "\000": self._abort(d, errormsg)
#
# Close the socket connection
#
self.socketobj.close()
return

def _sendheader(self, length):
#------------------------------------------------------------------------------------
# Send the receive data file subcommand
#
if self._trace: print "lprClass***Sending the receive data file subcommand"
self.socketobj.send("\003"+str(length)+" dfA000"+self.host+"\012")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Waiting for reply"
d=self.socketobj.recv(1024)
errormsg="lprClass***Error in receive data file subcommand for LPR daemon"
if d != "\000": self._abort(d, errormsg)
return
def queuefile(self, filename):
try:
ifile=open(filename,'r')
except:
print "lprClass***Unable to find filename=%s to print" % filename
return
#
# Call routine to initialize LPR daemon with control information
#
self._initcontrol(filename)
#
# Must determine the length of the file by seeking to the
# end, save for later
#
ifile.seek(0, 2)
length = ifile.tell()
if self._debug: print "lprClass***queuefile-length=",length
#
# Send file header to LPR
#
self._sendheader(length)
#
# Return to the beginning of the file
#
ifile.seek(0, 0)
#
# Send the data file
if self._debug: print "Sending the data file"
while 1:
contents = ifile.readline()
if not contents: break
self.socketobj.send(contents)

ifile.close()
self._closelpr()
return

def queuestring(self, string):
#
# Call routine to initialize LPR daemon with control information
#
self._initcontrol('')
self._sendheader(len(string))
self.socketobj.send(string)
self._closelpr()
return

if __name__=="__main__":
import time
LPR=lprClass('','','','alarm')
LPR.queuestring('22222222222')
time.sleep(3.0)
LPR=lprClass('','','','alarm')
LPR.queuestring('00000000000')
LPR=lprClass('','','','alarm')
LPR.queuefile('testalarm.py')
time.sleep(3.0)
LPR=lprClass('','','','alarm')
LPR.queuestring('00000000000')
Aug 12 '05 #5

P: n/a
Larry Bates wrote:
I adapted some code from David Boddie into a Python class to write
directly to Linux print queues. I have used it in one project and
it worked just fine. I've attached a copy for your use. You are
free to use it as you wish, with no guarantees or warranties.

Hope it helps.

Larry Bates

Steve M wrote:
Hello,

I'm having problems sending information from a python
script to a printer. I was wondering if someone might send me
in the right direction. I wasn't able to find much by Google

TIA
Steve


Thank you, I'll give it a try.

Steve
Aug 12 '05 #6

P: n/a
Kristian Zoerhoff wrote:
On 8/11/05, Steve M <st***@myplace.com> wrote:
Kristian Zoerhoff wrote:
> On 8/11/05, Steve M <st***@myplace.com> wrote:
>> Hello,
>>
>> I'm having problems sending information from a python
>> script to a printer. I was wondering if someone might send me
>> in the right direction. I wasn't able to find much by Google
>
> Which platform? Directions will vary wildly.
>

Ooops, sorry forgot to mention I'm using Suse 9.0 and Python 2.3x


Assuming a local printer, you could just open the appropriate
device file (e.g. /dev/lp0) in write mode and write the text to it.
Another option would be to create a temp file, and then feed that
to the lpr or enscript commands via the subprocess module.


Thank you for your help. I try and work it out now that I have a
direction.

Steve
Aug 12 '05 #7

P: n/a
Kristian Zoerhoff <kr***************@gmail.com> writes:
On 8/11/05, Steve M <st***@myplace.com> wrote:
Kristian Zoerhoff wrote:
> On 8/11/05, Steve M <st***@myplace.com> wrote:
>> Hello,
>>
>> I'm having problems sending information from a python
>> script to a printer. I was wondering if someone might send me
>> in the right direction. I wasn't able to find much by Google
>
> Which platform? Directions will vary wildly.
>

Ooops, sorry forgot to mention I'm using Suse 9.0 and Python 2.3x


Assuming a local printer, you could just open the appropriate device
file (e.g. /dev/lp0) in write mode and write the text to it. Another
option would be to create a temp file, and then feed that to the lpr
or enscript commands via the subprocess module.


There's a fair chance you have to be root to open the printer. Even if
you can, it won't work right if you've got a winprinter and are trying
to feed it ASCII. Likewise, if you're trying to feed it some pade
description language - HTML, PDF, PS, etc., that probably won't do the
right thing either.

Feeding your text to the lpr command should be the solution to all of
the above problems. The daemon that does the printing runs as
root. The printer configuration should transate whatever format you
feed it to something the printer can print.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Aug 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.