Hierarchy

  • PrintPowers

Index

Methods

Methods

Static processDocument

  • Creates a print document from a template and stores it for retrieval via a context command used by andify.js. Templates are based on the Handlebars template language and currently support all the builtin aspects except the abilities to use Block and Custom Helpers.

    throws

    {TypeError} When template is missing.

    throws

    {TypeError} When objectModel is missing.

    throws

    {Error} When unable to create document

    throws

    {Error} When unable to store created document

    example

    The following example uses the template expression {{report}} which generates an html table with headers and rows. It just requires that the data being fed to the power is shaped accordingly. If the root object property name is not passed the expression will assume that the header and row properties are in the root of the data model. In this example the parameter reportOne was passed in {{report reportOne}}. The processDocument method returns a PrintProcessResponse that includes the property filedId which is used with a context command to retrieve the template and have it rendered.

    
    import { PrintPowers, PrintProcessResponse } from "@andi/powers";
    
    export async function generateReport(): Promise<string> {
         const response: PrintProcessResponse = await PrintPowers.processDocument(template, data);
         return response.fileId;
    }
    
    const data = {
        reportOne: {
             header: [{name: "Name"},
                      {name: "Order"},
                      {name: "Price"}],
             rows: [
                       ["Alice","4 apples","$10"],
                       ["Bob","5 bananas", "$5"],
                       ["Carl", "30 cherries", "$8"]
                  ]
         }
    };
    
    const template = `
    <html lang="en">
      <head>
          <meta charset="utf-8">
          <title>Print Test</title>
          <style>
            .h {
              border-bottom: 1px solid black;
              padding-bottom: 8px;
            }
            .s {
              margin-top: 4px;
              margin-bottom: 8px;
            }
            .p {
              border: 1px solid black;
              border-radius: 4px;
              padding: 4px;
            }
            .print-report {
              border-collapse: collapse;
            }
            td {
              border: 1px solid black;
              padding: 4px;
            }
            @media print {
              .h {
                display: none;
              }
            }
          </style>
    
      </head>
      <body>
        <div class="s">
          This is an example print out.
        </div>
        <div class="s">
           {{report "reportOne"}}
        </div>
      </body>
    `;
    

    Type parameters

    • T = unknown

    Parameters

    • template: string

      Template used the print service to transform objectModel utilizing handlebar.js format

    • objectModel: T

      Data being processed by the template

    Returns Promise<PrintProcessResponse>

Generated using TypeDoc