Main Content

Create a Custom HTML or PDF Template

To customize the format styles used in the default HTML, single-file HTML, or PDF template, copy the template and modify or add style definitions in the copy.

Copy the Template

You can copy a default template to use as the basis for your custom template. You can copy a default template in Report Explorer. Alternatively, you can use mlreportgen.dom.Document.createTemplate to create a template programmatically.

  1. In Report Explorer, select Tools > Edit Document Conversion Template.

  2. In the Library pane, select the template you want to copy. For example, select the Default HTML Template.

  3. In the Properties pane, click Copy template.

  4. In the file browser, navigate to where you want to save the template file.

    Select a path that is on the MATLAB® path, for example, in the MATLAB folder in your home folder.

    Specify the file name, using the default file extension for an HTML template (.htmtx), single-file HTML template (.htmt), or PDF template (.pdftx). Click Save.

  5. From the list of templates, select the template copy.

  6. In the dialog box, in the Template id and Display name boxes, specify a unique ID and display name for the template.

    The display name is the name that appears in the Report Explorer list of templates. Use the template ID to identify a template in your code. See Set a Template’s Properties.

  7. To save the template properties that you entered, click outside of the Properties pane.

Edit HTML or PDF Templates

A template consists of the main part (root.html), which defines the default page, and the document part templates (docpart_templates.html). For a single-file HTML template, all parts of the template are in a single file with an .htmt extension. For an HTML or PDF template, the parts of the template are separate files that are packaged into one file with an extension of .htmtx or .pdftx, respectively. When you edit an HTML or PDF template in Report Explorer, Report Explorer unzips the template. After you edit and save the template files, you must package the template files into a single template file by using zipTemplate.

You can make similar types of changes in HTML and PDF templates as you can in Word templates. See Customize Microsoft Word Component Templates.

The HTML and PDF templates in Report Explorer are similar, with these exceptions:

  • PDF templates define a page layout, including page headers and footers. You can modify the document part templates for these layout elements. PDF templates can use a set of DOM API HTML tags supplied for this purpose. See DOM API HTML Elements.

  • PDF templates can use only a subset of standard HTML elements. See Standard HTML Elements.

HTML and PDF templates use DOM API HTML tags to define a document part library and the document part templates within them. The <dplibrary> element defines the library. Your template can contain only one <dplibrary> tag, which is in place in the default template. The <dptemplate> element defines a document part. It takes an argument for the name. For example:

 <dptemplate name="rgChapter">

Look in the docpart_templates.html file in your template for some examples.

To edit a template:

  1. From the list of templates in the middle pane, select the template that you want to edit.

    Tip

    If the Report Explorer middle pane does not show a list of templates, then select Tools > Edit Document Conversion Template.

  2. In the right pane, click Open template.

  3. In the HTML editor, edit and save the template.

  4. If you edited an HTML or PDF template, package the template files into a single template file by using the zipTemplate function at the MATLAB command line.

Edit HTML or PDF Styles in a Template

You can customize or add format styles in your HTML or PDF template. You edit the styles using cascading style sheets (CSS).

For HTML templates, you can use any CSS property or selector. For PDF, you can use a subset. See PDF Style Sheets. You can also use XSL formatting objects (FO) to format elements in a PDF template. However, to simplify and streamline your code, use FO only for properties you cannot define using CSS.

  1. From the list of templates in the middle pane, select the template that you want to edit.

    Tip

    If the Report Explorer middle pane does not show a list of templates, then select Tools > Edit Document Conversion Template.

  2. In the right pane, click Open style sheet.

  3. In the HTML editor, edit the CSS.

    For information about editing a cascading style sheet, see documentation such as the W3Schools.com CSS tutorial.

  4. Save the style sheet.

  5. If you edited an HTML or PDF template, package the template files into a single template file by using the zipTemplate function at the MATLAB command line.

Assign the Template to a Report

You can assign your template to a Report Explorer report.

  1. In the Report Explorer, select Report Generator node.

  2. From the list of reports, select the report you want to assign the template to.

  3. In the Report Options dialog box, set File format to one of the (from template) options. Select your template from the list.

Select an HTML Editor

By default, when you edit an HTML or PDF style sheet, the style sheet appears in the MATLAB Editor.

To use a different editor:

  1. In the Report Explorer, select File > Preferences.

  2. In Edit HTML Command, enter a MATLAB expression that opens the HTML editor you want to use. For example:

    system('Dreamweaver %<FileName> &')

    When you open an HTML style sheet, the Report Explorer replaces FileName with the template that you selected. The ampersand (&) opens the editor in the background.

See Also

Related Examples

More About

External Websites