-
Notifications
You must be signed in to change notification settings - Fork 22
/
HomeController.cs
225 lines (190 loc) · 6.63 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using Microsoft.AspNetCore.Mvc;
using WebApp.Model;
using jsreport.AspNetCore;
using jsreport.Types;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace WebApp.Controllers
{
public class HomeController : Controller
{
public IJsReportMVCService JsReportMVCService { get; }
public HomeController(IJsReportMVCService jsReportMVCService)
{
JsReportMVCService = jsReportMVCService;
}
public IActionResult Index()
{
return View();
}
[MiddlewareFilter(typeof(JsReportPipeline))]
public IActionResult Invoice()
{
HttpContext.JsReportFeature().Recipe(Recipe.ChromePdf);
return View(InvoiceModel.Example());
}
[MiddlewareFilter(typeof(JsReportPipeline))]
public IActionResult InvoiceDownload()
{
HttpContext.JsReportFeature().Recipe(Recipe.ChromePdf)
.OnAfterRender((r) => HttpContext.Response.Headers["Content-Disposition"] = "attachment; filename=\"myReport.pdf\"");
return View("Invoice", InvoiceModel.Example());
}
[MiddlewareFilter(typeof(JsReportPipeline))]
public async Task<IActionResult> InvoiceWithHeader()
{
var header = await JsReportMVCService.RenderViewToStringAsync(HttpContext, RouteData, "Header", new { });
HttpContext.JsReportFeature()
.Recipe(Recipe.ChromePdf)
.Configure((r) => r.Template.Chrome = new Chrome
{
HeaderTemplate = header,
DisplayHeaderFooter = true,
MarginTop = "1cm",
MarginLeft = "1cm",
MarginBottom = "1cm",
MarginRight = "1cm"
});
return View("Invoice", InvoiceModel.Example());
}
[MiddlewareFilter(typeof(JsReportPipeline))]
public IActionResult Items()
{
HttpContext.JsReportFeature()
.Recipe(Recipe.HtmlToXlsx)
.Configure((r) => r.Template.HtmlToXlsx = new HtmlToXlsx() { HtmlEngine = "chrome" });
return View(InvoiceModel.Example());
}
[MiddlewareFilter(typeof(JsReportPipeline))]
public IActionResult ItemsExcelOnline()
{
HttpContext.JsReportFeature()
.Configure(req => req.Options.Preview = true)
.Recipe(Recipe.HtmlToXlsx)
.Configure((r) => r.Template.HtmlToXlsx = new HtmlToXlsx() { HtmlEngine = "chrome" });
return View("Items", InvoiceModel.Example());
}
[MiddlewareFilter(typeof(JsReportPipeline))]
public async Task<IActionResult> InvoiceWithCover()
{
var coverHtml = await JsReportMVCService.RenderViewToStringAsync(HttpContext, RouteData, "Cover", new { });
HttpContext.JsReportFeature()
.Recipe(Recipe.ChromePdf)
.Configure((r) =>
{
r.Template.PdfOperations = new[]
{
new PdfOperation()
{
Template = new Template
{
Content = coverHtml,
Engine = Engine.None,
Recipe = Recipe.ChromePdf
},
Type = PdfOperationType.Prepend
}
};
});
return View("Invoice", InvoiceModel.Example());
}
[MiddlewareFilter(typeof(JsReportPipeline))]
public async Task<IActionResult> ChartWithPrintTrigger()
{
HttpContext.JsReportFeature()
.Recipe(Recipe.ChromePdf)
.Configure(cfg =>
{
cfg.Template.Chrome = new Chrome
{
WaitForJS = true
};
});
return View("Chart", new { });
}
[MiddlewareFilter(typeof(JsReportPipeline))]
public async Task<IActionResult> ToC()
{
HttpContext.JsReportFeature()
.Recipe(Recipe.ChromePdf)
.Configure(cfg =>
{
cfg.Template.Helpers = @"
const jsreport = require('jsreport-proxy')
const headings = []
function heading(h, opts) {
const { id, content, parent } = opts.hash
headings.push({ id, content, parent })
const headingHtml = `<${h} id='${id}'>${content}</${h}>`
//we put to the html hidden mark which we in the second render use to find out the page number
//of the heading
const hiddenMark = pdfAddPageItem.call(this, {
...opts,
hash: {
headingId: id
}
})
return headingHtml + hiddenMark
}
async function toc(opts) {
// we need to postpone the toc printing till all the headings are registered
// using heading helper
await jsreport.templatingEngines.waitForAsyncHelpers()
let res = ''
for (let { id, content, parent } of headings) {
res += opts.fn({
...this,
pageNumber: getPageNumber(id, opts),
content,
parent,
id
})
}
return res
}
function getPageNumber(id, opts) {
if (!opts.data.root.$pdf) {
return 'NA'
}
for (let i = 0; i < opts.data.root.$pdf.pages.length; i++) {
const item = opts.data.root.$pdf.pages[i].items.find(item => item.headingId === id)
if (item) {
return i + 1
}
}
return 'NOT FOUND'
}";
cfg.Template.Scripts = new[]
{
new Script
{
Content = @"
const jsreport = require('jsreport-proxy')
function beforeRender(req, res) {
req.options.pdfUtils = { removeHiddenMarks: false }
}
async function afterRender (req, res) {
if (req.data.secondRender) {
return
}
const p = await jsreport.pdfUtils.parse(res.content, true)
const finalR = await jsreport.render({
template: {
...req.template
},
data: {
...req.data,
$pdf: p,
secondRender: true
}
})
res.content = finalR.content
}"
}
};
cfg.Template.Engine = Engine.Handlebars;
});
return View("ToC", new { });
}
}
}