Main Content

Customize the Page Size and Margins of a PDF Report

This example shows how to use Report API reporter templates to customize the page size and the header, footer, and gutter margins of a PDF report.

By default, the Report API generates 8.5-by-11-inch portrait pages with one-inch margins. Headers, footers, and gutters are each one-half-inch wide. You may need to change these dimensions in your report. For example, if you are generating a report for a European locale, you may need to generate A4 pages. This example shows how to generate A4 pages. Use the same procedure to generate other page dimensions.

To change the page dimensions of a Report API report, you must change the dimensions specified by the templates of the mlreportgen.report.TitlePage, mlreportgen.report.TableOfContents, and mlreportgen.report.Chapter reporters. This example shows how to modify and use the PDF template for each reporter.

Customize the Title Page PDF Template

Create a copy of the mlreportgen.report.TitlePage reporter default PDF template.

mlreportgen.report.TitlePage.createTemplate("titlePageTemplate.pdftx", "pdf");

Unzip the template.

unzipTemplate("titlePageTemplate.pdftx"); 

Open the titlePageTemplate unzipped folder.

In an HTML or text editor, open the docpart_templates.html file.

In the layout element, use the style attribute to modify the page size and margins to the A4 values:

  • Top, bottom,left,right margins of 0.98 inches.

  • Header and footer heights of 0.5 inches.

  • Gutter size (space for binding pages) of 0.

  • 8.27-inch by 11.69-inch page size in portrait orientation.

Save the docpart_templates.html file.

Zip the template.

zipTemplate("titlePageTemplate.pdftx");

Customize the Table of Contents PDF Template

Create a copy of the mlreportgen.report.TableOfContents reporter default PDF template.

mlreportgen.report.TableOfContents.createTemplate("tableOfContentsTemplate.pdftx", "pdf");

Unzip the template.

unzipTemplate("tableOfContentsTemplate.pdftx"); 

Open the tableOfContentsTemplate unzipped folder.

In an HTML or text editor, open the docpart_templates.html file.

In the layout element, use the style attribute to modify the page size and margins to the A4 values:

  • Top, bottom,left,right margins of 0.98 inches.

  • Header and footer heights of 0.5 inches.

  • Gutter size (space for binding pages) of 0.

  • 8.27-inch by 11.69-inch page size in portrait orientation.

Save the docpart_templates.html file.

Zip the template.

zipTemplate("tableOfContentsTemplate.pdftx");

Customize the Chapter PDF Template

Create a copy of the mlreportgen.report.Chapter reporter default PDF template.

mlreportgen.report.Chapter.createTemplate("chapterTemplate.pdftx", "pdf");

Unzip the template.

unzipTemplate("chapterTemplate.pdftx"); 

Open the chapterTemplate unzipped folder.

In an HTML or text editor, open the docpart_templates.html file.

In the layout element, use the style attribute to modify the page size and margins to the A4 values:

  • Top, bottom,left,right margins of 0.98 inches.

  • Header and footer heights of 0.5 inches.

  • Gutter size (space for binding pages) of 0.

  • 8.27-inch by 11.69-inch page size in portrait orientation.

Save the docpart_templates.html file.

Zip the template.

zipTemplate("chapterTemplate.pdftx");

Use the Customized Templates in a Report

Use the customized template to create a PDF report on magic squares, that is, matrices whose columns, rows, and diagonals each add up to the same number.

Import DOM and Report API Packages

Import the DOM and Report API packages to avoid having to use fully qualified class names.

import mlreportgen.report.*;
import mlreportgen.dom.*;

Create a report container.

rpt = Report("report","pdf");

Add a Title Page

Create a title page and specify the title, subtitle, and author.

title = TitlePage("Title", "Magic Squares");
title.Subtitle = "Columns, Rows, Diagonals: All Equal Sums";
title.Author = "Albrecht Durer"; 

Use the custom title page template to generate the title page.

title.TemplateSrc = fullfile(pwd,"titlePageTemplate.pdftx");

Add the title page to the report.

add(rpt,title);

Add a Table of Contents

toc = TableOfContents;

Use the custom table of contents template to generate the table of contents.

toc.TemplateSrc = fullfile(pwd,"tableOfContentsTemplate.pdftx");

Add the table of contents to the report.

add(rpt,toc);

Add a Chapter

Create a chapter and specify the title.

chapter = Chapter("Introduction"); 

Use the custom chapter template to generate the chapter.

chapter.TemplateSrc = fullfile(pwd,"chapterTemplate.pdftx");

Create a section and add a paragraph to it.

sec1 = Section("What is a Magic Square?"); 
para = Paragraph(['A magic square is an N-by-N matrix '... 
'constructed from the integers 1 through N^2 '... 
'with equal row, column, and diagonal sums.']); 
add(sec1,para);

Add the section to the chapter.

add(chapter,sec1);

Create another section and add a paragraph to it.

sec2 = Section("Albrect Durer and the Magic Square"); 
para = Paragraph([ ... 
'The German artist Albrecht Durer (1471-1528) created '... 
'many woodcuts and prints with religious and '... 
'scientific symbolism. One of his most famous works, '... 
'Melancholia I, explores the depressed state of mind '... 
'which opposes inspiration and expression. '... 
'Renaissance astrologers believed that the Jupiter '... 
'magic square (shown in the upper right portion of '... 
'the image) could aid in the cure of melancholy. The '... 
'engraving''s date (1514) can be found in the '... 
'lower row of numbers in the square.']); 
add(sec2,para);

Add the section to the chapter.

add(chapter,sec2);

Add the chapter to the report.

add(rpt,chapter);

Close and View the Document

close(rpt);
rptview(rpt);