Print PDF to a Specific size?

조회 수: 139 (최근 30일)
Jorge Rodriguez
Jorge Rodriguez 2019년 7월 10일
답변: Sergi Tarroc 2021년 6월 17일
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

답변 (2개)

Sergi Tarroc
Sergi Tarroc 2021년 6월 17일
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 2019년 7월 17일
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.

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by