Clear Filters
Clear Filters

Use custom layout for pdf within Report Generator preferably without template.pdftx?

4 views (last 30 days)
I am just beginning to explore the possibilities with the report generator - so beginner questions ahead.
My goal is to create a report in pdf format using my corporate identity layout. Basically, I want to modify font type, table formating and footer. Therefore either I have to modify the template or make the changes programmatically. I do prefer the latter. For now I followed the example given from
but I changed a few lines:
rpt = Report('OutputPath','c:\reporttest\output.pdf','Type','pdf');
rpt.Layout.PageSize = PageSize("297mm","210mm","portrait");
pageMarginsObj = PageMargins();
pageMarginsObj.Top = "15mm";
pageMarginsObj.Bottom = "15mm";
pageMarginsObj.Left = "20mm";
pageMarginsObj.Right = "20mm";
pageMarginsObj.Footer = "10mm";
pageMarginsObj.Gutter = "0in";
rpt.Layout.PageMargins = pageMarginsObj;
To get a better understanding of the report generator, I used unzipTemplate('mytemplate.pdftx') and zipTemplate('mytemplate.pdftx') to change the/a template file. I've changed all occurrences of any font names to Comic Sans to spot a difference. First question would be; how to add the new template to my line above where I start the Report ? I don't know which is the correct name pair for a template. Both TemplatePath and TemplateName which I found somewhere in the documenation seem not to work
With a second try I switched back to rpt = Report('myyyreport','pdf','myytemplate.pdftx'). But I did not notice a change in the font of a paragraph or a headline. Only the font of the Table of Contents did change. Also, the formating of a table did change - which was not on purpose. I assume, there is another source of formating - even when a mytemplate.pdftx is specified?
To circumvent these problems I'd like to make all modifications within a single .m file. So final question: does anyone knows an up to date source to add a picture to each footer and change the standard font? I followed the code in https://de.mathworks.com/matlabcentral/answers/469827-add-image-in-the-header-of-the-report but the code just adds a picture on one page.

Answers (1)

Namnendra
Namnendra on 23 Jul 2024 at 11:47
Hi Simon,
Creating a customized PDF report in MATLAB using the Report Generator can be a bit challenging for beginners, but it's a powerful tool once you get the hang of it. Below, I'll guide you through setting up a report with custom fonts, table formatting, and footers programmatically, without modifying the template file directly.
Step-by-Step Guide
1. Setting Up the Report
First, let's set up the report with custom page size and margins, as you have done:
import mlreportgen.report.*
import mlreportgen.dom.*
% Create a report
rpt = Report('OutputPath', 'c:\reporttest\output.pdf', 'pdf');
% Set page size and margins
rpt.Layout.PageSize = PageSize("297mm", "210mm", "portrait");
pageMarginsObj = PageMargins();
pageMarginsObj.Top = "15mm";
pageMarginsObj.Bottom = "15mm";
pageMarginsObj.Left = "20mm";
pageMarginsObj.Right = "20mm";
pageMarginsObj.Footer = "10mm";
pageMarginsObj.Gutter = "0in";
rpt.Layout.PageMargins = pageMarginsObj;
2. Customizing the Font
To customize the font for different elements, you can define styles and apply them programmatically:
% Define a custom font style
fontName = 'Comic Sans MS';
fontSize = '12pt';
% Create a paragraph with custom font
para = Paragraph('This is a custom font paragraph.');
para.Style = {FontFamily(fontName), FontSize(fontSize)};
% Add the paragraph to the report
add(rpt, para);
3. Customizing Table Formatting
To customize table formatting, you can define table styles and apply them:
% Create a table
table = Table(magic(5));
% Define table style
table.Style = {FontFamily(fontName), FontSize(fontSize)};
% Add the table to the report
add(rpt, table);
4. Finalizing and Generating the Report
Finally, you can close and generate the report:
% Close the report
close(rpt);
% View the report
rptview(rpt);
Above steps are very generic and may need to be changed according to specific user needs.
I hope the above information helps you.
Thank you.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!