function save_pdf(h,filename)
%SAVE_PDF 
%   save_pdf(h,filename)
%   Save figure as pdf file
%
%   Inputs:
%    h = figure handle
%    filename = pdf file name

%% get nice margins
h.Units = 'centimeters'; %set to cm
pos = get(h,'Position'); % get current window size
height = pos(4); width = pos(3);

% ax = gca;  % current axes
% ax.OuterPosition=[0 0 1 1]; % fill up window
% PBAR=ax.PlotBoxAspectRatio; % get aspect ratio
% height=width*PBAR(2)/PBAR(1); % set height to be correct
% h.Position = [2,2, width, height]; % resize

%% now print
set(h, ...
    'PaperSize',[width height],...
    'PaperUnits','centimeters',...
    'PaperPositionMode','auto');  % set paper size

saveas(h,filename, 'pdf')
end
