-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMSWinPrint.py
284 lines (241 loc) · 8.69 KB
/
MSWinPrint.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
283
284
# MSWinPrint.py
# Copyright (c) 2004-2018 Chris Gonnerman
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer. Redistributions in binary
# form must reproduce the above copyright notice, this list of conditions and
# the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""MSWinPrint -- Windows Printing Subsystem
MSWinPrint defines a class for use by programs needing to print complex
output on Windows 2K/XP/2003 hosts.
document is a class for creating and running print jobs.
listprinters() returns a list of printer names. The default printer is the
first element of the list, and all other printers follow in alphabetical order.
desc(printer) returns a dictionary containing the descriptive fields
for the named printer.
getfont(name, size, weight, italic = 0) returns a win32ui font object for the
named font scaled to the given size. Font substitution may have been done by
Windows, so don't be surprised if you don't get what you asked for.
"""
# "constants" for use with printer setup calls
HORZRES = 8
VERTRES = 10
LOGPIXELSX = 88
LOGPIXELSY = 90
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
import win32gui, win32ui, win32print, win32con, win32api
try:
from PIL import ImageWin
except:
ImageWin = None
scale_factor = 20
prdict = None
paper_sizes = {
"letter": 1,
"lettersmall": 2,
"tabloid": 3,
"ledger": 4,
"legal": 5,
"statement": 6,
"executive": 7,
"a3": 8,
"a4": 9,
"envelope9": 19,
"envelope10": 20,
"envelope11": 21,
"envelope12": 22,
"envelope14": 23,
"fanfold": 39,
}
orientations = {
"portrait": 1,
"landscape": 2,
}
duplexes = {
"normal": 1,
"none": 1,
"long": 2,
"short": 3,
}
class document:
def __init__(self, printer = None, papersize = None, orientation = None, duplex = None):
self.dc = None
self.font = None
self.printer = printer
self.papersize = papersize
self.orientation = orientation
self.page = 0
self.duplex = duplex
def scalepos(self, pos):
rc = []
for i in range(len(pos)):
p = pos[i]
if i % 2:
p *= -1
rc.append(int(p * scale_factor))
return tuple(rc)
def begin_document(self, desc = "MSWinPrint.py print job"):
# open the printer
if self.printer is None:
self.printer = win32print.GetDefaultPrinter()
self.hprinter = win32print.OpenPrinter(self.printer)
# load default settings
devmode = win32print.GetPrinter(self.hprinter, 8)["pDevMode"]
# change paper size and orientation
if self.papersize is not None:
if type(self.papersize) is int:
devmode.PaperSize = self.papersize
else:
devmode.PaperSize = paper_sizes[self.papersize]
if self.orientation is not None:
devmode.Orientation = orientations[self.orientation]
if self.duplex is not None:
devmode.Duplex = duplexes[self.duplex]
# create dc using new settings
self.hdc = win32gui.CreateDC("WINSPOOL", self.printer, devmode)
self.dc = win32ui.CreateDCFromHandle(self.hdc)
# self.dc = win32ui.CreateDC()
# if self.printer is not None:
# self.dc.CreatePrinterDC(self.printer)
# else:
# self.dc.CreatePrinterDC()
self.dc.SetMapMode(win32con.MM_TWIPS) # hundredths of inches
self.dc.StartDoc(desc)
self.dc.SetBkMode(win32con.TRANSPARENT)
self.pen = win32ui.CreatePen(0, int(scale_factor), 0L)
self.dc.SelectObject(self.pen)
win32gui.SetBkMode(self.hdc, 1) # transparent
self.page = 1
def end_document(self):
if self.page == 0:
return # document was never started
self.dc.EndDoc()
del self.dc
def end_page(self):
if self.page == 0:
return # nothing on the page
# endpage gets stupid if the page is completely blank
self.text((1, 1), " ")
self.dc.EndPage()
self.page += 1
def getsize(self):
if self.page == 0:
self.begin_document()
# returns printable (width, height) in points
width = float(self.dc.GetDeviceCaps(HORZRES)) * (72.0 / self.dc.GetDeviceCaps(LOGPIXELSX))
height = float(self.dc.GetDeviceCaps(VERTRES)) * (72.0 / self.dc.GetDeviceCaps(LOGPIXELSY))
return width, height
def line(self, from_, to):
if self.page == 0:
self.begin_document()
self.dc.MoveTo(self.scalepos(from_))
self.dc.LineTo(self.scalepos(to))
def rectangle(self, box):
if self.page == 0:
self.begin_document()
self.dc.MoveTo(self.scalepos((box[0], box[1])))
self.dc.LineTo(self.scalepos((box[2], box[1])))
self.dc.LineTo(self.scalepos((box[2], box[3])))
self.dc.LineTo(self.scalepos((box[0], box[3])))
self.dc.LineTo(self.scalepos((box[0], box[1])))
def text(self, position, text):
if self.page == 0:
self.begin_document()
self.dc.TextOut(int(scale_factor * position[0]),
int(-1 * scale_factor * position[1]), text)
def setfont(self, name, size, bold = None, italic = None):
if self.page == 0:
self.begin_document()
wt = 400
if bold:
wt = 700
if italic:
italic = 1
else:
italic = 0
self.font = getfont(name, size, wt, italic)
self.dc.SelectObject(self.font)
def image(self, position, image, size):
"print PIL image at position with given size"
if ImageWin is None:
raise NotImplementedError, "PIL required for image method"
if self.page == 0:
self.begin_document()
dib = ImageWin.Dib(image)
endpos = (position[0] + size[0], position[1] + size[1])
dest = (position[0] * scale_factor,
-1 * position[1] * scale_factor,
endpos[0] * scale_factor,
-1 * endpos[1] * scale_factor)
dib.draw(self.hdc, dest)
def setink(self, ink):
win32gui.SetTextColor(self.hdc, win32api.RGB(*ink))
def setfill(self, onoff):
pass
def build_dict():
global prdict
lst = win32print.EnumPrinters(
win32print.PRINTER_ENUM_CONNECTIONS
+ win32print.PRINTER_ENUM_LOCAL)
prdict = {}
for flags, description, name, comment in lst:
prdict[name] = {}
prdict[name]["flags"] = flags
prdict[name]["description"] = description
prdict[name]["comment"] = comment
def listprinters():
dft = win32print.GetDefaultPrinter()
if prdict is None:
build_dict()
keys = prdict.keys()
keys.sort()
rc = [ dft ]
for k in keys:
if k != dft:
rc.append(k)
return rc
def desc(name):
if prdict == None:
listprinters()
return prdict[name]
def getfont(name, size, weight = 400, italic = 0):
if italic:
return win32ui.CreateFont({
"name": name,
"height": scale_factor * size,
"weight": weight,
"italic": italic,
})
else:
return win32ui.CreateFont({
"name": name,
"height": scale_factor * size,
"weight": weight,
})
if __name__ == "__main__":
doc = document(orientation = "landscape")
doc.begin_document()
doc.setfont("Arial", 32)
doc.text((72, 72), "Testing...")
doc.text((72, 72+48), "Testing #2")
doc.rectangle((72, 72, 72*6, 72*3))
doc.line((72, 72), (72*6, 72*3))
doc.end_document()
# end of file.