Print Template

print

Overview

The Print Template page is a visual report designer offering two layout kinds: rich-text templates (TinyMCE-based, drag-to-design, headers/footers, 2-column layouts, one-to-many sub-tables) and overlay layouts (a self-written coordinate canvas with millimetre positioning for printing onto pre-printed forms). Saved templates can be printed from list or detail pages.

Printing is provided by the EasyAdminBlazor.Print extension: reference that NuGet package and call builder.AddEasyAdminBlazorPrint(). Without the extension the framework ships no print pages or print implementation.

Features

  • Visual Design: WYSIWYG editing powered by TinyMCE
  • Drag & Drop Fields: Drag entity fields from the field tree into the editor
  • Field Type Support:
    • Regular fields (string, number, date, etc.)
    • Built-in variables (RowNumber, CurrentDate)
    • ManyToOne navigation properties (e.g., UserName → NickName)
    • OneToMany / ManyToMany collection navigations (auto-rendered as sub-tables)
  • Header/Footer: Insert header/footer regions via toolbar, automatically repeated on every page
  • 2-Column Layout: One-click insertion from the toolbar
  • Page Break: Manual page break control
  • Template Management: Multiple templates, matched to the entity type
  • Standalone Print Service: PrintService can be injected into any Razor component
  • Overlay Layout: with Kind=Overlay, design on a coordinate canvas — millimetre positioning, 1mm snap, printer-offset calibration, real-data preview (see below)

Access

  • Template List: Click the print button in any AdminTable toolbar (requires configured templates)
  • Template Management: Find "Print Template" in the system menu

Quick Start

1. Create a Print Template

  1. Navigate to System Menu → "Print Template" (/Admin/PrintTemplate)
  2. Click Add, enter the template name (English letters only) and target entity type
  3. Drag the fields into the editor, adjust the styles, and save

2. Design the Template

The designer consists of three areas:

Area Description
Left Field Tree All available fields of the current entity, grouped by type (Regular / System Variables / ManyToOne / OneToMany)
Center Editor TinyMCE rich text editor for free-form styling, tables, images, and text
Toolbar Print-specific buttons: Header, Footer, 2-Column Layout, Page Break

Steps:

  1. Drag fields from the left field tree into the editor — {{FieldName}} placeholders are inserted automatically
  2. Use toolbar buttons to insert headers/footers, 2-column layouts, and page breaks
  3. Edit text, adjust styles, insert images and tables directly in the editor
  4. Save the template

3. Printing

  1. Select the records to print (multi-select supported)
  2. Click the Print button in the toolbar
  3. If multiple templates exist for the entity, a dropdown menu appears for selection
  4. The browser print dialog opens automatically
@inject EasyAdminBlazor.PrintService PrintService

@code {
    async Task PrintRecords()
    {
        // Auto-detect template by entity type
        await PrintService.PrintAsync("EasyAdminBlazor.SysUser", new long[] { 1, 2, 3 });

        // Print by template ID
        await PrintService.PrintAsync(templateId, new long[] { 1, 2, 3 });

        // Print with specific template name
        await PrintService.PrintAsync("EasyAdminBlazor.SysUser", ids, "UserInfoTemplate");

        // Get rendered HTML only (no print dialog)
        var html = await PrintService.RenderPrintHtmlAsync(templateId, ids);
    }
}

Template Syntax

Placeholders

Use {{PropertyName}} as data placeholders in templates:

Syntax Description Example
{{PropertyName}} Regular field {{Name}} → "John"
{{RowNumber}} Row number (1-based) {{RowNumber}} → "1"
{{CurrentDate}} Current date {{CurrentDate}} → "2026-06-17"
{{Navigation.SubProperty}} ManyToOne related field {{User.NickName}} → "Admin"

Sub-Tables

For OneToMany / ManyToMany collection navigation properties, use <table data-nav-name="PropertyName">:

<table data-nav-name="OrderItems" border="1" style="width: 100%;">
    <thead>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>{{OrderItems.ProductName}}</td>
            <td>{{OrderItems.Price}}</td>
            <td>{{OrderItems.Quantity}}</td>
        </tr>
    </tbody>
</table>
  • data-nav-name must match the navigation property name
  • Rows inside <tbody> are template rows, repeated for each child record
  • {{RowNumber}} inside sub-tables represents the child record's row number

Header/Footer

<div class="print-header">
    <h3>Company Report</h3>
    <small>Print Date: {{CurrentDate}}</small>
</div>

<div class="print-footer">
    <p>Page {{RowNumber}}</p>
</div>
  • Headers repeat at the top of every page
  • Footers are sticky at the bottom of every page
  • Automatic page breaks (page-break-after: always)

2-Column Layout

Use the "2 Columns" toolbar button to insert:

<div style="display: flex; gap: 20px;">
    <div style="flex: 1;">Left content</div>
    <div style="flex: 1;">Right content</div>
</div>

Overlay Layout (Coordinate Positioning)

Rich-text templates suit flow-style reports; overlay printing (placing fields at fixed positions on pre-printed forms) needs millimetre coordinates, which TinyMCE's flow layout cannot do. A Kind field therefore distinguishes the two layout kinds:

  • Kind = Html (default): the original TinyMCE rich-text template, behaviour unchanged.
  • Kind = Overlay: overlay layout, designed on a self-written coordinate canvas; the result is stored in LayoutJson.

Existing rows get Kind = 0 (Html) after the column is added, so they are unaffected.

Canvas Designer

When creating/editing a template, switch Layout Type to Overlay and the middle becomes a coordinate canvas:

Area Description
Left tree Same field list as rich-text (PrintFieldInspector); dropping a field creates an element
Canvas Drawn in paper millimetres with a 1mm grid + 10mm major lines; drag to position, 8 handles to resize; arrow keys nudge 0.1mm (Shift = one grid), Delete removes
Right panel Layout settings (paper / margins / printer offset) + selected-element properties (position/size, text, font, align, border, wrap; sub-table nav & columns; image picker)
  • The origin is the paper's top-left corner, in millimetres; element x/y are paper-relative, while the printer compensation lives separately in offset and shifts the whole sheet at print time without changing canvas coordinates.
  • Snap: with 1mm snap on, both drop points and dragging stick to the grid; off keeps 0.1mm.
  • Elements outside the paper are outlined in red.
  • Background alignment: upload a scan of the pre-printed form as the background (background.src) in Layout Settings; it fills the canvas so fields can be dragged onto the matching spots, with adjustable opacity. The background is a design-time reference only and is never printed — the paper already carries the printed form, so only field values are printed.

Element Types

type Description
text Text; content supports placeholders
line Rule / rectangle, shown via border
table One-to-many sub-table via nav + columns, row height rowHeight
image Image; src chosen with AdminFileInput

LayoutJson Shape

{
  "paper": { "width": 210, "height": 297, "marginX": 10, "marginY": 10 },
  "offset": { "x": -1.5, "y": 2 },
  "background": { "src": "/uploads/form-scan.png", "opacity": 0.6 },
  "elements": [
    { "id": "e1", "type": "text", "x": 32, "y": 48, "w": 60, "h": 6,
      "text": "{{CustomerName}}", "font": "SimSun", "size": 10.5, "bold": false,
      "align": "left", "border": "", "wrap": true },
    { "id": "e2", "type": "line", "x": 10, "y": 40, "w": 190, "h": 0, "border": "1px solid #000" },
    { "id": "e3", "type": "table", "nav": "Details", "x": 10, "y": 90, "w": 190,
      "rowHeight": 5, "border": "1px solid #000",
      "columns": [ { "title": "Item", "field": "GoodsName", "w": 60, "align": "left", "format": "" } ] }
  ]
}
  • paper.marginX/marginY are design-time printable-area guides only; they are not emitted into the print output.
  • Sub-table row count follows the data; anything past the paper edge is not printed.

Placeholders

Same set as rich-text: {{Field}}, {{Nav.SubField}}, {{Field:format}}, {{RowNumber}}, {{CurrentDate}}; sub-table columns use field + optional format. Value resolution reuses PrintTemplateRenderer — no second rule set.

Calibration & Preview

  • Calibration sheet: the toolbar Calibration button prints a millimetre ruler + reference box (top-left exactly at 10,10mm) + centre cross. After printing, measure from the paper edge to the reference box's top-left corner and enter the difference from 10mm as the horizontal/vertical offset.
  • Preview: the toolbar Preview button renders with the target entity's first real record (PrintServiceImpl.RenderOverlayPreview), using the same data-loading and renderer as printing, shown in an in-page iframe below the canvas for viewing only. Note the edit dialog does not nest a Modal (nesting disables the edit dialog's events), and window.open is not used (popup-blocked from a Blazor Server async callback).

Paper Output

Overlay printing uses a separate overlay-print.js: it injects @page { size; margin:0 } to pin the paper and zero margins, and does not rewrite table widths. The rich-text printHtml rewrites table widths to 100% and adds body padding, so it cannot be reused for overlay.

PrintService API

PrintService is registered as a Scoped service and can be injected into any Razor component.

Method Parameters Description
LoadPrintTemplatesAsync entityTypeName Load all templates for an entity
RenderPrintHtmlAsync (templateId, ids) Render HTML by template ID
RenderPrintHtmlAsync (entityTypeName, ids, templateName?) Render HTML by entity type
PrintAsync (templateId, ids) Print directly by template ID
PrintAsync (entityTypeName, ids, templateName?) Print directly by entity type

Parameters:

  • entityTypeName: Full type name of the entity, e.g. EasyAdminBlazor.SysUser
  • ids: List of record IDs to print
  • templateId: Print template ID
  • templateName: Optional template name, matches Name or Description

Configuration

Template fields:

Field Type Description
Name string Template name, English letters only (regex: ^[A-Za-z]+$)
TargetEntity string Target entity full type name
HtmlContent string Template HTML content (auto-generated, used by Kind=Html)
Description string Template description / notes
Kind enum Layout kind: Html(0) rich-text / Overlay(1) overlay
LayoutJson string Overlay layout JSON (millimetre coordinates, Kind=Overlay)

Notes

  • Template names support English letters only (no Chinese or special characters)
  • The designer uses TinyMCE 8.3.2 (loaded from CDN)
  • Click the editor area first to activate TinyMCE before dragging fields
  • One-to-many sub-tables require manually inserting <table data-nav-name="PropertyName"> in the editor
  • report-designer.js is loaded automatically (via sync XHR) before printing — no manual script reference needed
  • Overlay layouts do not load TinyMCE; canvas gestures come from overlay-designer.js and paper output from overlay-print.js
  • Overlay coordinates are in millimetres; after changing printers, print a Calibration sheet first and enter the measured offset before production printing
  • Overlay sub-table rows grow with the data and are not printed past the paper edge; watch for red outlines (element outside the paper) while designing
  • The overlay background is shown only while designing and is absent from the print output; if the scan's aspect ratio differs from the paper, adjust the paper size first, then align