ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pdftops.m
Go to the documentation of this file.
1function 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: http://xpdfreader.com
15%
16% IN:
17% cmd - Command string to be passed into pdftops (e.g. '-help').
18%
19% OUT:
20% status - 0 iff command ran without problem.
21% result - Output from pdftops.
22
23% Copyright: Oliver Woodford, 2009-2010
24
25% Thanks to Jonas Dorn for the fix for the title of the uigetdir window on Mac OS.
26% Thanks to Christoph Hertel for pointing out a bug in check_xpdf_path under linux.
27% 23/01/2014 - Add full path to pdftops.txt in warning.
28% 27/05/2015 - Fixed alert in case of missing pdftops; fixed code indentation
29% 02/05/2016 - Added possible error explanation suggested by Michael Pacer (issue #137)
30% 02/05/2016 - Search additional possible paths suggested by Jonas Stein (issue #147)
31% 03/05/2016 - Display the specific error message if pdftops fails for some reason (issue #148)
32% 22/09/2018 - Xpdf website changed to xpdfreader.com; improved popup logic
33% 03/02/2019 - Fixed one-off 'pdftops not found' error after install (Mac/Linux) (issue #266)
34% 15/01/2020 - Fixed reported path of pdftops.txt file in case of error; added warning ID
35% 23/07/2020 - Fixed issue #311 (confusion regarding Xpdf-tools download/installation); silent check of pdftops installation in case no input arg specified
36% 21/02/2023 - Don't display error dialog popup and fix messages in deployed apps
37
38 % If no command parameter specified, just check pdftops installation and bail out
39 if nargin < 1
40 xpdf_path(); % this will error if pdftops is not found
41 return % silent bail-out if pdftops was successfully located
42 end
43
44 % Call pdftops
45 [varargout{1:nargout}] = system([xpdf_command(xpdf_path()) cmd]);
46end
47
48function path_ = xpdf_path
49 % Return a valid path
50 % Start with the currently set path
51 path_ = user_string('pdftops');
52 % Check the path works
53 if check_xpdf_path(path_)
54 return
55 end
56 % Check whether the binary is on the path
57 if ispc
58 bin = 'pdftops.exe';
59 else
60 bin = 'pdftops';
61 end
62 if check_store_xpdf_path(bin)
63 path_ = bin;
64 return
65 end
66 % Search the obvious places
67 if ispc
68 paths = {'C:\Program Files\xpdf\pdftops.exe', 'C:\Program Files (x86)\xpdf\pdftops.exe'};
69 else
70 paths = {'/usr/bin/pdftops', '/usr/local/bin/pdftops'};
71 end
72 for a = 1:numel(paths)
73 path_ = paths{a};
74 if check_store_xpdf_path(path_)
75 return
76 end
77 end
78
79 % Ask the user to enter the path
80 errMsg1 = 'Pdftops utility not found. Please locate the program, or install xpdf-tools from ';
81 url1 = 'http://xpdfreader.com/download.html'; %='http://foolabs.com/xpdf';
82 errMsg2 = '"Xpdf command line tools" section';
83 fprintf(2, '%s%s (%s)\n', errMsg1, hyperlink(url1), errMsg2);
84 errMsg1 = [errMsg1 url1];
85 %if strncmp(computer,'MAC',3) % Is a Mac
86 % % Give separate warning as the MacOS uigetdir dialogue box doesn't have a title
87 % uiwait(warndlg(errMsg1))
88 %end
89
90 if isdeployed
91 errMsg2 = ['(' errMsg2 '. Ensure to install xpdf-tools, not XpdfReader)'];
92 errMsg = {errMsg1,errMsg2};
93 else
94 % Provide an alternative possible explanation as per issue #137
95 errMsg2 = 'If pdftops is installed, maybe Matlab is shaddowing it, as described in ';
96 url2 = 'https://github.com/altmany/export_fig/issues/137';
97 fprintf(2, '%s%s\n', errMsg2, hyperlink(url2,'issue #137'));
98 errMsg2 = [errMsg2 url2];
99
100 % Provide an alternative possible explanation as per issue #311
101 errMsg3 = 'Or perhaps you installed XpdfReader but not xpdf-tools, as described in ';
102 url3 = 'https://github.com/altmany/export_fig/issues/311';
103 fprintf(2, '%s%s\n', errMsg3, hyperlink(url3,'issue #311'));
104 errMsg3 = [errMsg3 url3];
105
106 errMsg = {errMsg1,'',errMsg2,'',errMsg3};
107 end
108
109 state = 1;
110 while ~isdeployed % only display the popup dialog if not deployed
111 if state
112 option1 = 'Install pdftops';
113 else
114 option1 = 'Issue #137';
115 end
116 answer = questdlg(errMsg,'Pdftops error',option1,'Locate pdftops','Ignore','Ignore');
117 drawnow; % prevent a Matlab hang: http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem
118 switch answer
119 case 'Install pdftops'
120 web('-browser',url1);
121 state = 0;
122 case 'Issue #137'
123 web('-browser',url2);
124 state = 1;
125 case 'Locate pdftops'
126 base = uigetdir('/', errMsg1);
127 if isequal(base, 0)
128 % User hit cancel or closed window
129 break
130 end
131 base = [base filesep]; %#ok<AGROW>
132 bin_dir = {'', ['bin' filesep], ['lib' filesep]};
133 for a = 1:numel(bin_dir)
134 path_ = [base bin_dir{a} bin];
135 if exist(path_, 'file') == 2
136 break
137 end
138 end
139 if check_store_xpdf_path(path_)
140 return
141 end
142
143 otherwise % User hit Ignore/Cancel or closed window
144 break
145 end
146 end
147 error('pdftops executable not found.');
148end
149
150function good = check_store_xpdf_path(path_)
151 % Check the path is valid
152 good = check_xpdf_path(path_);
153 if ~good
154 return
155 end
156 % Update the current default path to the path found
157 if ~user_string('pdftops', path_)
158 %filename = fullfile(fileparts(which('user_string.m')), '.ignore', 'pdftops.txt');
159 [unused, filename] = user_string('pdftops'); %#ok<ASGLU>
160 warning('export_fig:pdftops','Path to pdftops executable could not be saved. Enter it manually in %s.', filename);
161 return
162 end
163end
164
165function good = check_xpdf_path(path_)
166 % Check the path is valid
167 [good, message] = system([xpdf_command(path_) '-h']); %#ok<ASGLU>
168 % system returns good = 1 even when the command runs
169 % Look for something distinct in the help text
170 good = ~isempty(strfind(message, 'PostScript')); %#ok<STREMP>
171
172 % Display the error message if the pdftops executable exists but fails for some reason
173 % Note: on Mac/Linux, exist('pdftops','file') will always return 2 due to pdftops.m => check for '/','.' (issue #266)
174 if ~good && exist(path_,'file') && ~isempty(regexp(path_,'[/.]')) %#ok<RGXP1> % file exists but generates an error
175 fprintf('Error running %s:\n', path_);
176 fprintf(2,'%s\n\n',message);
177 end
178end
179
180function cmd = xpdf_command(path_)
181 % Initialize any required system calls before calling ghostscript
182 % TODO: in Unix/Mac, find a way to determine whether to use "export" (bash) or "setenv" (csh/tcsh)
183 shell_cmd = '';
184 if isunix
185 % Avoids an error on Linux with outdated MATLAB lib files
186 % R20XXa/bin/glnxa64/libtiff.so.X
187 % R20XXa/sys/os/glnxa64/libstdc++.so.X
188 shell_cmd = 'export LD_LIBRARY_PATH=""; ';
189 end
190 if ismac
191 shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';
192 end
193 % Construct the command string
194 cmd = sprintf('%s"%s" ', shell_cmd, path_);
195end
function find(in vendor)
Return a list of scalar MATLAB strings containing the paths to all detected mpiexec binaries installe...
function ghostscript(in cmd)
function install(in vendor, in isyes)
Install or provide instructions to install the MPI library from the requested input MPI vendor.
function lib()
Return a scalar MATLAB string containing the path to the lib directory of the ParaMonte library packa...
function check_store_xpdf_path(in path_)
function pdftops(in cmd)
function xpdf_path()
function check_xpdf_path(in path_)
function xpdf_command(in path_)
function user_string(in string_name, in string)
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...