how to include helpers in jsreport

nig*_*2k1 4 javascript handlebars.js .net-core jsreport

I am using Dotnet Core 2.2 to generate report with JSReport (https://jsreport.net/) I use local JsReport so I sent my template and data to the local server and get back the pdf. Now I need to format date in javascript, so I require to include moment.js into my templates. I have no idea how to do that.

In html I need to format StartDate to formated Date using moment.js I have no idea how to include moment.js into template and how to add helpers.

For JSReport I am using engine chrome-pdf and handlebars as templating engine (https://jsreport.net/learn/handlebars)

I have tried to include moment.js in the

<script src="http://localhost:5000/public/js/moment.js"/>
<script>
function formatDate(date){
return moment(date).format("dd MMMM yyyy");
}
</script>
Run Code Online (Sandbox Code Playgroud)

but when I called {{{formateDate startDate}}}} inside the html template, it seems the function/helpers not recognized.

My C# code:

[HttpGet("reporting")]
    public async  Task<IActionResult> Test(){
        var sdr = await _repo.GetStaffDefaultRates();  
        var dto  = _mapper.Map<List<StaffDefaultRate>, List<ReportDto>>(sdr);          
        var x = new {
            bootstrapcss = "http://localhost:5000/public/css/bootstrap.min.css",
            publicPath = "http://localhost:5000/public/",
            message = "hello world",
            today = DateTime.Now.ToString("dd MMM yyyy"),
            staffRates = dto,
        };
        var staffRates = _fileRepository.ReadReportTemplate("StaffRates.html");
        var rs = new ReportingService("http://localhost:5488");
        var report = await rs.RenderAsync(new RenderRequest(){
            Template = new Template(){
                Recipe = Recipe.ChromePdf,
                Engine = Engine.Handlebars,
                Content = staffRates,
            }, Data = x 
        }); 
        var memory = new MemoryStream();
        await report.Content.CopyToAsync(memory);
        memory.Position = 0 ;
        return File(memory, "application/pdf");
        // return Ok(staffRates);
    }
Run Code Online (Sandbox Code Playgroud)

My templates:

<html>

<head>
    <title> Staff Default Rates</title>
    <link href="{{bootstrapcss}}" rel="stylesheet">
    </link>
</head>

<body>
    <div class="sticky-top">
        <div class="row pt-3 header">
            <div class="col-6 text-left">
                <img src="http://localhost:5000/public/logo-full.jpg" width="100%"></img>
            </div>
            <div class="col-6 text-right"></div>
        </div>
    </div>
    <div class="container">
        <div class="row mt-5">
            <div class="col-12 text-center">
                <h2>Staff List {{today}}</h2>
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th rowspan="2">&nbsp;</th>
                            <th rowspan="2" colspan="1" class="align-middle">Staff Roles</th>
                            <th > Start Date </th>
                        </tr>
                    </thead>
                    <tbody>
                        {{#each staffRates}}
                        <tr>
                            <td>{{id}}</td>
                            <td>{{staffType}}</td>
                            <td class='text-center'>{{startDate}}</td>
                        </tr>
                        {{/each}}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="container">
        <div class="row pb-3 footer fixed-bottom">
            <div class="col-12 text-center">
                <img src="http://localhost:5000/public/footer.jpg" width="100%"></img>
            </div>
        </div>
    </div>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

Jan*_*aha 6

最小的例子看起来像这样。

var result = rs.RenderAsync(new RenderRequest  {
    Template = new Template
    {
        Content = "{{myFormat date}}",
        Helpers = @"
const moment = require('moment')
function myFormat(d) { 
  return moment(d).format('dd MMMM yyyy')
}",
        Engine = Engine.Handlebars,
        Recipe = Recipe.ChromePdf
    },
    Data = new
    {
        date = DateTime.Now
    }
}).Result;

result.Content.CopyTo(File.OpenWrite("report.pdf"));
Run Code Online (Sandbox Code Playgroud)

请注意,您的服务器应启用本地文件访问。否则调用require('moment')会抛出。提供的示例将在默认 jsreport 上工作,因为它已经安装了 moment 模块。如果您想使用另一个自定义模块,您需要使用npm i mycustommodule.