MATLAB Report Generator - Edit PDF page margins in sections.

10 次查看(过去 30 天)
Hi,
I built a pdf report using mlreportgen.report.Report() function. I want to edit heading format e.g. margin size around the pages. I found examples but the function Document() is used and I'd rather keep Report() function. I also found example using Report() but I can only edit title page and table of content format. I can't modify what's inside a section for instance.
From what I read, I should edit the pdf template to achieve that. But I can't find an easy way to edit *.pdftx files. I unzipped it and played with docpart_templates.html and root.html but I'm really not a html expert. Is there a tool to edit the template easily or is there a way to modify properties of chapter, section, paragraph, etc. using code in my script?
Here's my code. The template 'Default_PDF_Template.pdftx' is a copy of the built-in template "Default PDF Template" I took in Report Explorer. I assume the template can be edited in Report Explorer but I can figure out how.
Thanks in advance!
import mlreportgen.report.*
import mlreportgen.dom.*
% Create report
rpt = Report('MyReport', 'pdf','Default_PDF_Template.pdftx');
open(rpt);
% Add a title page
append(rpt, TitlePage(Title='My Report'));
% Add table of content
toc = TableOfContents;
append(rpt, toc);
% Create all sections
sect_1 = Section(Title='Simulation Results');
sect_1_1 = Section(Title='Signal Description');
sect_2 = Section(Title='Parameters');
% Append sections
append(sect_1, sect_1_1);
append(rpt, sect_1);
append(rpt, sect_2);
% Close and show report
close(rpt);
rptview(rpt);

采纳的回答

Namnendra
Namnendra 2024-5-13
Hi Alexandre,
Editing the `.pdftx` template for a MATLAB Report Generator report to customize headings, margins, and other styling aspects can be complex without a good understanding of HTML and CSS. Unfortunately, there isn't a straightforward graphical tool for editing `.pdftx` templates directly. However, you can still achieve significant customization by editing the template files and using the MATLAB code to apply custom styles. Here's a step-by-step guide to help you modify the properties of chapters, sections, paragraphs, etc., in your script and by editing the `.pdftx` template.
Editing the `.pdftx` Template
1. Extract the `.pdftx` File: You've already figured out that a `.pdftx` file is essentially a zipped folder containing HTML, CSS, and other resources. Extract this folder to a convenient location.
2. Edit HTML/CSS Files: The key files you'll want to edit are the HTML template files and the CSS stylesheets. For headings, margins, and other styles, focus on the CSS files. You can use any text editor or IDE (like Visual Studio Code, Atom, etc.) to edit these files. Look for CSS rules related to margins, padding, and font styles for headings (`h1`, `h2`, `h3`, etc.) and paragraphs (`p`). If you're not familiar with CSS, you might need to look up basic CSS properties related to margins (`margin-top`, `margin-bottom`, `margin-left`, `margin-right`), padding, and font styling.
3. Repackage the `.pdftx` File: After editing, you need to repackage the folder into a `.pdftx` file. Make sure to maintain the original folder structure and file names within the zip. Rename the zip file back to `.pdftx`.
Using MATLAB Code for Custom Styles
For modifying properties of chapters, sections, paragraphs, etc., using code, you can apply styles directly in your MATLAB script. While the `mlreportgen.report.Report` class doesn't provide as straightforward a method for styling as the `mlreportgen.dom.Document` class does, you can still use the `mlreportgen.dom` package to create custom styles and apply them to your sections and paragraphs.
import mlreportgen.report.*
import mlreportgen.dom.*
% Create and open report
rpt = Report('MyReport', 'pdf', 'Default_PDF_Template.pdftx');
open(rpt);
% Define custom styles
custHeadingStyle = {FontFamily('Arial'), FontSize('14pt'), Bold(true), Color('red')};
custParaStyle = {FontFamily('Helvetica'), FontSize('11pt'), Color('blue')};
% Add a title page
append(rpt, TitlePage(Title='My Report'));
% Add table of content
toc = TableOfContents;
append(rpt, toc);
% Create and style sections
sect_1 = Section(Title='Simulation Results');
sect_1.Style = [sect_1.Style, custHeadingStyle]; % Apply custom heading style
sect_1_1 = Section(Title='Signal Description');
sect_1_1.Style = [sect_1_1.Style, custHeadingStyle]; % Apply custom heading style
% Create a paragraph with custom style
para = Paragraph('This is a custom styled paragraph.');
para.Style = [para.Style, custParaStyle]; % Apply custom paragraph style
% Append sections and paragraph
append(sect_1, sect_1_1);
append(sect_1, para); % Add paragraph to section
append(rpt, sect_1);
% Close and view report
close(rpt);
rptview(rpt);
Note
- While you can directly apply some styles using the `mlreportgen.dom` package in your MATLAB code, not all aspects of the PDF layout and styling can be controlled this way, especially for more complex changes like global margins which are better managed through the `.pdftx` template.
- If you're unfamiliar with HTML and CSS, consider spending some time with basic tutorials online; understanding these technologies will greatly enhance your ability to customize report templates.
- Remember to test your report after each significant change to ensure that your modifications produce the desired effect.
I hope the above information answers your query.
Thank you.
  1 个评论
Alexandre
Alexandre 2024-5-13
Thanks for the clear explanation. There was a slight error in the code for custom style. A section does not have Style properties. I need to create a Text.
import mlreportgen.report.*
import mlreportgen.dom.*
% Create and open report
rpt = Report('MyReport', 'pdf');
open(rpt);
% Define custom styles
custHeadingStyle = {FontFamily('Arial'), FontSize('14pt'), Bold(true), Color('red')};
custParaStyle = {FontFamily('Helvetica'), FontSize('11pt'), Color('blue')};
% Add a title page
append(rpt, TitlePage(Title='My Report'));
% Add table of content
toc = TableOfContents;
append(rpt, toc);
% Create and style sections
sect_1 = Section;
sect_1.Title = Text('Simulation Results');
sect_1.Title.Style = custHeadingStyle; % Apply custom heading style
sect_1_1 = Section;
sect_1_1.Title = Text('Signal Description');
sect_1_1.Title.Style = custHeadingStyle; % Apply custom heading style
% Create a paragraph with custom style
para = Paragraph('This is a custom styled paragraph.');
para.Style = [para.Style, custParaStyle]; % Apply custom paragraph style
% Append sections and paragraph
append(sect_1, sect_1_1);
append(sect_1, para); % Add paragraph to section
append(rpt, sect_1);
% Close and view report
close(rpt);
rptview(rpt);

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by