Пример #1
0
        public async Task <IActionResult> GetPDF([FromRoute] int Id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var indexAcceptPdf = Request.Headers["Accept"].ToList().IndexOf("application/pdf");
                int timeoffsset    = Convert.ToInt32(Request.Headers["x-timezone-offset"]);
                var model          = await Facade.ReadByIdAsync(Id);

                if (model == null)
                {
                    Dictionary <string, object> Result =
                        new ResultFormatter(ApiVersion, General.NOT_FOUND_STATUS_CODE, General.NOT_FOUND_MESSAGE)
                        .Fail();
                    return(NotFound(Result));
                }
                else
                {
                    //var oldKanbanModel = !model.OldKanbanId.Equals(0) ? await Facade.ReadByIdAsync(model.OldKanbanId) : null;

                    var viewModel = Mapper.Map <FabricQualityControlViewModel>(model);
                    //var oldKanbanViewModel = Mapper.Map<KanbanViewModel>(oldKanbanModel);

                    var          PdfTemplate = new FabricQualityControlPdfTemplate();
                    MemoryStream stream      = PdfTemplate.GeneratePdfTemplate(viewModel, timeoffsset);
                    return(new FileStreamResult(stream, "application/pdf")
                    {
                        FileDownloadName = viewModel.Code + ".pdf"
                    });
                }
            }
            catch (Exception e)
            {
                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.INTERNAL_ERROR_STATUS_CODE, e.Message)
                    .Fail();
                return(StatusCode(General.INTERNAL_ERROR_STATUS_CODE, Result));
            }
        }
        public async Task <IActionResult> GetPDF([FromRoute] int Id)
        {
            if (!ModelState.IsValid)
            {
                var exception = new
                {
                    error = ResultFormatter.FormatErrorMessage(ModelState)
                };
                return(new BadRequestObjectResult(exception));
            }

            try
            {
                var indexAcceptPdf = Request.Headers["Accept"].ToList().IndexOf("application/pdf");
                int timeoffsset    = Convert.ToInt32(Request.Headers["x-timezone-offset"]);
                var model          = await _service.ReadById(Id);

                if (model == null)
                {
                    return(StatusCode((int)HttpStatusCode.NotFound, "Not Found"));
                }
                else
                {
                    var          PdfTemplate = new FabricQualityControlPdfTemplate();
                    MemoryStream stream      = PdfTemplate.GeneratePdfTemplate(model, timeoffsset);
                    return(new FileStreamResult(stream, "application/pdf")
                    {
                        FileDownloadName = model.Code + ".pdf"
                    });
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }