This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
TestViewsController.cs
293 lines (262 loc) · 8.99 KB
/
TestViewsController.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Rotativa.Models;
using Wkhtmltopdf.NetCore;
namespace Rotativa.Controllers
{
[Route("[controller]")]
[ApiController]
public class TestViewsController : ControllerBase
{
readonly IGeneratePdf _generatePdf;
public TestViewsController(IGeneratePdf generatePdf)
{
_generatePdf = generatePdf;
}
/// <summary>
/// View pdf generation as ActionResult
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("GetSSL")]
public async Task<IActionResult> GetSSL()
{
var data = new TestData
{
Text = "This is a test",
Number = 123456
};
return await _generatePdf.GetPdf("Views/TestBootstrapSSL.cshtml", data);
}
/// <summary>
/// View pdf generation as ActionResult
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("Get")]
public async Task<IActionResult> Get()
{
var data = new TestData
{
Text = "This is a test",
Number = 123456
};
return await _generatePdf.GetPdf("Views/Test.cshtml", data);
}
/// <summary>
/// View pdf generation as ByteArray
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("GetByteArray")]
public async Task<IActionResult> GetByteArray()
{
var data = new TestData
{
Text = "This is a test",
Number = 123456
};
var pdf = await _generatePdf.GetByteArray("Views/Test.cshtml", data);
var pdfStream = new System.IO.MemoryStream();
pdfStream.Write(pdf, 0, pdf.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
}
/// <summary>
/// "Hardcode" html pdf generation as ByteArray
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("GetByHtml")]
public IActionResult GetByHtml()
{
var html = @"<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<h1>This is a hardcoded test</h1>
</header>
<div>
<h2>456789</h2>
</div>
</body>";
var pdf = _generatePdf.GetPDF(html);
var pdfStream = new System.IO.MemoryStream();
pdfStream.Write(pdf, 0, pdf.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
}
/// <summary>
/// "Hardcode" html pdf generation as ByteArray and save file
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("SaveByHtml")]
public IActionResult SaveByHtml()
{
var html = @"<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<h1>This is a hardcoded test</h1>
</header>
<div>
<h2>456789</h2>
</div>
</body>";
System.IO.File.WriteAllBytes("testHard.pdf", _generatePdf.GetPDF(html));
return Ok();
}
/// <summary>
/// View pdf generation as ByteArray and save file
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("SaveFile")]
public async Task<IActionResult> SaveFile()
{
var data = new TestData
{
Text = "This is a test",
Number = 123456
};
System.IO.File.WriteAllBytes("test.pdf", await _generatePdf.GetByteArray("Views/Test.cshtml", data));
return Ok();
}
/// <summary>
/// "Hardcode" html pdf generation as ByteArray with Header
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("HeaderTest")]
public async Task<IActionResult> HeaderTest()
{
var options = new CustomOptions
{
HeaderHtml = "http://localhost/header.html",
PageOrientation = Wkhtmltopdf.NetCore.Options.Orientation.Landscape
};
_generatePdf.SetConvertOptions(options);
var data = new TestData
{
Text = "This is a test",
Number = 123456
};
var pdf = await _generatePdf.GetByteArray("Views/Test.cshtml", data);
var pdfStream = new System.IO.MemoryStream();
pdfStream.Write(pdf, 0, pdf.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
}
/// <summary>
/// "Hardcode" html pdf generation as ByteArray with Header
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("MarginTest")]
public async Task<IActionResult> MarginTest()
{
var options = new ConvertOptions
{
PageMargins = new Wkhtmltopdf.NetCore.Options.Margins()
{
Left = 0,
Right = 0
}
};
_generatePdf.SetConvertOptions(options);
var data = new TestData
{
Text = "This is a test",
Number = 123456
};
var pdf = await _generatePdf.GetByteArray("Views/Test.cshtml", data);
var pdfStream = new System.IO.MemoryStream();
pdfStream.Write(pdf, 0, pdf.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
}
/// <summary>
/// "Hardcode" html pdf generation as ByteArray with Header
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("HeaderPagingTest")]
public IActionResult HeaderPagingTest()
{
var options = new ConvertOptions
{
HeaderHtml = "http://localhost/header.html"
};
_generatePdf.SetConvertOptions(options);
string htmlCode = "";
using (WebClient client = new WebClient())
{
htmlCode = client.DownloadString("https://www.spacex.com/vehicles/starship/");
}
var pdf = _generatePdf.GetPDF(htmlCode);
var pdfStream = new System.IO.MemoryStream();
pdfStream.Write(pdf, 0, pdf.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
}
/// <summary>
/// "Hardcode" html pdf generation as ByteArray with Footer and Replacements
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("FooterTest")]
public async Task<IActionResult> FooterTest()
{
var kv = new Dictionary<string, string>
{
{ "username", "Veaer" },
{ "age", "20" },
{ "url", "google.com" }
};
var options = new ConvertOptions
{
FooterHtml = "http://localhost/footer.html",
Replacements = kv
};
_generatePdf.SetConvertOptions(options);
var data = new TestData
{
Text = "This is a test",
Number = 123456
};
var pdf = await _generatePdf.GetByteArray("Views/Test.cshtml", data);
var pdfStream = new System.IO.MemoryStream();
pdfStream.Write(pdf, 0, pdf.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
}
/// string form view pdf generation as ByteArray
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("GetFormByteArray")]
public async Task<IActionResult> GetFormByteArray()
{
var options = new ConvertOptions
{
EnableForms = true
};
_generatePdf.SetConvertOptions(options);
var pdf = await _generatePdf.GetByteArray("Views/Test_Form.cshtml");
var pdfStream = new System.IO.MemoryStream();
pdfStream.Write(pdf, 0, pdf.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
}
}
}