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:
- Create a print template entity.
- Create a print entity generation action.
- 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:
- On the main menu bar, click Manage .
- On the Manage page, click Print templates.
- On the Print templates page, click New Print template.
In the New Print template dialog box, define the following parameters.
Field Type Description Value Mandatory Name String Name of the print template String value Yes Target definition Drop-down list Definition of the entities that show in the PDF file An entity definition Yes Custom properties String Extra properties that the user needs to specify upon generating the PDF file Name and label values No Click Create.
- In the Upload print template dialog box, upload a print template package.
To create a print template entity from the Entities page:
- On the main menu bar, click Manage .
- On the Manage page, click Entities.
- On the Entities page, in the Search field, enter Print.EntityGenerationTemplate and then select it.
- On the Print template entities page, click New entity .
On the Entity detail page page, click the System tab and define the following parameters:
Field Type Description Value Mandatory Name String Name of the print template String value Yes Target definition Drop-down list Definition of the entities that show in the PDF file An entity definition Yes Asset Selection list The print template ZIP package A ZIP file No Custom properties String Extra properties that the user needs to specify upon generating the PDF file Name and label values No On the Entity detail page, click the Technical tab and define the following parameters.
Field Type Description Value Mandatory Identifier String Print template entity identifier String value Yes Inherits security Toggle switch Inherits the security configuration from its parent entity On/Off No
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:
- On the main menu bar, click Manage .
- On the Manage page, click Actions.
- On the Actions page, click New action.
In the New action dialog box, define the following parameters:
Field Type Description Value Mandatory Name String Name of the action String value Yes Label String Label of the action String value No Type Drop-down list Action type Select Print Entity Generation Yes Definition Drop-down list Target definition of the PDF generation An entity definition Yes Template Drop-down list Print template entity to use for the PDF generation A print template Yes 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:
- On the main menu bar, click Manage .
- On the Manage page, click Pages.
- In the page portal, search for and select the Asset details page.
- Click the Layout tab and then click the Entity operations component to edit it.
- On the EntityOperations component page, click Add operation, then select Generate PDF.
- Configure the Generate PDF operation display settings.
Define the following operation settings:
Field Type Description Value Mandatory Action Selection list Print entity generation action that triggers the PDF generation An action of type Print Entity Generation Yes High-resolution Toggle switch Generates a higher quality PDF On/Off No 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