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 Tweet[]{
38 new Tweet("Test", "Good"),
39 new Tweet("Test2", "Bad"),
40 new Tweet("Another Tweet", "Bad"),
41 new Tweet("Another bad Tweet", "Bad")
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] Tweet[] tweets)
62 if (!ModelState.IsValid)
64 return BadRequest(ModelState);
67 // check for emptyString
68 foreach(Tweet tweet in tweets) {
69 if(tweet.Name.Length<1 || tweet.Sentiment.Length<1){
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(tweets),
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");