1 """GNUmed printing."""
2
3 __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
4 __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
5
6
7 import logging
8 import sys
9 import os
10 import subprocess
11 import codecs
12 import time
13
14
15 if __name__ == '__main__':
16 sys.path.insert(0, '../../')
17 from Gnumed.pycommon import gmShellAPI
18 from Gnumed.pycommon import gmTools
19
20
21 _log = logging.getLogger('gm.printing')
22
23
24 known_printjob_types = [
25 u'medication_list',
26 u'generic_document'
27 ]
28
29 external_print_APIs = [
30 u'gm-print_doc',
31 u'os_startfile',
32 u'gsprint',
33 u'acrobat_reader',
34 u'gtklp',
35 u'Internet_Explorer',
36 u'Mac_Preview'
37 ]
38
39
40
41
42 -def print_files(filenames=None, jobtype=None, print_api=None):
43
44 _log.debug('printing "%s": %s', jobtype, filenames)
45
46 if jobtype not in known_printjob_types:
47 print "unregistered print job type <%s>" % jobtype
48 _log.warning('print job type "%s" not registered', jobtype)
49
50 if print_api not in external_print_APIs:
51 _log.warning('print API "%s" unknown, trying all', print_api)
52
53 if print_api == u'os_startfile':
54 return _print_files_by_os_startfile(filenames = filenames)
55 elif print_api == u'gm-print_doc':
56 return _print_files_by_shellscript(filenames = filenames, jobtype = jobtype)
57 elif print_api == u'gsprint':
58 return _print_files_by_gsprint_exe(filenames = filenames)
59 elif print_api == u'acrobat_reader':
60 return _print_files_by_acroread_exe(filenames = filenames)
61 elif print_api == u'gtklp':
62 return _print_files_by_gtklp(filenames = filenames)
63 elif print_api == u'Internet_Explorer':
64 return _print_files_by_IE(filenames = filenames)
65 elif print_api == u'Mac_Preview':
66 return _print_files_by_mac_preview(filenames = filenames)
67
68
69 if (sys.platform == 'darwin') or (os.name == 'mac'):
70 if _print_files_by_mac_preview(filenames = filenames):
71 return True
72 elif os.name == 'posix':
73 if _print_files_by_gtklp(filenames = filenames):
74 return True
75 elif os.name == 'nt':
76 if _print_files_by_shellscript(filenames = filenames, jobtype = jobtype):
77 return True
78 if _print_files_by_gsprint_exe(filenames = filenames):
79 return True
80 if _print_files_by_acroread_exe(filenames = filenames):
81 return True
82 if _print_files_by_os_startfile(filenames = filenames):
83 return True
84 if _print_files_by_IE(filenames = filenames):
85 return True
86 return False
87
88 if _print_files_by_shellscript(filenames = filenames, jobtype = jobtype):
89 return True
90
91 return False
92
93
94
96
97
98 if sys.platform != 'darwin':
99 _log.debug('MacOSX <open> only available under MacOSX/Darwin')
100 return False
101
102 for filename in filenames:
103 cmd_line = [
104 r'open',
105 r'-a Preview',
106 filename
107 ]
108 _log.debug('printing with %s' % cmd_line)
109 try:
110 mac_preview = subprocess.Popen(cmd_line)
111 except OSError:
112 _log.debug('cannot run <open -a Preview>')
113 return False
114 mac_preview.communicate()
115 if mac_preview.returncode != 0:
116 _log.error('<open -a Preview> returned [%s], failed to print', mac_preview.returncode)
117 return False
118
119 return True
120
122
123 if os.name != 'nt':
124 _log.debug('Internet Explorer only available under Windows')
125 return False
126
127 try:
128 from win32com import client as dde_client
129 except ImportError:
130 _log.exception('<win32com> Python module not available for use in printing')
131 return False
132
133 i_explorer = dde_client.Dispatch("InternetExplorer.Application")
134
135 for filename in filenames:
136 if i_explorer.Busy:
137 time.sleep(1)
138 i_explorer.Navigate(os.path.normpath(filename))
139 if i_explorer.Busy:
140 time.sleep(1)
141 i_explorer.Document.printAll()
142
143 i_explorer.Quit()
144 return True
145
147
148
149 if sys.platform != 'linux2':
150 _log.debug('<gtklp> only available under Linux')
151 return False
152
153 cmd_line = [
154 r'gtklp',
155 r'-i',
156 r'-# 1'
157 ]
158 cmd_line.extend(filenames)
159 _log.debug('printing with %s' % cmd_line)
160 try:
161 gtklp = subprocess.Popen(cmd_line)
162 except OSError:
163 _log.debug('cannot run <gtklp>')
164 return False
165 gtklp.communicate()
166 if gtklp.returncode != 0:
167 _log.error('<gtklp> returned [%s], failed to print', gtklp.returncode)
168 return False
169
170 return True
171
173 """Use gsprint.exe from Ghostscript tools. Windows only.
174
175 - docs: http://pages.cs.wisc.edu/~ghost/gsview/gsprint.htm
176 - download: http://www.cs.wisc.edu/~ghost/
177 """
178 if os.name != 'nt':
179 _log.debug('<gsprint.exe> only available under Windows')
180 return False
181
182 conf_filename = gmTools.get_unique_filename (
183 prefix = 'gm2gsprint-',
184 suffix = '.cfg'
185 ).encode(sys.getfilesystemencoding())
186
187 for filename in filenames:
188 conf_file = codecs.open(conf_filename, 'wb', 'utf8')
189 conf_file.write('-color\n')
190 conf_file.write('-query\n')
191 conf_file.write('-all\n')
192 conf_file.write('-copies 1\n')
193 conf_file.write('%s\n' % os.path.normpath(filename))
194 conf_file.close()
195
196 cmd_line = [
197 r'gsprint.exe',
198 r'-config "%s"' % conf_filename
199 ]
200 _log.debug('printing with %s' % cmd_line)
201 try:
202 gsprint = subprocess.Popen(cmd_line)
203 except OSError:
204 _log.debug('cannot run <gsprint.exe>')
205 return False
206 gsprint.communicate()
207 if gsprint.returncode != 0:
208 _log.error('<gsprint.exe> returned [%s], failed to print', gsprint.returncode)
209 return False
210
211 return True
212
214 """Use Adobe Acrobat Reader. Windows only.
215
216 - docs: http://www.robvanderwoude.com/printfiles.php#PrintPDF
217 """
218 if os.name != 'nt':
219 _log.debug('Acrobat Reader only used under Windows')
220 return False
221
222 for filename in filenames:
223 cmd_line = [
224 r'AcroRd32.exe',
225 r'/s',
226 r'/o',
227 r'/h',
228 r'/p',
229 os.path.normpath(filename)
230 ]
231 _log.debug('printing with %s' % cmd_line)
232 try:
233 acroread = subprocess.Popen(cmd_line)
234 except OSError:
235 _log.debug('cannot run <AcroRd32.exe>')
236 cmd_line[0] = r'acroread.exe'
237 _log.debug('printing with %s' % cmd_line)
238 try:
239 acroread = subprocess.Popen(cmd_line)
240 except OSError:
241 _log.debug('cannot run <acroread.exe>')
242 return False
243
244 acroread.communicate()
245 if acroread.returncode != 0:
246 _log.error('Acrobat Reader returned [%s], failed to print', acroread.returncode)
247 return False
248
249 return True
250
252
253 try:
254 os.startfile
255 except AttributeError:
256 _log.error('platform does not support "os.startfile()"')
257 return False
258
259 _log.debug('printing [%s]', filenames)
260
261 for filename in filenames:
262 fname = os.path.normcase(os.path.normpath(filename))
263 _log.debug('%s -> %s', filename, fname)
264 try:
265 os.startfile(fname, 'print')
266 except OSError:
267 _log.exception('cannot os.startfile()')
268
269 return True
270
272
273 paths = gmTools.gmPaths()
274 local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc')
275
276
277 candidates = [u'gm-print_doc', local_script, u'gm-print_doc.bat']
278 found, binary = gmShellAPI.find_first_binary(binaries = candidates)
279 if not found:
280 binary = r'gm-print_doc.bat'
281
282 cmd_line = [
283 binary,
284 jobtype
285 ]
286 cmd_line.extend(filenames)
287 _log.debug('printing with %s', cmd_line)
288 try:
289 gm_print_doc = subprocess.Popen(cmd_line)
290 except OSError:
291 _log.debug('cannot run <gm_print_doc(.bat)>')
292 return False
293 gm_print_doc.communicate()
294 if gm_print_doc.returncode != 0:
295 _log.error('<gm_print_doc> returned [%s], failed to print', gm_print_doc.returncode)
296 return False
297
298 return True
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 if __name__ == '__main__':
317
318 if len(sys.argv) < 2:
319 sys.exit()
320
321 if sys.argv[1] != 'test':
322 sys.exit()
323
324 from Gnumed.pycommon import gmLog2
325 from Gnumed.pycommon import gmI18N
326 gmI18N.activate_locale()
327 gmI18N.install_domain()
328
329
332
334 print_files(filenames = [sys.argv[2], sys.argv[2]], jobtype = u'generic_document', print_api = 'gm-print_doc')
335
337 print_files(filenames = [sys.argv[2], sys.argv[2]], jobtype = u'generic_document', print_api = u'gtklp')
338
340 print "testing printing via Mac Preview"
341 _print_files_by_mac_preview(filenames = [sys.argv[0]])
342
343
344
345 test_print_files_by_mac_preview()
346
347
348