forked from hasemar/SMU_catcherBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save2pdf.m
67 lines (57 loc) · 2.07 KB
/
save2pdf.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
%SAVE2PDF Saves a figure as a properly cropped pdf
%
% save2pdf(pdfFileName,handle,dpi)
%
% - pdfFileName: Destination to write the pdf to.
% - handle: (optional) Handle of the figure to write to a pdf. If
% omitted, the current figure is used. Note that handles
% are typically the figure number.
% - dpi: (optional) Integer value of dots per inch (DPI). Sets
% resolution of output pdf. Note that 150 dpi is the Matlab
% default and this function's default, but 600 dpi is typical for
% production-quality.
%
% Saves figure as a pdf with margins cropped to match the figure size.
% (c) Gabe Hoffmann, [email protected]
% Written 8/30/2007
% Revised 9/22/2007
% Revised 1/14/2007
function save2pdf(pdfFileName,handle,dpi)
% Verify correct number of arguments
narginchk(0,3);
% If no handle is provided, use the current figure as default
if nargin<1
[fileName,pathName] = uiputfile('*.pdf','Save to PDF file:');
if fileName == 0; return; end
pdfFileName = [pathName,fileName];
end
if nargin<2
handle = gcf;
end
if nargin<3
dpi = 150;
end
% Backup previous settings
prePaperType = get(handle,'PaperType');
prePaperUnits = get(handle,'PaperUnits');
preUnits = get(handle,'Units');
prePaperPosition = get(handle,'PaperPosition');
prePaperSize = get(handle,'PaperSize');
% Make changing paper type possible
set(handle,'PaperType','<custom>');
% Set units to all be the same
set(handle,'PaperUnits','inches');
set(handle,'Units','inches');
% Set the page size and position to match the figure's dimensions
paperPosition = get(handle,'PaperPosition');
position = get(handle,'Position');
set(handle,'PaperPosition',[0,0,position(3:4)]);
set(handle,'PaperSize',position(3:4));
% Save the pdf (this is the same method used by "saveas")
print(handle,'-dpdf',pdfFileName,sprintf('-r%d',dpi))
% Restore the previous settings
set(handle,'PaperType',prePaperType);
set(handle,'PaperUnits',prePaperUnits);
set(handle,'Units',preUnits);
set(handle,'PaperPosition',prePaperPosition);
set(handle,'PaperSize',prePaperSize);