[英]how to include helpers in jsreport
我正在使用 Dotnet Core 2.2 生成帶有 JSReport 的報告( https://jsreport.net/ )我使用本地 JsReport,所以我將模板和數據發送到本地服務器並取回 pdf。 現在我需要在 javascript 中格式化日期,所以我需要將 moment.js 包含到我的模板中。 我不知道該怎么做。
在 html 我需要使用 moment.js 將 StartDate 格式化為格式化的 Date 我不知道如何將 moment.js 包含到模板中以及如何添加幫助程序。
對於 JSReport,我使用引擎 chrome-pdf 和車把作為模板引擎( https://jsreport.net/learn/handlebars )
我試圖將 moment.js 包含在
<script src="http://localhost:5000/public/js/moment.js"/>
<script>
function formatDate(date){
return moment(date).format("dd MMMM yyyy");
}
</script>
但是當我在 html 模板中調用{{{formateDate startDate}}}}時,似乎無法識別函數/助手。
我的 C# 代碼:
[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);
}
我的模板:
<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"> </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>
最小的例子看起來像這樣。
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"));
請注意,您的服務器應該啟用本地文件訪問。 否則調用require('moment')
會拋出。 提供的示例將在默認 jsreport 上正常工作,因為它已經安裝了 moment 模塊。 如果您想使用另一個自定義模塊,您需要使用npm i mycustommodule
安裝它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.