MATLAB Answers

Add image in the header of the report

35 views (last 30 days)
Jack Arnoldi
Jack Arnoldi on 2 Jul 2019
Commented: farzad on 22 May 2020
I am trying to put a simple image in the header of my report. I am not using the report generator app but just coding.
I don't understand how to construct a header in this report generator. Do I always have to go through a template?
Can someone give me links to a tutorial or documentation that will give me hints on how to construct a header?
Thank you in advance!

  0 Comments

Sign in to comment.

Answers (1)

Mary Abbott
Mary Abbott on 2 Jul 2019
Hello,
Templates are not required for constructing a header. To create a header in a report programmatically, you can add a header object to the report's page layout. The following is an example for PDF reports. If you are creating a Word report instead, change the report type to 'docx' and create a DOCXPageHeader instead of a PDFPageHeader:
import mlreportgen.dom.*
import mlreportgen.report.*
rpt = Report('exampleReport', 'pdf') % Create an mlreportgen.report.Report object
% Create a sample image
im = Image(which('peppers.png'));
im.Width = '0.75in';
im.Height = '0.75in';
% Construct the header
header = PDFPageHeader();
append(header, im);
% Get the report's page layout and set header
layout = getReportLayout(rpt);
layout.PageHeaders = header;
% (optional) Adjust the space for the header and top page margin
layout.PageMargins.Header = '1in';
layout.PageMargins.Top = '0.5in';
% Add sample content
p = Paragraph('sample text');
add(rpt, p);
% Close and view report
close(rpt);
rptview(rpt)
If you would like to use a template, or if you are using a DOM API Document object instead of the newer Report API Report object, please see the following link:

  9 Comments

Show 6 older comments
Varun Vyas
Varun Vyas on 13 Jan 2020
Mary,
Thanks for your answer. This works out now, but now I am seeing two chapter titles, one from the section title and one from the header title. And removing any one of them is not helping. If I remove the header title, then it removes the header margin completely and if I remove the section title, then I don't see any chapter title (section or header).
Also, when I try to use the align command on img command line, it does not accomplish the align command.
Thanks,
Varun
Mary Abbott
Mary Abbott on 28 Jan 2020
Hi Varun,
You can use the text-align style in PDF templates to align the img element. For example:
<img height=".75in" width=".75in" style="text-align:center;" src="./images/peppers.png"/>
You can also create a style in the template's style sheet. More information about the available styles for PDF templates is here:
I'm not sure I understand the issue you describe with the two chapter titles. I suggest reaching out to technical support, as they will be able to look into the issue more.
farzad
farzad on 22 May 2020
dear Marry, may you please take a look at my question here ? I got unresolvable problem with compiling my app with report generator

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by