How to print a figure as a PDF, that has the same size of my figure (or, how to resize the PDF according to my figure dimensions)?

7 views (last 30 days)
How to print a figure as a PDF, that has the same size of my figure (or, how to resize the PDF according to my figure dimensions)?
In other words, how can I get rid of the upper and lower white margins in the following printed PDF?
% create a figure with "subtightplot"
subplot = @(m,n,p) subtightplot (m, n, p, [0.001 0], [0.01 0.01], [0.01 0.01]);
fig = figure('position',[700 100 700 1200],'Color',[1 1 1])
hold on
for i = 1 : 6
subplot(2,3,i);
end
hold off
% print the figure as PDF
fig.Units = 'centimeters'; % set figure units to cm
fig.PaperUnits = 'centimeters'; % set pdf printing paper units to cm
fig.PaperSize = fig.Position(3:4); % assign to the pdf printing paper the size of the figure
path = ( '/Users/Donald/.../' ); % where to print my figure
print (fig(:), '-dpdf','-loose','-opengl','-r600', [path, 'fig'] )
This is the resulting PDF, where you can clearly see the upper and lower white margins that I would like to get rid of:
  3 Comments
Sim
Sim on 7 Mar 2023
Edited: Sim on 7 Mar 2023
Thanks a lot! Yes, that solution works! Sorry for asking something similar to my old post - Maybe I can delete this question (if Matlab moderators/staff thinks it is better)

Sign in to comment.

Accepted Answer

Sim
Sim on 7 Mar 2023
Edited: Sim on 7 Mar 2023
Solved, thanks to the @Luca Ferro's comment (Yes, similar to my old post - and sorry for asking something similar, I did not realise I could use the same solution, my fault)...Many thanks @Luca Ferro!!
Just the exportgraphics function solves my issue:
% Solution:
% create a figure with "subtightplot"
subplot = @(m,n,p) subtightplot (m, n, p, [0.001 0], [0.01 0.01], [0.01 0.01]);
fig = figure('position',[700 100 700 1200],'Color',[1 1 1])
hold on
for i = 1 : 6
subplot(2,3,i);
end
hold off
% print the figure as PDF
path = ( '/Users/Donald/.../' ); % where to print my figure
exportgraphics(fig, fullfile(path, 'fig.pdf'),'ContentType','vector') % this solves my issue
And this is the resulting PDF, without the upper and lower white margins:
At this point, I do not know if the following commands that I have used (by following the method/solution proposed by Sergi Tarroc on 17 Jun 2021) are really necessary, maybe no, or at least I do not see differences:
fig.Units = 'centimeters'; % set figure units to cm
fig.PaperUnits = 'centimeters'; % set pdf printing paper units to cm
fig.PaperSize = fig.Position(3:4); % assign to the pdf printing paper the size of the figure

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!