]> git.somenet.org - pub/jan/aic18.git/blob - PdfService/Controllers/PdfController.cs
Issue #3:
[pub/jan/aic18.git] / PdfService / Controllers / PdfController.cs
1 using DinkToPdf;
2 using DinkToPdf.Contracts;
3 using Microsoft.AspNetCore.Mvc;
4 using System.IO;
5 using PdfService.Utility;
6 using PdfService.Models;
7 namespace PdfService.Controllers
8 {
9
10     
11     [ApiController]
12     public class PdfController : ControllerBase
13     {
14         private IConverter _converter;
15
16         public PdfController(IConverter converter)
17         {
18             _converter = converter;
19         }
20         // GET /generatePDF
21         [Route("generatePDF")]
22         [HttpGet]
23         public ActionResult Get()
24         {
25             var globalSettings = new GlobalSettings
26             {
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 Report",
32             };
33  
34             var objectSettings = new ObjectSettings
35             {
36                 PagesCount = true,
37                 HtmlContent = TemplateGenerator.GetHTMLString(new Tweet[]{new Tweet("Test", "Good"), new Tweet("Test2", "Bad")}),
38                 WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
39             };
40  
41             var pdf = new HtmlToPdfDocument()
42             {
43                 GlobalSettings = globalSettings,
44                 Objects = { objectSettings }
45             };
46  
47             var file = _converter.Convert(pdf);
48             return File(file, "application/pdf");
49         }
50
51     }
52 }