logo
logo

Print templates

Sitecore Content Hub lets you generate a PDF file from a list of entities. You can use the print templates functionality to create a PDF based on entity properties and relations, such as the PDF report of annotations made on an asset.

To configure the generate PDF functionality in Content Hub, you need to perform the following steps:

  1. Create a print template entity.
  2. Create a print entity generation action.
  3. Configure the Generate PDF operation.
Note

You can download the generated PDF files from the Downloads page. Content Hub limits the processing of PDF file renditions to 2 GB.

Tip

You can find a complete PDF generation example in the Sitecore Content Marketing Platform (CMP)™ section.

Create a print template entity

The print template entity is a ZIP package containing .cshtml, .json, and .css files that define the structure of the PDF file using the entity properties and relations.

To create a print template entity from the Print templates page:

  1. On the main menu bar, click Manage .
  2. On the Manage page, click Print templates.
  3. On the Print templates page, click New Print template.
  4. In the New Print template dialog box, define the following parameters.

    FieldTypeDescriptionValueMandatory
    NameStringName of the print templateString valueYes
    Target definitionDrop-down listDefinition of the entities that show in the PDF fileAn entity definitionYes
    Custom propertiesStringExtra properties that the user needs to specify upon generating the PDF fileName and label valuesNo
  5. Click Create.

  6. In the Upload print template dialog box, upload a print template package.

To create a print template entity from the Entities page:

  1. On the main menu bar, click Manage .
  2. On the Manage page, click Entities.
  3. On the Entities page, in the Search field, enter Print.EntityGenerationTemplate and then select it.
  4. On the Print template entities page, click New entity .
  5. On the Entity detail page page, click the System tab and define the following parameters:

    FieldTypeDescriptionValueMandatory
    NameStringName of the print templateString valueYes
    Target definitionDrop-down listDefinition of the entities that show in the PDF fileAn entity definitionYes
    AssetSelection listThe print template ZIP packageA ZIP fileNo
    Custom propertiesStringExtra properties that the user needs to specify upon generating the PDF fileName and label valuesNo
  6. On the Entity detail page, click the Technical tab and define the following parameters.

    FieldTypeDescriptionValueMandatory
    IdentifierStringPrint template entity identifierString valueYes
    Inherits securityToggle switchInherits the security configuration from its parent entityOn/OffNo
Note

You can edit existing print template entities from the Print templates page and the entity detail pages.

Create a print entity generation action

After you create the print template entity, you use a print entity generation action to generate the PDF file based on the print template.

To create a print entity generation action:

  1. On the main menu bar, click Manage .
  2. On the Manage page, click Actions.
  3. On the Actions page, click New action.
  4. In the New action dialog box, define the following parameters:

    FieldTypeDescriptionValueMandatory
    NameStringName of the actionString valueYes
    LabelStringLabel of the actionString valueNo
    TypeDrop-down listAction typeSelect Print Entity GenerationYes
    DefinitionDrop-down listTarget definition of the PDF generationAn entity definitionYes
    TemplateDrop-down listPrint template entity to use for the PDF generationA print templateYes
  5. Click Save.

Note

The Template drop-down list contains the print templates linked to the selected target definition.

Configure the Generate PDF operation

After you create the print template entity and the print entity generation action, you must configure the Generate PDF operation. Calling this operation triggers the print entity generation action, which in turn starts the PDF generation.

You can configure the Generate PDF operation for page components on any page you want. The following procedure details how to add the operation to the Entity operations component on the asset detail page but the procedure is similar for any page and page component that supports the Generate PDF operation.

To add the Generate PDF operation:

  1. On the main menu bar, click Manage .
  2. On the Manage page, click Pages.
  3. In the page portal, search for and select the Asset details page.
  4. Click the Layout tab and then click the Entity operations component to edit it.
  5. On the EntityOperations component page, click Add operation, then select Generate PDF.
  6. Configure the Generate PDF operation display settings.
  7. Define the following operation settings:

    FieldTypeDescriptionValueMandatory
    ActionSelection listPrint entity generation action that triggers the PDF generationAn action of type Print Entity GenerationYes
    High-resolutionToggle switchGenerates a higher quality PDFOn/OffNo
  8. Click Save.

Note

For the download in high-resolution option to work, the asset.json file in the print template package should also include:

"highRes": {
    "property": "Renditions",
    "rendition": "thumbnail"
    }

Print template ZIP package example

This section provides an example of a print template ZIP package used with an entity named Module, which contains a list of related entities called Feature.

The ZIP package contains the following files:

  • Module.json - main file specifying the Module properties to use, and any related entities (in this case, Feature entities).
  • Module.cshtml - specifies the structure of the defined Module properties in the PDF file using HTML format. This file can reference other .cshtml files to embed them within the structure.
  • Feature.cshtml - specifies the structure of the defined Feature properties in the PDF file using HTML format. This file can be embedded within other .cshtml files.
  • CSS file: this file specifies the CSS related to the .cshtml files.

Module json

The following code is an example of the Module.json file:

{
  "template": {
    "type": "Template",
    "file": "module.cshtml",
    "source": {
      "type": "entity",
      "variables": [
        {
          "name": "TopTitleLabel",
          "type": "snippet",
          "value": {
            "en-US": "Module Specifications"
          }
        },
        {
          "name": "Preview",
          "type": "rendition",
          "lowRes": {
            "property": "Renditions",
            "rendition": "preview"
          },
          "highRes": {
            "property": "MainFile"
          },
          "relation": {
            "name": "ModuleToMasterAsset",
            "role": "Parent",
            "relation": {
              "name": "MasterFile",
              "role": "Parent"
            }
          }
        },
        {
          "name": "FeaturesLabel",
          "type": "snippet",
          "value": {
            "en-US": "Features"
          }
        },
        {
          "name": "ModuleName",
          "type": "property",
          "property": "ModuleName"
        },
        {
          "name": "ModuleDescription",
          "type": "property",
          "property": "ModuleDescription"
        },
        {
          "name": "Features",
          "type": "Template",
          "template": {
            "type": "Template",
            "file": "features.cshtml",
            "source": {
              "type": "relation",
              "relation": {
                "name": "ModuleToFeature",
                "role": "Parent"
              },
              "variables": [
                {
                  "name": "FeatureName",
                  "type": "property",
                  "property": "FeatureName"
                },
                {
                  "name": "FeatureDescription",
                  "type": "property",
                  "property": "FeatureDescription"
                },
                {
                  "name": "FeatureType",
                  "type": "property",
                  "property": "FeatureType"
                }
              ]
            }
          }
        }
      ]
    }
  }
}
Note

To correctly print the option list variables, use "type": "datasource".

Module cshtml

The following code is an example of the Module.cshtml file:

@model Stylelabs.M.Print.EntityGeneration.Models.Model

<html>
  <head>
    <link rel="stylesheet" href="bootstrap.min.css" />
    <link rel="stylesheet" href="styles.css" />
    <title>Module specification</title>
  </head>
  <body>
    <div class="top-header">
      <img src="/logo.png" class="logo" />
      <h5 class="top-title">
        @Raw(@Model.Properties["$$TopTitleLabel$$"].FirstOrDefault())
      </h5>
    </div>
    <div class="container-wrapper">
      <div class="container">
        <div class="row">
          <div class="col-xs-8">
            <h1>@Raw(@Model.Properties["$$ModuleName$$"].FirstOrDefault())</h1>
            <div class="lead">
              @Raw(@Model.Properties["$$ModuleDescription$$"].FirstOrDefault())
            </div>
          </div>
          <div class="col-xs-4">
            <img src="@(@Model.Properties["$$Preview$$"].FirstOrDefault())" d>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
      <h5>@Raw(@Model.Properties["$$FeaturesLabel$$"].FirstOrDefault())</h5>
      <div class="features-wrapper">
        @foreach (var feature in @Model.Properties["$$Features$$"]) {
        @Raw(feature) }
      </div>
    </div>
  </body>
</html>

Feature cshtml

The following code is an example of the Feature.cshtml file:

@model Stylelabs.M.Print.EntityGeneration.Models.Model

<h4>
  @Raw(@Model.Properties["$$FeatureName$$"].FirstOrDefault()) @*<span
    class="type"
    >@Raw(@Model.Properties["$$FeatureType$$"].FirstOrDefault())</span
  >*@
</h4>
<div class="feature-description">
  @Raw(@Model.Properties["$$FeatureDescription$$"].FirstOrDefault())
</div>

Can we improve this article ? Provide feedback