How to add a footer in PDF report using only Report API with chapters?

13 views (last 30 days)
How to add a footer in a PDF report only using 'Report' API (and not using Document') with chapter and title page?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Nov 2023
Edited: MathWorks Support Team on 12 Jan 2024
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:
>> mlreportgen.report.Chapter.customizeReporter('@customChapter');
               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 :
......
<layout>
<pfooter type="default" template-name="MyPageFooter"/>
</layout>
:
:
<dptemplate name="MyPageFooter">
<p style="text-align:right;font-family:Arial,Helvetica,sans-serif;font-size:10pt">
<hole id="PageFooterHole1" /> </p>
</dptemplate>......
For more details on how to add footer, run the following command in MATLAB R2018a-
web(fullfile(docroot, 'rptgen/ug/add-page-footers-and-headers.html'))
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:
Please follow the below link to search for the required information regarding the current release:

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!