2 using DinkToPdf.Contracts;
3 using Microsoft.AspNetCore.Mvc;
5 using PdfService.Utility;
6 using PdfService.Models;
7 namespace PdfService.Controllers
12 public class PdfController : ControllerBase
14 private IConverter _converter;
16 public PdfController(IConverter converter)
18 _converter = converter;
23 public ActionResult GeneratePdfDemo()
25 var globalSettings = new GlobalSettings
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",
34 var objectSettings = new ObjectSettings
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)
43 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
46 var pdf = new HtmlToPdfDocument()
48 GlobalSettings = globalSettings,
49 Objects = { objectSettings }
52 var file = _converter.Convert(pdf);
53 return File(file, "application/pdf");
57 [Route("generatePDF")]
59 public ActionResult GeneratePdf([FromBody] Term[] terms)
62 if (!ModelState.IsValid)
64 return BadRequest(ModelState);
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);
74 var globalSettings = new GlobalSettings
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",
83 var objectSettings = new ObjectSettings
86 HtmlContent = TemplateGenerator.GetHTMLString(terms),
87 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
90 var pdf = new HtmlToPdfDocument()
92 GlobalSettings = globalSettings,
93 Objects = { objectSettings }
96 var file = _converter.Convert(pdf);
97 return File(file, "application/pdf");