Robochameleon  v1.0
pdftops.m
1 function varargout = pdftops(cmd)
2 %PDFTOPS Calls a local pdftops executable with the input command
3 %
4 % Example:
5 % [status result] = pdftops(cmd)
6 %
7 % Attempts to locate a pdftops executable, finally asking the user to
8 % specify the directory pdftops was installed into. The resulting path is
9 % stored for future reference.
10 %
11 % Once found, the executable is called with the input command string.
12 %
13 % This function requires that you have pdftops (from the Xpdf package)
14 % installed on your system. You can download this from:
15 % http://www.foolabs.com/xpdf
16 %
17 % IN:
18 % cmd - Command string to be passed into pdftops.
19 %
20 % OUT:
21 % status - 0 iff command ran without problem.
22 % result - Output from pdftops.
23 
24 % Copyright: Oliver Woodford, 2009-2010
25 
26 % Thanks to Jonas Dorn for the fix for the title of the uigetdir window on
27 % Mac OS.
28 % Thanks to Christoph Hertel for pointing out a bug in check_xpdf_path
29 % under linux.
30 % 23/01/2014 - Add full path to pdftops.txt in warning.
31 
32 % Call pdftops
33 [varargout{1:nargout}] = system(sprintf('"%s" %s', xpdf_path, cmd));
34 return
35 
36 function path_ = xpdf_path
37 % Return a valid path
38 % Start with the currently set path
39 path_ = user_string('pdftops');
40 % Check the path works
41 if check_xpdf_path(path_)
42  return
43 end
44 % Check whether the binary is on the path
45 if ispc
46  bin = 'pdftops.exe';
47 else
48  bin = 'pdftops';
49 end
50 if check_store_xpdf_path(bin)
51  path_ = bin;
52  return
53 end
54 % Search the obvious places
55 if ispc
56  path_ = 'C:\Program Files\xpdf\pdftops.exe';
57 else
58  path_ = '/usr/local/bin/pdftops';
59 end
60 if check_store_xpdf_path(path_)
61  return
62 end
63 % Ask the user to enter the path
64 while 1
65  if strncmp(computer,'MAC',3) % Is a Mac
66  % Give separate warning as the uigetdir dialogue box doesn't have a
67  % title
68  uiwait(warndlg('Pdftops not found. Please locate the program, or install xpdf-tools from http://users.phg-online.de/tk/MOSXS/.'))
69  end
70  base = uigetdir('/', 'Pdftops not found. Please locate the program.');
71  if isequal(base, 0)
72  % User hit cancel or closed window
73  break;
74  end
75  base = [base filesep];
76  bin_dir = {'', ['bin' filesep], ['lib' filesep]};
77  for a = 1:numel(bin_dir)
78  path_ = [base bin_dir{a} bin];
79  if exist(path_, 'file') == 2
80  break;
81  end
82  end
83  if check_store_xpdf_path(path_)
84  return
85  end
86 end
87 error('pdftops executable not found.');
88 
89 function good = check_store_xpdf_path(path_)
90 % Check the path is valid
91 good = check_xpdf_path(path_);
92 if ~good
93  return
94 end
95 % Update the current default path to the path found
96 if ~user_string('pdftops', path_)
97  warning('Path to pdftops executable could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'pdftops.txt'));
98  return
99 end
100 return
101 
102 function good = check_xpdf_path(path_)
103 % Check the path is valid
104 [good, message] = system(sprintf('"%s" -h', path_));
105 % system returns good = 1 even when the command runs
106 % Look for something distinct in the help text
107 good = ~isempty(strfind(message, 'PostScript'));
108 return