MATLAB: How to add a custom footer to the MATLAB Report Generator report

footergeneratorMATLABreport

I would like to create a custom PDF report which contains metadata in the footer that is related to user inputs.
Is there a way to do this using MATLAB Report Generator?

Best Answer

Unfortunately, you cannot create reports with a custom footer using the Report Explorer.
However, it is possible to do this within the Report Generator API. The MATLAB function below generates an example report with a footer corresponding to a given test and run number. Note that the inputs 'r' and 't' are simply metadata that need to be logged, and 'h' is an array of figure handles. Also, note that you must generate and save the 'default' PDFXTX file from the MATLAB report generator for this function to run.
function example_report(t,r,figObj)
import mlreportgen.dom.*
d = Document('testReport','pdf','default');
left = Paragraph(' ');
left.WhiteSpace = 'preserve';
center = Paragraph();
append(center, Page);
center.HAlign = 'center';
right = Paragraph(sprintf('Test/Run: %i/%i', t, r));
right.HAlign = 'right';
footer(1) = PDFPageFooter('default');
append(footer, HorizontalRule);
ft = Table({left, center, right});
ft.Width = '6.5in';
ft.entry(1,1).Style = {Width('2.17in')};
ft.entry(1,2).Style = {Width('2.17in')};
ft.entry(1,3).Style = {Width('2.17in')};
append(footer, ft);
plo = PDFPageLayout;
plo.FirstPageNumber = 1;
plo.PageFooters = footer;
append(d, plo);
append(d, Heading1(sprintf('Test %i, Run %i', t,r')));
if ~isdir('tempimages')
mkdir tempimages
end
for i = 1:length(figObj)
set(figObj(i),'PaperPositionMode','Auto');
fname = fullfile('tempimages',sprintf('image%i',i));
print(figObj(i),fname,'-djpeg');
h = Heading2(sprintf('Figure %i',i));
h.Style = {KeepWithNext, OuterMargin('0','0','12pt','6pt')};
append(d,h);
image = Image([fname '.jpg']);
image.Width = '4in';
image.Height = '3in';
append(d,image);
end
close(d);
The method by which this function works is outlined below:
1. Import all functions from the package 'mlreportgen.dom'.
2. Open a PDF document for writing with the "Document" function.
3. Create the objects 'left', 'center', and 'right' that correspond to the text that will appear on each part of the footer. Specifically, 'left' is a 'Paragraph' object containing a single space (with is not visible), 'center' is a 'Paragraph' object containing the page number, and 'right' is a 'Paragraph' object containing the test and run number information.
4. Create a 'PDFPageFooter' object from the default template. Append both a 'HorizontalRule' object, and a 'Table' object which has the 'left', 'center', and 'right' objects as entries.
5. Create a 'PDFPageLayout' object, and append the footer to this object.
6. If it does not yet exist, create a directory for temporary image files.
7. Loop through the handle containing your figures. Save them to the 'tempimages' directory. Finally, create 'Image' objects and append these to the report. 
8. Close the document 'd'.