yavsc/Yavsc/ApiControllers/PdfEstimateController.cs

91 lines
2.8 KiB
C#

8 years ago
using System.IO;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
8 years ago
using System.Web.Routing;
using Microsoft.AspNet.Mvc.ViewComponents;
using Microsoft.AspNet.Razor;
8 years ago
namespace Yavsc.ApiControllers
{
8 years ago
using Models;
8 years ago
[Route("api/pdfestimate"), Authorize]
public class PdfEstimateController : Controller
{
8 years ago
ApplicationDbContext dbContext;
DefaultViewComponentHelper helper;
IViewComponentDescriptorCollectionProvider provider;
IViewComponentInvokerFactory factory;
RazorEngineHost host;
RazorTemplateEngine engine;
IViewComponentSelector selector;
public PdfEstimateController(
IViewComponentDescriptorCollectionProvider provider,
IViewComponentSelector selector,
IViewComponentInvokerFactory factory,
ApplicationDbContext context)
{
this.selector = selector;
this.provider = provider;
this.factory = factory;
helper = new DefaultViewComponentHelper(provider, selector, factory);
dbContext = context;
var language = new CSharpRazorCodeLanguage();
host = new RazorEngineHost(language)
{
DefaultBaseClass = "RazorPage",
DefaultClassName = "Estimate",
DefaultNamespace = "Yavsc",
};
// Everyone needs the System namespace, right?
host.NamespaceImports.Add("System");
engine = new RazorTemplateEngine(host);
/*
GeneratorResults razorResult =
engine.GenerateCode(
) */
}
[HttpGet("get/{id}", Name = "Get"), Authorize]
8 years ago
public IActionResult Get(long id)
{
var filename = $"estimate-{id}.pdf";
8 years ago
8 years ago
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = filename,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
8 years ago
FileInfo fi = new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
if (!fi.Exists) return Ok(new { Error = "Not generated" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
[HttpGet("estimate-{id}.tex", Name = "GetTex"), Authorize]
public IActionResult GetTex(long id)
{
Response.ContentType = "text/x-tex";
return ViewComponent("Estimate",new object[] { id, "LaTeX" });
8 years ago
}
[HttpPost("gen/{id}")]
8 years ago
public IActionResult GeneratePdf(long id)
8 years ago
{
return ViewComponent("Estimate",new object[] { id, "Pdf" } );
8 years ago
}
}
}