]> git.somenet.org - pub/jan/aic18.git/blob - service-reporting/Controllers/PdfController.cs
changed port to 8083 and added expose
[pub/jan/aic18.git] / service-reporting / Controllers / PdfController.cs
1 using DinkToPdf;
2 using DinkToPdf.Contracts;
3 using Microsoft.AspNetCore.Mvc;
4 using System.IO;
5 using PdfService.Utility;
6 using PdfService.Models;
7 namespace PdfService.Controllers
8 {
9
10     
11     [ApiController]
12     public class PdfController : ControllerBase
13     {
14         private IConverter _converter;
15
16         public PdfController(IConverter converter)
17         {
18             _converter = converter;
19         }
20
21         [Route("")]
22         [HttpGet]
23         public ActionResult GeneratePdfDemo()
24         {
25             var globalSettings = new GlobalSettings
26             {
27                 ColorMode = ColorMode.Color,
28                 Orientation = Orientation.Portrait,
29                 PaperSize = PaperKind.A4,
30                 Margins = new MarginSettings { Top = 0, Left = 0, Right = 0 },
31                 DocumentTitle = "PDF Demo Report",
32             };
33  
34             var objectSettings = new ObjectSettings
35             {
36                 PagesCount = true,
37                 HtmlContent = TemplateGenerator.GetHTMLString(new Tweet[]{
38                     new Tweet("Test", "Good"),
39                     new Tweet("Test2", "Bad"),
40                     new Tweet("Another Tweet", "Bad"),
41                     new Tweet("Another bad Tweet", "Bad")
42                     }),
43                 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
44             };
45  
46             var pdf = new HtmlToPdfDocument()
47             {
48                 GlobalSettings = globalSettings,
49                 Objects = { objectSettings }
50             };
51  
52             var file = _converter.Convert(pdf);
53             return File(file, "application/pdf");
54         }        
55
56         // GET /generatePDF
57         [Route("generatePDF")]
58         [HttpPost]
59         public ActionResult GeneratePdf([FromBody] Tweet[] tweets)
60         {
61             // check parameters
62             if (!ModelState.IsValid)
63             {
64                 return BadRequest(ModelState);
65             }
66
67             // check for emptyString
68             foreach(Tweet tweet in tweets) {
69                 if(tweet.Name.Length<1 || tweet.Sentiment.Length<1){
70                     return BadRequest(ModelState);
71                 }
72             }
73
74             var globalSettings = new GlobalSettings
75             {
76                 ColorMode = ColorMode.Color,
77                 Orientation = Orientation.Portrait,
78                 PaperSize = PaperKind.A4,
79                 Margins = new MarginSettings { Top = 0, Left = 0, Right = 0 },
80                 DocumentTitle = "PDF Report",
81             };
82  
83             var objectSettings = new ObjectSettings
84             {
85                 PagesCount = true,
86                 HtmlContent = TemplateGenerator.GetHTMLString(tweets),
87                 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
88             };
89  
90             var pdf = new HtmlToPdfDocument()
91             {
92                 GlobalSettings = globalSettings,
93                 Objects = { objectSettings }
94             };
95  
96             var file = _converter.Convert(pdf);
97             return File(file, "application/pdf");
98         }
99
100     }
101 }