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')));
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.
The report generation in MATLAB allows you to create a custom template for reporters using 'customizeReporter' method for reporter classes in mlreportgen.report.*. You can create a custom chapter template first and add holes to create a footer in the PDF template. For reference, template holes are places in a template that the report program fills with content.
The following steps illustrate how to add a footer in the report:
Step 1 – Create a custom chapter class with the following command:
This will create a folder named '@customChapter' in your current directory. It contains the file 'customChapter.m' and a resource directory.
Step 2 – Navigate to the PDF template - @customChapter/resources/templates/pdf, unzip the PDF template using the command:
>> unzip("default.pdftx","default")
This will create a folder named “default” with "root.html" and "docpart_templates.html"
Step 3 – Edit the PDF template to add a footer containing a hole. For example, you can update the "docpart_templates.html" with following code in the <body> section :
Step 4 – Zip the template (remember to be on the path @customChapter/resources/templates/pdf:
>> zipTemplate("default.pdftx","default")
Step 5 – Edit 'customChapter.m' and add a property that matches the hole ID in the template. Each hole in the report will be replaced by the value of the corresponding property of the report. In this case, create property name 'PageFooterHole1' for 'customChapter' class.
Step 6 – Create a chapter with custom chapter template. Set the content for the footer hole by setting the corresponding property of the report object. In this example, following code would create report and add a chapter with desired footer content:
>> rpt = Report('test','pdf'); %create a report object
>> ch = customChapter;
>> tp = TitlePage; %create a title page
>> tp.Title = 'Test Case Results';
>> tp.Subtitle = 'Subtitle';
>> tp.Author = 'John';
>> add(rpt,tp);
>> ch.PageFooterHole1 = mlreportgen.dom.Text("This is my footer1");
>> add(rpt,ch);
>> add(rpt, "body content");
>> add(rpt, mlreportgen.dom.PageBreak);
>> add(rpt, "body content");
>> close(rpt);
>> rptview(rpt); % To view the report
This will create a PDF report with a title page and a chapter with two pages showing the footer with the style specified in "docpart_templates.html".
Files are attached for reference.
Please note that you can adapt this method to add headers as well or any other customization needed by your workflow. For instance, the following MATLAB Answers post discusses an approach to add an image to a header:
When creating a page footer, you can first specify which pages you would like it to appear on (first page only, even pages, odd pages, or all pages). Then, you can append different DOM objects to the footer, such as a page number or a paragraph.
The following code is an adaptation of the example linked above. This code demonstrates how to add a paragraph with custom text to a footer which already includes the page number:
Best Answer