예제 #1
0
        public async Task <IActionResult> GeneratePdf(string[] postcodes, string[] svgGuids, Industry industry, string region)
        {
            var sources = _claimService
                          .GetSources(region)
                          .OrderBy(a => a.Id)
                          .ToDictionary(a => a.Id, a => a.Text);

            var pdfModel = new ComparePdfViewModel()
            {
                Industry = industry,
                Sources  = sources
            };

            var populatedPostcodes = new List <ComparisonPostcode>();

            foreach (var code in postcodes)
            {
                populatedPostcodes.Add(_claimService.ComparePostcode(code, industry, region));
            }

            pdfModel.Postcodes          = populatedPostcodes;
            pdfModel.TotalCards         = populatedPostcodes.Sum(x => x.Cards);
            pdfModel.TotalTransactions  = populatedPostcodes.Sum(x => x.Transactions);
            pdfModel.TotalMerchantSpend = populatedPostcodes.Sum(x => x.MerchantSpend);

            pdfModel.ImageDomain = _asposeOptions.ImageUrl;

            var cardSvgPath  = Path.Combine(_env.WebRootPath, "svgs", svgGuids[0] + ".svg");
            var transSvgPath = Path.Combine(_env.WebRootPath, "svgs", svgGuids[1] + ".svg");
            var spendSvgPath = Path.Combine(_env.WebRootPath, "svgs", svgGuids[2] + ".svg");

            pdfModel.CardSvg  = System.IO.File.ReadAllText(cardSvgPath);
            pdfModel.TransSvg = System.IO.File.ReadAllText(transSvgPath);
            pdfModel.SpendSvg = System.IO.File.ReadAllText(spendSvgPath);

            var html = _view.Render(@"Pdf\CompareView", pdfModel);

            var authClient = new Auth0.AuthenticationApi.AuthenticationApiClient("mudbath.au.auth0.com");

            var authReq = new AuthenticationRequest()
            {
                ClientId   = _auth0Config.ClientId,
                Username   = _auth0Config.Username,
                Password   = _auth0Config.Password,
                Connection = "Username-Password-Authentication",
                GrantType  = "password",
                Scope      = "openid"
            };
            var resp = await authClient.AuthenticateAsync(authReq);

            var httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri(this._asposeOptions.BaseUrl);
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resp.IdToken);

            var content = JsonConvert.SerializeObject(new { Html = html });

            var httpResp = await httpClient.PostAsync("api/pdf/generate", new StringContent(content, Encoding.UTF8, "application/json"));

            if (!httpResp.IsSuccessStatusCode)
            {
                var error = httpResp.Content.ReadAsStringAsync();
                Console.WriteLine(error);
                httpResp.EnsureSuccessStatusCode();
            }

            var pdf = await httpResp.Content.ReadAsStreamAsync();

            System.IO.File.Delete(cardSvgPath);
            System.IO.File.Delete(transSvgPath);
            System.IO.File.Delete(spendSvgPath);

            return(File(pdf, "application/pdf", $"comparison-{DateTime.Now.ToString("dd-MM-yyy HH:mm")}.pdf"));
        }