MATLAB: How to print images to pdf with page size larger than screen size (r2016b, r2017a)? (Warning: An error occurred while drawing the scene)

imagepdfprint

I am having trouble printing figures filled with images to pdf when the paper size is >= screen size. The pdf gets generated but the figure size, placement and content are incorrect. During the print, I get this warning:
Warning: An error occurred while drawing the scene
> In defaulterrorcallback (line 12)
In alternatePrintPath>LocalSetPaintDisabled
In alternatePrintPath>printCleanup
In alternatePrintPath>@()printCleanup(pj,pt)
In onCleanup/delete (line 60)
In alternatePrintPath
In print (line 82)
I am seeing this issue with r2016b and r2017a. I was previously using r2015b and never had this problem. Below is a script that I have used to regenerate the issue:(Note that the goal is to have the image fill the pdf page hence the need for explicitly setting the 'paperposition' property)
% Store original units
originalUnits = get(0,'units');
% Set units to inches
set(0,'units','inches');
% Obtain screen size information
screenSizeInches = get(0,'screensize');
% Scale to desired paper size
largePaperSize = screenSizeInches*1.2;
smallPaperSize = screenSizeInches*0.8;
% Get data
data = rand(9);
% Figure with image only
imageHandle = figure;
imagesc(data);
% Figure with lines only
plotHandle = figure;
plot(data);
% Figure with both image and lines
combinedHandle = figure;
subplot(2,1,1)
imagesc(data);
subplot(2,1,2)
plot(data);
% Print to paper larger than screen
% Print image - *Issue here*
set(imageHandle,'paperunits','inches')
set(imageHandle,'papersize',largePaperSize (3:4))
set(imageHandle,'paperposition',largePaperSize );
print(imageHandle, 'testImagePrintLarge.pdf', '-dpdf', '-r0');
% Print lines - No issue
set(plotHandle,'paperunits','inches')
set(plotHandle,'papersize',largePaperSize (3:4))
set(plotHandle,'paperposition',largePaperSize);
print(plotHandle, 'testLinePrintLarge.pdf', '-dpdf', '-r0');
% Print both - No issue
set(combinedHandle,'paperunits','inches')
set(combinedHandle,'papersize',largePaperSize (3:4))
set(combinedHandle,'paperposition',largePaperSize);
print(combinedHandle, 'testCombinedPrintLarge.pdf', '-dpdf', '-r0');
% Print to paper smaller than screen
% Print image - No issue
set(imageHandle,'paperunits','inches')
set(imageHandle,'papersize',smallPaperSize(3:4))
set(imageHandle,'paperposition',smallPaperSize);
print(imageHandle, 'testImagePrintSmall.pdf', '-dpdf', '-r0');
% Restore original units property
set(0, 'units', originalUnits);

Best Answer

You can use 'opengl' renderer option to generate output. For example,
print(imageHandle, 'testImagePrintLarge.pdf', '-dpdf', '-opengl', '-r0');
To get higher resolution output, use '-r150', '-r300' or larger number.
Related Question