]> git.somenet.org - pub/jan/aic18.git/blob - service-reporting/Controllers/PdfController.cs
Prevent default action on reset button click
[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 System;
6 using PdfService.Utility;
7 using PdfService.Models;
8 namespace PdfService.Controllers
9 {
10
11     
12     [ApiController]
13     public class PdfController : ControllerBase
14     {
15         private IConverter _converter;
16
17         public PdfController(IConverter converter)
18         {
19             _converter = converter;
20         }
21
22         [Route("")]
23         [HttpGet]
24         public ActionResult GeneratePdfDemo()
25         {
26             var globalSettings = new GlobalSettings
27             {
28                 ColorMode = ColorMode.Color,
29                 Orientation = Orientation.Portrait,
30                 PaperSize = PaperKind.A4,
31                 Margins = new MarginSettings { Top =0, Left = 0, Right = 0 },
32                 DocumentTitle = "Sentiment Analysis - Advanced Report - " + DateTime.Now ,
33             };
34  
35             var objectSettings = new ObjectSettings
36             {
37                 PagesCount = true,
38                 HtmlContent = TemplateGenerator.GetHTMLString(new Term[]{
39                     new Term("Good Term", 0.9),
40                     new Term("Term 1", 0.5),
41                     new Term("Another Term", 0.3),
42                     new Term("Another bad Tweet", 0.1)
43                     }),
44                 WebSettings = { 
45                     DefaultEncoding = "utf-8",
46                     UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css")
47                 },
48                 FooterSettings = { FontSize = 9, Right = "Page [page] of [toPage]    ", Line = false, Spacing = 0.812  }
49
50             };
51  
52             var pdf = new HtmlToPdfDocument()
53             {
54                 GlobalSettings = globalSettings,
55                 Objects = { objectSettings }
56             };
57  
58             var file = _converter.Convert(pdf);
59             return File(file, "application/pdf");
60         }        
61
62         // GET /generatePDF
63         [Route("generatePDF")]
64         [HttpPost]
65         public ActionResult GeneratePdf([FromBody] Term[] terms)
66         {
67             // check parameters
68             if (!ModelState.IsValid)
69             {
70                 return BadRequest(ModelState);
71             }
72
73             // check parameter if they have correct form
74             foreach(Term term in terms) {
75                 if(term.Name.Length<1 || term.Sentiment<0.0 || term.Sentiment>1.0){
76                     return BadRequest(ModelState);
77                 }
78             }
79
80             var globalSettings = new GlobalSettings
81             {
82                 ColorMode = ColorMode.Color,
83                 Orientation = Orientation.Portrait,
84                 PaperSize = PaperKind.A4,
85                 Margins = new MarginSettings { Top = 0, Left = 0, Right = 0 },
86                 DocumentTitle = "Sentiment Analysis - Advanced Report - " + DateTime.Now ,
87             };
88  
89             var objectSettings = new ObjectSettings
90             {
91                 PagesCount = true,
92                 HtmlContent = TemplateGenerator.GetHTMLString(terms),
93                 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") },
94                 FooterSettings = { FontSize = 9, Right = "Page [page] of [toPage]    ", Line = false, Spacing = 0.812  }
95             };
96  
97             var pdf = new HtmlToPdfDocument()
98             {
99                 GlobalSettings = globalSettings,
100                 Objects = { objectSettings }
101             };
102             
103             var file = _converter.Convert(pdf);
104             if(Request.Headers["Accept"] == "application/base64")
105                 return Content(Convert.ToBase64String(file));
106             else
107                 return File(file, "application/pdf");
108         }
109
110     }
111 }