Generate PDF from templates

Sitecore Content Hub lets you generate a PDF file from print templates based on entity properties and relations, such as the PDF report of annotations to an asset.

Important

To generate PDFs, you must have at least Read permissions for both the Print.EntityGenerationTemplate and M.Action definitions.

To configure the PDF generation functionality, you need to:

Note

You can download the generated PDF files on the Downloads page. Content Hub limits the processing of PDF renditions to files of 2GB or less.

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: Contains the CSS related to the .cshtml files.

Module json

Here is an example of the module.json file:

RequestResponse
    json
    {
        "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

Here is an example of the module.cshtml file:

RequestResponse
html
@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

Here is an example of the feature.cshtml file:

RequestResponse
html
    @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>

Do you have some feedback for us?

If you have suggestions for improving this article,