]> git.somenet.org - pub/jan/aic18.git/blob - service-reporting/Controllers/PdfController.cs
Merge remote-tracking branch 'origin/master' into 46-create-basic-website
[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 = "PDF Demo Report",
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 = { DefaultEncoding = "utf-8", UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
45             };
46  
47             var pdf = new HtmlToPdfDocument()
48             {
49                 GlobalSettings = globalSettings,
50                 Objects = { objectSettings }
51             };
52  
53             var file = _converter.Convert(pdf);
54             return File(file, "application/pdf");
55         }        
56
57         // GET /generatePDF
58         [Route("generatePDF")]
59         [HttpPost]
60         public ActionResult GeneratePdf([FromBody] Term[] terms)
61         {
62             // check parameters
63             if (!ModelState.IsValid)
64             {
65                 return BadRequest(ModelState);
66             }
67
68             // check parameter if they have correct form
69             foreach(Term term in terms) {
70                 if(term.Name.Length<1 || term.Sentiment<0.0 || term.Sentiment>1.0){
71                     return BadRequest(ModelState);
72                 }
73             }
74
75             var globalSettings = new GlobalSettings
76             {
77                 ColorMode = ColorMode.Color,
78                 Orientation = Orientation.Portrait,
79                 PaperSize = PaperKind.A4,
80                 Margins = new MarginSettings { Top = 0, Left = 0, Right = 0 },
81                 DocumentTitle = "PDF Report",
82             };
83  
84             var objectSettings = new ObjectSettings
85             {
86                 PagesCount = true,
87                 HtmlContent = TemplateGenerator.GetHTMLString(terms),
88                 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
89             };
90  
91             var pdf = new HtmlToPdfDocument()
92             {
93                 GlobalSettings = globalSettings,
94                 Objects = { objectSettings }
95             };
96             
97             var file = _converter.Convert(pdf);
98             if(Request.Headers["Accept"] == "application/base64")
99                 return Content(Convert.ToBase64String(file));
100             else
101                 return File(file, "application/pdf");
102         }
103
104     }
105 }