Print PDF to a Specific size?

159 views (last 30 days)
Jorge Rodriguez
Jorge Rodriguez on 10 Jul 2019
Answered: Sergi Tarroc on 17 Jun 2021
Hello,
I'm trying to print a plot in PDF to a size of 8.4 cm by 5 cm. However, the result is always a 8x11 inch figure (US default). Here is an sample of the ploting code i'm using:
Plot(x,y);
set(gcf,'PaperUnits','centimeters','PaperSize',[8.4 5])
fig = gcf;fig.PaperUnits = 'centimeters';
fig.PaperPosition = [0 0 8.4 5];fig.Units = 'centimeters';
fig.PaperSize=[8.4 5];fig.Units = 'centimeters';
print(fig,'fig_name','-dpdf','-r200')
It seem as it ignores the change in units and size.
Thanks for your help

Answers (2)

Sergi Tarroc
Sergi Tarroc on 17 Jun 2021
Hello,
I have been having lots of issues arround printing figures directly in pdf conserving the aspect ratio, the vectorized printing of it and having fitted margins. @Hari Krishna Ravuri comment help me a lot to solve the issue, and I found a easier way for doing it that maight be helpfull. The key thing is properly modifying the figure class properties (https://es.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html)
The approach is basicaly measure the figure size, and set the pdf printing size to the same value. The tricky part about it is that the default units of the figure size Position(3:4) are in pixels, while the pdf page size PaperSize is in 'inches' (if you are in United States) or 'centimeters' (if you are anywhere else). So for doing this adjustment properly, it is requierd to match the units first.
I write you down here an example. Notice that I modify the properties with a 'struct' aproach, but it is not the only way: you can use instead the funtions get() and set():
f = figure(); % generate a figure
plot(0:10,0:2:20); % perform any plot (in this case y=2*x)
f.Units = 'centimeters'; % set figure units to cm
f.PaperUnits = 'centimeters'; % set pdf printing paper units to cm
f.PaperSize = f.Position(3:4); % assign to the pdf printing paper the size of the figure
print -dpdf fileName; % print the figure
If you want, you can also add some margin into your print simply by making the paper sigltly bigger. Notice that the figure by default is centered in the middle of the paper, and the size change is symmetrical in all directions
f.PaperSize = f.Position(3:4)+0.2; % Add 0.1 cm of margin in each direction
I hope the approach is usefull

Hari Krishna Ravuri
Hari Krishna Ravuri on 17 Jul 2019
Hi,
I understand that you are trying to print your Figure to a PDF File having Paper size of 8.4 cm by 5cm, but you are getting 8X11 inch page sized document.
MATLAB is setting the paper size of the document to 8.4cm by 5cm.The reason why you are seeing the paper size as 8X11 inch is because of your print settings. Choosing the paper size to print by the PDF page size may solve your problem. I executed your script with
x=[1 2 3]
and
y = [1 4 9]
as below.
plot([1 2 3],[1 4 9]);
set(gcf,'PaperUnits','centimeters');
set(gcf,'PaperSize',[8.4 5]);
fig = gcf;
fig.PaperUnits = 'centimeters';
fig.PaperPosition = [0 0 8.4 5];
fig.Units = 'centimeters';
fig.PaperSize=[8.4 5];
fig.Units = 'centimeters';
print(fig,'myFigPDF','-dpdf','-r200');
Assuming that, you are using Adobe Acrobat Reader for printing the PDF on a Windows machine, I have attached a screenshot of the print settings that may help you.

Community Treasure Hunt

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

Start Hunting!