-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprinter.py
282 lines (258 loc) · 9.82 KB
/
printer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env
# -*- coding: utf-8 -*-
"""
Simply send in the options you want in Printer.__init__
and then override printThis() to do what you want.
DONE!
Ready to run it with run()
"""
import typing
import os
import sys
import subprocess
from virtualPrinter.printServer import PrintCallbackDocType
from virtualPrinter.printerException import PrinterException
# get the location of ghostscript
if os.name=='nt':
GHOSTSCRIPT_APP=None
for gsDir in (
os.environ['ProgramFiles']+os.sep+'gs',
os.environ['ProgramFiles(x86)']+os.sep+'gs'
):
if os.path.isdir(gsDir):
# find the newest version
bestVersion:float=0.0
val:float
for f in os.listdir(gsDir):
path=gsDir+os.sep+f
if os.path.isdir(path) and f.startswith('gs'):
try:
val=float(f[2:])
except ValueError:
val=0.0
if bestVersion<val:
for appName in (
'gswin64c.exe',
'gswin32c.exe',
'gswin.exe',
'gs.exe'
):
appName=gsDir+os.sep+f+os.sep+'bin'+os.sep+appName
if os.path.isfile(appName):
bestVersion=val
GHOSTSCRIPT_APP='"'+appName+'"'
break
if GHOSTSCRIPT_APP is None:
errString="""ERR: Ghostscript not found!
You can get it from:
http://www.ghostscript.com"""
raise PrinterException(errString)
else: # assume we can find it
GHOSTSCRIPT_APP='gs'
print(f'GHOSTSCRIPT_APP={GHOSTSCRIPT_APP}')
# find a good shell_escape routine
try:
import shlex
if hasattr(shlex,'quote'):
shell_escape=shlex.quote
else:
import pipes # pylint: disable=deprecated-module
shell_escape=pipes.quote
except ImportError:
import pipes # pylint: disable=deprecated-module
shell_escape=pipes.quote
class Printer:
"""
You can derive from this class to create your own printer!
Simply send in the options you want in Printer.__init__
and then override printThis() to do what you want.
DONE!
Ready to run it with run()
"""
def __init__(self,
name:str='My Virtual Printer',
acceptsFormat:str='png',
acceptsColors:str='rgba'):
"""
name - the name of the printer to be installed
acceptsFormat - the format that the printThis() method accepts
Available formats are "pdf", or "png" (default=png)
acceptsColors - the color format that the printThis() method accepts
(if relevent to acceptsFormat)
Available colors are "grey", "rgb", or "rgba" (default=rgba)
"""
from virtualPrinter.printServer import PrintServer
self._server:typing.Optional[PrintServer]=None
self.name:str=name
self.acceptsFormat:str=acceptsFormat
self.acceptsColors:str=acceptsColors
self.bgColor:str='#ffffff' # not sure how necessary this is
def printThis(self,
doc:PrintCallbackDocType,
title:typing.Optional[str]=None,
author:typing.Optional[str]=None,
filename:typing.Optional[str]=None
)->None:
"""
you probably want to override this
called when something is being printed
defaults to saving a file
TODO: keep track of filename?
"""
_=filename # For now, don't care
if title is None:
title='printed'
if author is not None:
title=title+' - '+author
with open(shell_escape(title+'.'+self.acceptsFormat),'wb') as f:
f.write(doc)
def run(self,
host:str='127.0.0.1',
port:typing.Union[None,int,str]=None,
autoInstallPrinter:bool=True
)->None:
"""
normally all the default values are exactly what you need!
autoInstallPrinter is used to install the printer in the OS
(currently only supports Windows)
startServer is required for this
"""
from virtualPrinter.printServer import PrintServer
self._server=PrintServer(
self.name,host,port,autoInstallPrinter,self._printServerCallback)
self._server.run()
del self._server # delete it so it gets un-registered
self._server=None
def _printServerCallback(self,
dataSource:PrintCallbackDocType,
title:typing.Optional[str]=None,
author:typing.Optional[str]=None,
filename:typing.Optional[str]=None
):
"""
Default callback, turns around and calls
printPostscript() with the data given to it
"""
self.printPostscript(dataSource,False,title,author,filename)
def _postscriptToFormat(self,
data,
gsDev:str='pdfwrite',
gsDevOptions:typing.Optional[typing.Iterable[str]]=None,
outputDebug:bool=True
)->str:
"""
Converts postscript data (in a string) to pdf data (in a string)
gsDev is a ghostscript format device
For ghostscript command line, see also:
http://www.ghostscript.com/doc/current/Devices.htm
http://www.ghostscript.com/doc/current/Use.htm#Options
"""
if GHOSTSCRIPT_APP is None:
msg="ghostscript is inaccessible. Is it installed?"
raise PrinterException(msg)
cmd:typing.List[str]=[GHOSTSCRIPT_APP,'-q','-sDEVICE='+gsDev]
if gsDevOptions is not None:
cmd.extend(gsDevOptions)
cmd.extend((r'-sstdout=%stderr','-sOutputFile=-','-dBATCH','-'))
if outputDebug:
print(' '.join(cmd))
with subprocess.Popen(cmd,
stdin=subprocess.PIPE,stderr=subprocess.PIPE,
stdout=subprocess.PIPE,shell=True) as po:
data,gsStdoutStderr=po.communicate(input=data)
if outputDebug:
# note: stdout also goes to stderr because of
# the -sstdout=%stderr flag above
print(gsStdoutStderr)
return data
def printPostscript(self,
datasource:PrintCallbackDocType,
datasourceIsFilename:bool=False,
title:typing.Optional[str]=None,
author:typing.Optional[str]=None,
filename:typing.Optional[str]=None
)->None:
"""
datasource is either:
a filename
None to get data from stdin
a file-like object
something else to convert using str() and then print
Keep in mind that it MUST contain postscript data
"""
# -- open data source
data=None
if datasource is None:
data=sys.stdin.read()
elif isinstance(datasource,str):
if datasourceIsFilename:
with open(datasource,
'rb',
encoding='utf-8',
errors='ignore') as f: # noqa: E129
data=f.read()
if title is None:
title=datasource.rsplit(os.sep,1)[-1].rsplit('.',1)[0]
else:
data=datasource
elif hasattr(datasource,'read'):
data=datasource.read()
else:
data=str(datasource)
# -- convert the data to the required format
print('Converting data...')
gsDevOptions=[]
if self.acceptsFormat=='pdf':
gsDev='pdfwrite'
elif self.acceptsFormat=='png':
gsDevOptions.append('-r600')
gsDevOptions.append('-dDownScaleFactor=3')
if self.acceptsColors=='grey':
gsDev='pnggray'
elif self.acceptsColors=='rgba':
if self.bgColor is None: # how necessary is this?
self.bgColor='#ffffff'
gsDev='pngalpha'
if self.bgColor is not None:
if self.bgColor[0]!='#':
self.bgColor='#'+self.bgColor
gsDevOptions.append('-dBackgroundColor=16'+self.bgColor)
#elif self.acceptsColors=='rgb': #TODO
else:
msg=f'Unknown color format "{self.acceptsColors}"'
raise PrinterException(msg)
else:
msg=r'Unacceptable data type format "{self.acceptsFormat}"'
raise PrinterException(msg)
data=self._postscriptToFormat(data,gsDev,gsDevOptions)
# -- send the data to the printThis function
print('Printing data...')
self.printThis(data,title=title,author=author,filename=filename)
if __name__=='__main__':
usage="""
USAGE:
virtualPrinter filename.ps ..... print a file
virtualPrinter - ............... print postscript piped in from stdin
virtualPrinter ip[:port]........ start a print server
NOTE:
you can do multiple commands with the same virtualPrinter
"""
if len(sys.argv)<2:
print(usage)
else:
p=Printer()
for arg in sys.argv[1:]:
if arg=='-':
p.printPostscript(sys.stdin)
elif arg.find(os.sep)<0 and len(sys.argv[1].split('.'))>2:
# looks like an ip to me!
ipPort=arg.rsplit(':',1)
port:typing.Union[None,str,int]
if len(ipPort)>1:
port=ipPort[-1]
else:
port=None
ip=ipPort[0]
p.run(ip,port)
else:
p.printPostscript(arg,True)