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