public async Task <FileContentResult> BuildPDF(INodeServices nodeServices, PDFRequest rawdata) { JObject options = JObject.Parse(rawdata.options); JSONResponse result = null; // execute the Node.js component to generate a PDF result = await nodeServices.InvokeAsync <JSONResponse>("./pdf.js", rawdata.html, options); return(new FileContentResult(result.data, "application/pdf")); }
public IActionResult Print() { var userData = HttpContext.Session.GetObjectFromJson <FarmData>("FarmData"); FileContentResult result = null; //JSONResponse result = null; var pdfHost = Environment.GetEnvironmentVariable("PDF_SERVICE_NAME"); //string pdfHost = Configuration["PDF_SERVICE_NAME"]; string targetUrl = pdfHost + "/api/PDF/BuildPDF"; ViewBag.Service = targetUrl; // call the microservice try { PDFRequest req = new PDFRequest(); HttpClient client = new HttpClient(); string rawdata = "<!DOCTYPE html><html><head><title></title></head><body><div style='width: 100%; background-color:lightgreen'>Section 1</div><br><div style='page -break-after:always; '></div><div style='width: 100%; background-color:lightgreen'>Section 2</div></body></html>"; req.html = rawdata; string payload = JsonConvert.SerializeObject(req); var request = new HttpRequestMessage(HttpMethod.Post, targetUrl); request.Content = new StringContent(payload, Encoding.UTF8, "application/json"); request.Headers.Clear(); // transfer over the request headers. foreach (var item in Request.Headers) { string key = item.Key; string value = item.Value; request.Headers.Add(key, value); } Task <HttpResponseMessage> responseTask = client.SendAsync(request); responseTask.Wait(); HttpResponseMessage response = responseTask.Result; ViewBag.StatusCode = response.StatusCode.ToString(); if (response.StatusCode == HttpStatusCode.OK) // success { var bytetask = response.Content.ReadAsByteArrayAsync(); bytetask.Wait(); result = new FileContentResult(bytetask.Result, "application/pdf"); } } catch (Exception e) { result = null; } return(result); }
public async Task <FileContentResult> PrintReportAsync(string content, bool portrait) { string reportHeader = string.Empty; FileContentResult result = null; var pdfHost = Environment.GetEnvironmentVariable("PDF_SERVICE_NAME"); string targetUrl = pdfHost + "/api/PDF/BuildPDF"; PDF_Options options = new PDF_Options(); options.border = new PDF_Border(); options.header = new PDF_Header(); options.footer = new PDF_Footer(); options.type = "pdf"; options.quality = "75"; options.format = "letter"; options.orientation = (portrait) ? "portrait" : "landscape"; options.fontbase = "/usr/share/fonts/dejavu"; options.border.top = ".25in"; options.border.right = ".25in"; options.border.bottom = ".25in"; options.border.left = ".25in"; options.header.height = "20mm"; options.header.contents = "<div><span style=\"font-size:14px\">Farm Name: " + _ud.FarmDetails().farmName + "<br />" + "Planning Year: " + _ud.FarmDetails().year + "</span></div><div style=\"float:right; vertical-align:top\">Printed: " + DateTime.Now.ToShortDateString() + "</div>"; options.footer.height = "15mm"; options.footer.contents = "<div><span style=\"color: #444;\">Page {{page}}</span>/<span>{{pages}}</span></div><div style=\"float:right\">Version " + _sd.GetStaticDataVersion() + "</div>"; // call the microservice try { PDFRequest req = new PDFRequest(); HttpClient client = new HttpClient(); reportHeader = await RenderHeader(); string rawdata = "<!DOCTYPE html>" + "<html>" + reportHeader + "<body>" + //"<div style='display: table; width: 100%'>" + //"<div style='display: table-row-group; width: 100%'>" + content + //"</div>" + //"</div>" + "</body></html>"; req.html = rawdata; req.options = JsonConvert.SerializeObject(options); req.options = req.options.Replace("fontbase", "base"); //FileContentResult res = await BuildPDF(nodeServices, req); //return res; string payload = JsonConvert.SerializeObject(req); var request = new HttpRequestMessage(HttpMethod.Post, targetUrl); request.Content = new StringContent(payload, Encoding.UTF8, "application/json"); request.Headers.Clear(); // transfer over the request headers. foreach (var item in Request.Headers) { string key = item.Key; string value = item.Value; request.Headers.Add(key, value); } Task <HttpResponseMessage> responseTask = client.SendAsync(request); responseTask.Wait(); HttpResponseMessage response = responseTask.Result; ViewBag.StatusCode = response.StatusCode.ToString(); if (response.StatusCode == HttpStatusCode.OK) // success { var bytetask = response.Content.ReadAsByteArrayAsync(); bytetask.Wait(); result = new FileContentResult(bytetask.Result, "application/pdf"); } else { string errorMsg = "Url: " + targetUrl + "\r\n" + "Result: " + response.ToString(); result = new FileContentResult(Encoding.ASCII.GetBytes(errorMsg), "text/plain"); } } catch (Exception e) { string errorMsg = "Exception " + "\r\n" + "Url: " + targetUrl + "\r\n" + "Result: " + e.Message + "\r\n" + "Result: " + e.InnerException.Message; result = new FileContentResult(Encoding.ASCII.GetBytes(errorMsg), "text/plain"); } return(result); }