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 = "PDF Demo Report",
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)
44 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
47 var pdf = new HtmlToPdfDocument()
49 GlobalSettings = globalSettings,
50 Objects = { objectSettings }
53 var file = _converter.Convert(pdf);
54 return File(file, "application/pdf");
58 [Route("generatePDF")]
60 public ActionResult GeneratePdf([FromBody] Term[] terms)
63 if (!ModelState.IsValid)
65 return BadRequest(ModelState);
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);
75 var globalSettings = new GlobalSettings
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",
84 var objectSettings = new ObjectSettings
87 HtmlContent = TemplateGenerator.GetHTMLString(terms),
88 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
91 var pdf = new HtmlToPdfDocument()
93 GlobalSettings = globalSettings,
94 Objects = { objectSettings }
97 var file = _converter.Convert(pdf);
98 if(Request.Headers["Accept"] == "application/base64")
99 return Content(Convert.ToBase64String(file));
101 return File(file, "application/pdf");