1 """GNUmed printing."""
2
3 __version__ = "$Revision: 1.4 $"
4 __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
5 __license__ = 'GPL (details at http://www.gnu.org)'
6
7
8 import logging
9 import sys
10 import os
11 import subprocess
12 import codecs
13 import time
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 _log.info(__version__)
24
25
26 known_printjob_types = [
27 u'medication_list',
28 u'generic_document'
29 ]
30
31 external_print_APIs = [
32 u'gm-print_doc',
33 u'os_startfile',
34 u'gsprint',
35 u'acrobat_reader',
36 u'gktlp',
37 u'Internet_Explorer',
38 u'Mac_Preview'
39 ]
40
41
42
43
44 -def print_file(filename=None, jobtype=None, print_api=None):
45
46 _log.debug('printing "%s": [%s]', jobtype, filename)
47
48 if jobtype not in known_printjob_types:
49 print "unregistered print job type <%s>" % jobtype
50 _log.warning('print job type "%s" not registered', jobtype)
51
52 if print_api not in external_print_APIs:
53 _log.warning('print API "%s" unknown, trying all', print_api)
54
55 if print_api == u'os_startfile':
56 return _print_file_by_os_startfile(filename = filename)
57 elif print_api == u'gm-print_doc':
58 return _print_file_by_shellscript(filename = filename, jobtype = jobtype)
59 elif print_api == u'gsprint':
60 return _print_file_by_gsprint_exe(filename = filename)
61 elif print_api == u'acrobat_reader':
62 return _print_file_by_acroread_exe(filename = filename)
63 elif print_api == u'gtklp':
64 return _print_file_by_gtklp(filename = filename)
65 elif print_api == u'Internet_Explorer':
66 return _print_file_by_IE(filename = filename)
67 elif print_api == u'Mac_Preview':
68 return _print_file_by_mac_preview(filename = filename)
69
70
71 if (sys.platform == 'darwin') or (os.name == 'mac'):
72 if _print_file_by_mac_preview(filename = filename):
73 return True
74 elif os.name == 'posix':
75 if _print_file_by_gtklp(filename = filename):
76 return True
77 elif os.name == 'nt':
78 if _print_file_by_gsprint_exe(filename = filename):
79 return True
80 if _print_file_by_acroread_exe(filename = filename):
81 return True
82 if _print_file_by_os_startfile(filename = filename):
83 return True
84 if _print_file_by_IE(filename = filename):
85 return True
86
87 if _print_file_by_shellscript(filename = filename, jobtype = jobtype):
88 return True
89
90 return False
91
92
93
95
96 if sys.platform != 'darwin':
97
98 _log.debug('MacOSX <open> only available under MacOSX/Darwin')
99 return False
100
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, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
109 except OSError:
110 _log.debug('cannot run <open -a Preview>')
111 return False
112
113 stdout, stderr = mac_preview.communicate()
114 if mac_preview.returncode != 0:
115 _log.error('<open -a Preview> returned [%s], failed to print', mac_preview.returncode)
116 return False
117
118 return True
119
121
122 if os.name != 'nt':
123 _log.debug('Internet Explorer only available under Windows')
124 return False
125
126 try:
127 from win32com import client as dde_client
128 except ImportError:
129 _log.exception('<win32com> Python module not available for use in printing')
130 return False
131
132 i_explorer = dde_client.Dispatch("InternetExplorer.Application")
133 i_explorer.Navigate(os.path.normpath(filename))
134 if i_explorer.Busy:
135 time.sleep(1)
136 i_explorer.Document.printAll()
137 i_explorer.Quit()
138
139 return True
140
142
143 if sys.platform != 'linux2':
144
145 _log.debug('<gtklp> only available under Linux')
146 return False
147
148 cmd_line = [
149 r'gtklp',
150 r'-i',
151 r'-# 1',
152 filename
153 ]
154 _log.debug('printing with %s' % cmd_line)
155 try:
156 gtklp = subprocess.Popen(cmd_line, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
157 except OSError:
158 _log.debug('cannot run <gtklp>')
159 return False
160
161 stdout, stderr = gtklp.communicate()
162 if gtklp.returncode != 0:
163 _log.error('<gtklp> returned [%s], failed to print', gtklp.returncode)
164 return False
165
166 return True
167
169 """Use gsprint.exe from Ghostscript tools. Windows only.
170
171 - docs: http://pages.cs.wisc.edu/~ghost/gsview/gsprint.htm
172 - download: http://www.cs.wisc.edu/~ghost/
173 """
174 if os.name != 'nt':
175 _log.debug('<gsprint.exe> only available under Windows')
176 return False
177
178 conf_filename = gmTools.get_unique_filename(prefix = 'gm2gsprint-', suffix = '.cfg')
179 conf_file = codecs.open(conf_filename, 'wb', 'utf8')
180 conf_file.write('-color\n')
181 conf_file.write('-query\n')
182 conf_file.write('-all\n')
183 conf_file.write('-copies 1\n')
184 conf_file.write('%s\n' % os.path.normpath(filename))
185 conf_file.close()
186
187
188 cmd_line = [ r'gsprint.exe', r'-config "%s"' % conf_filename ]
189 _log.debug('printing with %s' % cmd_line)
190 try:
191 gsprint = subprocess.Popen(cmd_line, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
192 except OSError:
193 _log.debug('cannot run <gsprint.exe>')
194 return False
195
196 stdout, stderr = gsprint.communicate()
197 if gsprint.returncode != 0:
198 _log.error('<gsprint.exe> returned [%s], failed to print', gsprint.returncode)
199 return False
200
201 return True
202
204 """Use Adobe Acrobat Reader. Windows only.
205
206 - docs: http://www.robvanderwoude.com/printfiles.php#PrintPDF
207 """
208 if os.name != 'nt':
209 _log.debug('Acrobat Reader only used under Windows')
210 return False
211
212 cmd_line = [
213 r'AcroRd32.exe',
214 r'/s',
215 r'/o',
216 r'/h',
217 r'/p',
218 os.path.normpath(filename)
219 ]
220 _log.debug('printing with %s' % cmd_line)
221 try:
222 acroread = subprocess.Popen(cmd_line, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
223 except OSError:
224 _log.debug('cannot run <AcroRd32.exe>')
225 cmd_line[0] = r'acroread.exe'
226 _log.debug('printing with %s' % cmd_line)
227 try:
228 acroread = subprocess.Popen(cmd_line, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
229 except OSError:
230 _log.debug('cannot run <acroread.exe>')
231 return False
232
233 stdout, stderr = acroread.communicate()
234 if acroread.returncode != 0:
235 _log.error('Acrobat Reader returned [%s], failed to print', acroread.returncode)
236 return False
237
238 return True
239
241
242 _log.debug('printing [%s]', filename)
243
244 try:
245 os.startfile(filename, 'print')
246 except AttributeError:
247 _log.exception('platform does not support "os.startfile()", cannot print')
248 return False
249
250 return True
251
253
254 paths = gmTools.gmPaths()
255 local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc')
256
257 candidates = [u'gm-print_doc', u'gm-print_doc.bat', local_script, u'gm-print_doc.bat']
258 args = u' %s %s' % (jobtype, filename)
259
260 success = gmShellAPI.run_first_available_in_shell (
261 binaries = candidates,
262 args = args,
263 blocking = True,
264 run_last_one_anyway = True
265 )
266
267 if success:
268 return True
269
270 _log.error('print command failed')
271 return False
272
273
274
275 if __name__ == '__main__':
276
277 if len(sys.argv) < 2:
278 sys.exit()
279
280 if sys.argv[1] != 'test':
281 sys.exit()
282
283 from Gnumed.pycommon import gmLog2
284 from Gnumed.pycommon import gmI18N
285 gmI18N.activate_locale()
286 gmI18N.install_domain()
287
288
291
292
293 print test_print_file()
294
295
296