]> git.somenet.org - pub/jan/aic18.git/blob - service-reporting/Controllers/PdfController.cs
Merge branch '17-change-reporting-service-rest-parameter' into 'master'
[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 Term[]{
38                     new Term("Good Term", 0.9),
39                     new Term("Term 1", 0.5),
40                     new Term("Another Term", 0.3),
41                     new Term("Another bad Tweet", 0.1)
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] Term[] terms)
60         {
61             // check parameters
62             if (!ModelState.IsValid)
63             {
64                 return BadRequest(ModelState);
65             }
66
67             // check parameter if they have correct form
68             foreach(Term term in terms) {
69                 if(term.Name.Length<1 || term.Sentiment<0.0 || term.Sentiment>1.0){
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(terms),
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 }