2 using DinkToPdf.Contracts;
3 using Microsoft.AspNetCore.Mvc;
6 using PdfService.Utility;
7 using PdfService.Models;
8 namespace PdfService.Controllers
13 public class PdfController : ControllerBase
15 private IConverter _converter;
17 public PdfController(IConverter converter)
19 _converter = converter;
24 public ActionResult GeneratePdfDemo()
26 var globalSettings = new GlobalSettings
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 ,
35 var objectSettings = new ObjectSettings
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)
45 DefaultEncoding = "utf-8",
46 UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css")
48 FooterSettings = { FontSize = 9, Right = "Page [page] of [toPage] ", Line = false, Spacing = 0.812 }
52 var pdf = new HtmlToPdfDocument()
54 GlobalSettings = globalSettings,
55 Objects = { objectSettings }
58 var file = _converter.Convert(pdf);
59 return File(file, "application/pdf");
63 [Route("generatePDF")]
65 public ActionResult GeneratePdf([FromBody] Term[] terms)
68 if (!ModelState.IsValid)
70 return BadRequest(ModelState);
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);
80 var globalSettings = new GlobalSettings
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 ,
89 var objectSettings = new ObjectSettings
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 }
97 var pdf = new HtmlToPdfDocument()
99 GlobalSettings = globalSettings,
100 Objects = { objectSettings }
103 var file = _converter.Convert(pdf);
104 if(Request.Headers["Accept"] == "application/base64")
105 return Content(Convert.ToBase64String(file));
107 return File(file, "application/pdf");