How to force output of large figure to not be "scaled", or how to properly print a pdf whose dimensions won't fit on the [small] screen

Hello,
I am trying to create a figure that will eventually be "print"ed to pdf at a standard paper size. However, if I tried to create this figure when my screen resolution + screen dpi is insufficient to hold the full figure, I receive the following error upon invoking "print":
Warning: UI controls may not print or export properly because the output has been scaled. Setting the figure's
PaperPositionMode property to 'auto' may help.
And indeed the uicontrols do not output correctly on the resulting pdf. Following the advice and setting PaperPositionMode to 'auto' does not fix the problem, although it does change the way in which the output is incorrect.
It seems I would want to prevent matlab from automatically "scaling the output", but I cannot find how to do this. I understand that what I want will prevent me from viewing the full figure correctly on my screen, but I don't care because I ultimately want the printed pdf to be correct.
Alternatively, maybe I am misusing the "PaperPosition" and "PaperSize" properties of the figure?
Here is a minimal example to reproduce the warning, though it does not give an appreciation of what goes wrong with the ui control placement on the pdf (in my application I carefully place many uicontrols). In this example, I want a 20"x20" pdf, but it cannot be held in full on my screen, which is a 1920x1080 display with reported ScreenPixelsPerInch=96 to give 20"x11.25". In my real application, I want a 8.5"x11" (portrait), and I want it to work even if the screen is small (e.g., laptop with 720 pixels tall and typical screen dpi).
PaperSize = [21,21]; % in inches, too large to fit on screen;
hFig = figure('Units','inches',...
'Position',[0,0,PaperSize],...
'PaperPosition',[0,0,PaperSize],...
'PaperSize',PaperSize,...
'PaperPositionMode','manual');
% need a uicontrol to trigger warning
uicontrol(hFig,'Style','pushbutton','String','uicontrol element')
print('tmp.pdf','-dpdf','-r600');
Thanks in advance for any help.
Matlab version is 9.1.0.441655 (R2016b)

 Accepted Answer

I got a work-around.
The issue seems to be that figures are automagically resized when they don't fit on the screen, and this messes up any kind of placement of uicontrols, axes, etc. on the figure.
The workaround is to determine a safe length scaling factor based on screen resolution reported by
ScreenSize = get(0,'ScreenSize');
SPPI = get(0,'ScreenPixelsPerInch');
SH = floor((ScreenSize(4)-200)/SPPI);
SMult = SH/(PaperSize(2));
And scale all lengths and positions of uicontrols in the figure by this amount.
The next step is to rely on the behavior of
print -fillpage
which asserts a 0.25" margin but otherwise fills the page, and so define our figure on screen as
printMargin = 0.25; % immovable 0.25" margin with print -fillpage
FigureSize = (PaperSize-2*printMargin) * SMult;
This seems to work reasonably well...so far...

More Answers (1)

I haven't exported to PDF, but I used to have similar problems when exporting figures to emf. I have a short script that fixes it for me 99% of the time. Maybe it'll fix your PDF export.
Here's the contents:
unis = get(gcf,'units');
ppos = get(gcf,'paperposition');
set(gcf,'units',get(gcf,'paperunits'));
pos = get(gcf,'position');
ppos(3:4) = pos(3:4);
% pos(1:2) = [1 1];
set(gcf,'paperposition',ppos);
set(gcf,'units',unis);

7 Comments

Thanks Marc for the reply! Unfortunately, this sequence of commands didn't resolve my problem. My PaperUnits was already the same as Units, and the values of the Position was already identical to PaperPosition, but I tried this exact sequence anyway just in case.
Hm, shame it didn't help.
Have you tried using the FileExchange submission export_fig?
print() can be buggy sometimes.
Thanks for the suggestion. Interestingly, export_fig() DOES output correctly if I use the png driver, whereas print() itself did NOT [edit: only when not specifying output resolution...specifying output resolution broke it again]
However, export_fig() also relies on ghostscript, which I do not want to force on users (I want to deploy the app with compiler).
Yair states that export_fig() is only a fancy wrapper around print(), so there must be some magic in there independent of the ghostscript route to make eps/pdf output...
Getting closer...
Hm, I don't know the function well enough, but I would take a closer look at his subfunctions print2eps and eps2pdf. You might be able to extract what you need from there.
What is the 'PaperType' property of your figure? Setting that according to the paper type of the PDF document may help (e. g. 'A4' or 'A3')
Thanks for the continued help. I had been using "US Letter" for PaperType, and ensured that my PaperSize and PaperPosition correspond to the correct dimensions (8.5x11 inches).
I ended up not figuring out export_fig (though it seemed promising for a while).
However, I settled on a workaround as I describe below. I'd be happy to see if there are cleaner ways to do it!
Thanks!
While this is 10 years old it just fixed a problem for me where saving a page to PDF was 1 in 10 times a different size then expected. I have been playing with pauses and different methods for months to no avail. This wizardry worked on a test case of 100 prints back to back. Without it I would get 1/4 sized pages and pages with crazy big fonts about 30% of the time with it 100 for 100 worked as expected! Many thanks to you!

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Asked:

on 24 Oct 2016

Commented:

on 5 Oct 2026

Community Treasure Hunt

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

Start Hunting!