473,769 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing to printer

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
7 6878
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.zoerho ff(AT)gmail.com
zoerhoff(AT)fre eshell.org
Aug 12 '05 #2
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
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.zoerho ff(AT)gmail.com
zoerhoff(AT)fre eshell.org
Aug 12 '05 #4
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=socke t.gethostname()

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

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

if server: self.server=ser ver
else: self.server=soc ket.gethostbyad dr(self.host)[2][0]

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

if self._debug:
print "lprClass** * host=",self.hos t
print "lprClass** * user=",self.use r
print "lprClass** * server=",self.s erver
print "lprClass** * printer=",self. printer

#
# Create a socket object to communcate with the LPR daemon
#
if self._trace: print "lprClass***Cre ating a socketobj instance"
self.socketobj= socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
if self._trace: print "lprClass***Con ecting to port 515 on server"
self.socketobj. connect((self.s erver, 515))
if self._trace: print "lprClass***Ini tializing LPR daemon for job to printer=", self.printer
self.socketobj. send("\002"+sel f.printer+"\012 ")
d=self.socketob j.recv(1024)
#
# Wait for reply and check response string
#
errormsg="lprCl ass***Error initializing communications with LPR daemon"
if d != "\000": self._abort(d, errormsg)
return

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

#------------------------------------------------------------------------------------
# Send the receive control file subcommand
#
if self._trace: print "lprClass***Sen ding the receive control file subcommand"
self.socketobj. send("\002"+str (len(control))+ " cfA000"+self.ho st+"\012")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Wai ting for reply"
d=self.socketob j.recv(1024)
errormsg="lprCl ass***Error in control string initialization for LPR daemon"
if d != "\000": self._abort(d, errormsg)
#------------------------------------------------------------------------------------
# Send the control file
#
if self._trace: print "lprClass***Sen ding the control file command"
self.socketobj. send(control)
self.socketobj. send("\000")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Wai ting for reply"
d=self.socketob j.recv(1024)
errormsg="lprCl ass***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.socketob j.recv(1024)
errormsg="lprCl ass***Error in _closelpr"
if d != "\000": self._abort(d, errormsg)
#
# Close the socket connection
#
self.socketobj. close()
return

def _sendheader(sel f, length):
#------------------------------------------------------------------------------------
# Send the receive data file subcommand
#
if self._trace: print "lprClass***Sen ding the receive data file subcommand"
self.socketobj. send("\003"+str (length)+" dfA000"+self.ho st+"\012")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Wai ting for reply"
d=self.socketob j.recv(1024)
errormsg="lprCl ass***Error in receive data file subcommand for LPR daemon"
if d != "\000": self._abort(d, errormsg)
return
def queuefile(self, filename):
try:
ifile=open(file name,'r')
except:
print "lprClass***Una ble to find filename=%s to print" % filename
return
#
# Call routine to initialize LPR daemon with control information
#
self._initcontr ol(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***que uefile-length=",length
#
# Send file header to LPR
#
self._sendheade r(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(sel f, string):
#
# Call routine to initialize LPR daemon with control information
#
self._initcontr ol('')
self._sendheade r(len(string))
self.socketobj. send(string)
self._closelpr( )
return

if __name__=="__ma in__":
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
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
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
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.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Aug 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1520
by: Mario | last post by:
Hello, I am a new Python entusiast, I am studing it since some weeks and I think it's a great language and I was very happy with it, but when I came to make a printer routine I really got demotivated by the difficulty, why does Python covers easily the most complicated tasks and on the printer there is this big hole? I was looking on the web, on newsgroups, to find some good solution but I couldn't believe that there is this big lack on...
0
3282
by: KohlerTommy | last post by:
In my application I need to give the user the ability to print duplex if the selected printer supports duplex printing. Many of the printer options do not make much sense in my application, and many of the settings in the common printer dialog would have a negative impact on my printing process. To handle this strict printing constraint on which my application imposes I do not want to show the common print dialog. I want to be able to...
16
48886
by: cyranoVR | last post by:
This is the approach I used to automate printing of Microsoft Access reports to PDF format i.e. unattended and without annoying "Save As..." dialogs, and - more importantly - without having to use a commercial program such as Adobe Acrobat and its associated API. The technique uses Ghostscript and Redirection Port Monitor - two free programs for creating PDF documents provided free by Russell Lang. The actual automation requires VBA...
4
6478
by: Suzanka | last post by:
Hello, I have an application written in C# on visual studio .NET. It is a web aplication. The application consists of many different forms, that users occassionaly want to print out for filing. When they log to application (through web browser) and choose the print option, on the right margin few cm get cut off (so some fields do not print out). Is there any function that ensure that when user pritns he gets the
0
3256
by: Tessa | last post by:
Is there any security reason why you cannot print to a network printer from ASP.NET under IIS6 on Windows 2003 server? I'm using ASP.NET code to print to a server print queue using PrintDocument.Print() (.NET framework v 1.1) I can print to a local printer plugged into LPT1 on the web server, but not to a network printer. The same printing code to a network printer works in a .NET web app when
4
4041
by: Grahammer | last post by:
This is a stupid question, but the examples I've seen don't make sense to me... I simply want to print the contents of a textbox to a printer when the user clicks a button on my form. Printing to the default printer is fine, but eventually I will want the user to be able to choose a printer (not necessarily at print time). Can someone show or link me to a simple sample of the CLICK event for a print button that just dumps the contents...
4
2011
by: Charlie | last post by:
I need to print using the VB.net printing classes to a tractor feed printer. My client loaned me a Panasonic kx-p2123 to set up the job. I was able to print from Notepad using the Generic/Text Only driver, but not from VB. The printer specific driver is available in the Win2k driver list, but when using that driver, the printer prints a few characters of jibberish then feeds to a new page and prints a new line of jibberish. It does this...
3
6319
by: Mika M | last post by:
Hi all! I have made an application for printing simple barcode labels using PrintDocument object, and it's working fine. Barcode printer that I use is attached to the computer, and this computer has drivers installed for this printer, and this printer is shared for the network. Question 1:
5
6485
by: Raman | last post by:
Hello friends, I want to print an ID card. I have one Windows Form that contains front and back side. The printer is printing both front and back side at a time. I am trying to send both sides at a time. But it is printing front side on one card and back side on second card. I want to print both sides in a same card.
18
11314
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing classes, for each of the checked rows in the GridView. This works fine in the Visual Studio 2005 development environment on localhost. But, when I move the page to the web server, I get the error "Settings to access printer...
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.