Add image in the header of the report

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!

Antworten (1)

Mary Abbott
Mary Abbott am 2 Jul. 2019

1 Stimme

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 Kommentare

Jack Arnoldi
Jack Arnoldi am 2 Jul. 2019
Hello Mary, thank you for your answer!
It works but only for one page. I guess it then gets overwritten by the automatic header constructor of the chapters. Do you know if there is a way to just add the picture with the autmatic generated chapter header?
Yes, chapters will add new page layouts when added to a report. In this case, it's easiest to modify the chapter's template to insert the picture. You can either create and modify a copy of the chapter template:
mlreportgen.report.Chapter.createTemplate('myChapterTemplate', 'pdf');
unzipTemplate('myChapterTemplate.pdftx', 'myChapterTemplate_pdftx');
% (modify template files in the directory myChapterTemplate_pdftx)
zipTemplate('myChapterTemplate.pdftx', 'myChapterTemplate_pdftx');
ch = mlreportgen.report.Chapter('Example Chapter');
ch.TemplateSrc = 'myChapterTemplate.pdftx';
or, if you would like to further customize the the chapter reporter, create a custom class that is a subclass of the Chapter class:
mlreportgen.report.Chapter.customizeReporter('@myChapter');
% (naviagate to myChapter/resources/templates/pdf, unzip template, modify, zip template)
ch = myChapter('Example Chapter');
In the unzipped template directory, put the image file in the images folder and modify docpart_templates.html as described in the link I gave in the original answer. To insert an image, add an img element to the dptemplate element for the header. By default it is named SectionDefaultPageHeader:
<img height=".75in" width=".75in" src="./images/peppers.png"/>
Varun Vyas
Varun Vyas am 9 Jan. 2020
Hi Mary,
I found this post helpful and I am trying to implement something similar in my report. I followed all the steps that you mentioned in your comments and updated the dptemplate element for the header with this command <img height=".75in" width=".75in" src="./images/Logo.png"/>., as my png filename is Logo.png.
It should have run fine but instead it is giving out an error that it's not able to delete the _FO directory when it tries to close the document. When I try to delete the _FO directory manually after the run has failed, it complains that it's being used by another program. Then I go into the _FO directory and under images and try to delete Logo.png file and it complains that it cannot delete this file since it's open in MATLAB 2018b.
I don't understand what I could be doing wrong here and how to resolve this issue. Can you give me any suggestions to resolve this?
Thanks,
Varun
Mary Abbott
Mary Abbott am 10 Jan. 2020
Hi Varun,
This error usually happens when there is an image with zero width and height. Can you make sure the height and width are set correctly for all references to Logo.png? Are you able to open the Logo.png file in the images folder of the template (to confirm there were no problems when copying the image to the folder)?
This error happens after the report is generated, so you should still be able to open the generated PDF. Is the image correct in the generated report?
Varun Vyas
Varun Vyas am 10 Jan. 2020
Hi Mary,
Thanks for your reply. Here are my comments on your questions.
  1. I looked at the size of Logo.png in my docpart_templates.html file that I edited, and the size is 0.75 inches as you had in the example above.
  2. I am able to open the Logo.png file in the images folder of the template.
  3. And yes it is generating a report successfully, but it does not have the image in the chapter header when I open the report.
I am also attaching a sample script. It is one of the examples by mathworks to generate a report and I am using only a part of it. I have also attached the chapter template that I am using to attach Logo.png in my report.
Varun
Thank you, I was able to reproduce the issue with your reproduction script.
The error happens when an image is referenced in a PDF template but is not actually printed in the report. Usually this is because the width and height are 0. In this case, it was because the image was referenced in the header for odd pages of the chapter, but the chapter was not long enough to have an odd-numbered page (there is a separate header for the title page, so page 1 does not count as odd). I've marked this as a bug so that we can look into it. A workaround is, if an image is defined in a header, make sure that header is used in the report.
The Chapter template by default defines three different header types: first (first page of the chapter), even (even pages of the chapter), and default. The default type header is used for only odd pages in this case. However, it is used for even pages if no "even" type header is defined, and used for the first page if no "first" type header is defined. If you want the same header for all pages in a chapter, delete the "first" and "even" type headers defined for the "Section1" document part in "docpart_templates.html". For example, the following uses only one header and one footer for all pages of the chapter:
<dptemplate name="Section1">
<layout style="page-margin: 0.5in 1in 0.5in 1in 0.5in 0.5in 0in; page-size: 8.5in 11in portrait">
<pheader type="default" template-name="SectionDefaultPageHeader"/>
<pfooter type="default" template-name="SectionDefaultPageFooter"/>
<pnumber format="1" />
</layout>
<hole id="Title" default-style-name="SectionTitle1">TITLE</hole>
<hole id="Content" default-style-name="SectionContent">CONTENT</hole>
</dptemplate>
Varun Vyas
Varun Vyas am 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
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 am 22 Mai 2020
dear Marry, may you please take a look at my question here ? I got unresolvable problem with compiling my app with report generator

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB Report Generator finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018a

Gefragt:

am 2 Jul. 2019

Kommentiert:

am 22 Mai 2020

Community Treasure Hunt

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

Start Hunting!

Translated by