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 | % pdftops.m,v 1.1 2012/05/08 08:00:33 nadolski Exp |
---|
25 | % Copyright: Oliver Woodford, 2009 |
---|
26 | |
---|
27 | % Thanks to Jonas Dorn for the fix for the title of the uigetdir window on |
---|
28 | % Mac OS. |
---|
29 | |
---|
30 | % Call pdftops |
---|
31 | [varargout{1:nargout}] = system(sprintf('"%s" %s', xpdf_path, cmd)); |
---|
32 | return |
---|
33 | |
---|
34 | function path = xpdf_path |
---|
35 | % Return a valid path |
---|
36 | % Start with the currently set path |
---|
37 | path = current_xpdf_path; |
---|
38 | % Check the path works |
---|
39 | if check_xpdf_path(path) |
---|
40 | return |
---|
41 | end |
---|
42 | % Check whether the binary is on the path |
---|
43 | if ispc |
---|
44 | bin = 'pdftops.exe'; |
---|
45 | else |
---|
46 | bin = 'pdftops'; |
---|
47 | end |
---|
48 | if check_store_xpdf_path(bin) |
---|
49 | path = bin; |
---|
50 | return |
---|
51 | end |
---|
52 | % Search the obvious places |
---|
53 | if ispc |
---|
54 | path = 'C:\Program Files\xpdf\pdftops.exe'; |
---|
55 | else |
---|
56 | path = '/usr/local/bin/pdftops'; |
---|
57 | end |
---|
58 | if check_store_xpdf_path(path) |
---|
59 | return |
---|
60 | end |
---|
61 | % Ask the user to enter the path |
---|
62 | while 1 |
---|
63 | if strncmp(computer,'MAC',3) % Is a Mac |
---|
64 | % Give separate warning as the uigetdir dialogue box doesn't have a |
---|
65 | % title |
---|
66 | uiwait(warndlg('Pdftops not found. Please locate the program, or install xpdf-tools from http://users.phg-online.de/tk/MOSXS/.')) |
---|
67 | end |
---|
68 | base = uigetdir('/', 'Pdftops not found. Please locate the program.'); |
---|
69 | if isequal(base, 0) |
---|
70 | % User hit cancel or closed window |
---|
71 | break; |
---|
72 | end |
---|
73 | base = [base filesep]; |
---|
74 | bin_dir = {'', ['bin' filesep], ['lib' filesep]}; |
---|
75 | for a = 1:numel(bin_dir) |
---|
76 | path = [base bin_dir{a} bin]; |
---|
77 | if exist(path, 'file') == 2 |
---|
78 | break; |
---|
79 | end |
---|
80 | end |
---|
81 | if check_store_xpdf_path(path) |
---|
82 | return |
---|
83 | end |
---|
84 | end |
---|
85 | error('pdftops executable not found.'); |
---|
86 | |
---|
87 | function good = check_store_xpdf_path(path) |
---|
88 | % Check the path is valid |
---|
89 | good = check_xpdf_path(path); |
---|
90 | if ~good |
---|
91 | return |
---|
92 | end |
---|
93 | % Update the current default path to the path found |
---|
94 | if change_value(path, 'current_xpdf_path_str', [mfilename('fullpath') '.m']) |
---|
95 | warning('Path to pdftops executable could not be saved. Enter it manually in pdftops.m.'); |
---|
96 | return |
---|
97 | end |
---|
98 | return |
---|
99 | |
---|
100 | function good = check_xpdf_path(path) |
---|
101 | % Check the path is valid |
---|
102 | [good message] = system(sprintf('"%s" -h', path)); |
---|
103 | if ispc |
---|
104 | % system returns good = 1 even when the command runs (on Windows, anyway) |
---|
105 | % Look for something distinct in the help text |
---|
106 | good = ~isempty(strfind(message, 'PostScript')); |
---|
107 | else |
---|
108 | good = good == 1; |
---|
109 | end |
---|
110 | return |
---|
111 | |
---|
112 | function current_xpdf_path_str = current_xpdf_path |
---|
113 | current_xpdf_path_str = 'pdftops'; |
---|
114 | return |
---|