Esempio n. 1
0
 public HttpResponseMessage GetPDF(ReportModel reportModel)
 {
     return(CreateHttpResponse(() =>
     {
         if (reportModel != null)
         {
             var user = GetUserDetailsByUserName(UserName);
             if (user != null)
             {
                 reportModel.Company = user.CompanyName;
                 reportModel.Email = user.Email;
                 reportModel.UserID = UserID;
                 reportModel.UserName = UserName;
                 reportModel.Phone = user.Phone;
                 reportModel.brand = user.CompanyName;
                 var pdfStream = _pdfProcesor.GeneratePDF(reportModel);
                 if (pdfStream != null)
                 {
                     var memoryStream = new MemoryStream(pdfStream.ToArray());
                     if (memoryStream != null)
                     {
                         HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                         result.Content = new StreamContent(memoryStream);
                         _pdfProcesor.DeleteImage();
                         return result;
                     }
                     else
                     {
                         _traceMessage.WriteTrace(TraceMessaging.EventSource, TraceLevel.Error, "SolutionID: " + reportModel.SolutionId.ToString(), "GetPDF", "PDF Memory stream null.", "PDF Memory stream null.");
                         //return Request.CreateResponse(HttpStatusCode.NoContent, "PDF not found");
                         return null;
                     }
                 }
                 else
                 {
                     _traceMessage.WriteTrace(TraceMessaging.EventSource, TraceLevel.Error, "SolutionID: " + reportModel.SolutionId.ToString(), "GetPDF", "PDF Memory stream null.", "PDF Memory stream null.");
                     //return Request.CreateResponse(HttpStatusCode.NoContent, "PDF not found");
                     return null;
                 }
             }
             else
             {
                 _traceMessage.WriteTrace(TraceMessaging.EventSource, TraceLevel.Error, "SolutionID: " + reportModel.SolutionId.ToString(), "GetPDF", "User not available.", "User not available.");
                 return null;
             }
         }
         else
         {
             _traceMessage.WriteTrace(TraceMessaging.EventSource, TraceLevel.Error, "SolutionID: " + reportModel.SolutionId.ToString(), "GetPDF", "Report model variable is null.", "Report model variable is null.");
             //return Request.CreateResponse(HttpStatusCode.BadRequest, "Report Parameter is NULL");
             return null;
         }
     }));
 }
Esempio n. 2
0
        private async Task GenerateAndStoreReceiptPDF(Instance instance, string taskId, DataElement dataElement, Type dataElementModelType)
        {
            string app             = instance.AppId.Split("/")[1];
            string org             = instance.Org;
            int    instanceOwnerId = int.Parse(instance.InstanceOwner.PartyId);
            Guid   instanceGuid    = Guid.Parse(instance.Id.Split("/")[1]);

            string     layoutSetsString = _resourceService.GetLayoutSets();
            LayoutSets layoutSets       = null;
            LayoutSet  layoutSet        = null;

            if (!string.IsNullOrEmpty(layoutSetsString))
            {
                layoutSets = JsonConvert.DeserializeObject <LayoutSets>(layoutSetsString);
                layoutSet  = layoutSets.Sets.FirstOrDefault(t => t.DataType.Equals(dataElement.DataType) && t.Tasks.Contains(taskId));
            }

            string layoutSettingsFileContent = layoutSet == null?_resourceService.GetLayoutSettingsString() : _resourceService.GetLayoutSettingsStringForSet(layoutSet.Id);

            LayoutSettings layoutSettings = null;

            if (!string.IsNullOrEmpty(layoutSettingsFileContent))
            {
                layoutSettings = JsonConvert.DeserializeObject <LayoutSettings>(layoutSettingsFileContent);
            }

            object data = await _dataService.GetFormData(instanceGuid, dataElementModelType, org, app, instanceOwnerId, new Guid(dataElement.Id));

            layoutSettings = await FormatPdf(layoutSettings, data);

            XmlSerializer serializer = new XmlSerializer(dataElementModelType);

            using MemoryStream stream = new MemoryStream();

            serializer.Serialize(stream, data);
            stream.Position = 0;

            byte[] dataAsBytes = new byte[stream.Length];
            await stream.ReadAsync(dataAsBytes);

            string encodedXml = Convert.ToBase64String(dataAsBytes);

            string          language    = "nb";
            Party           actingParty = null;
            ClaimsPrincipal user        = _httpContextAccessor.HttpContext.User;

            int?userId = user.GetUserIdAsInt();

            if (userId != null)
            {
                UserProfile userProfile = await _profileService.GetUserProfile((int)userId);

                actingParty = userProfile.Party;

                if (!string.IsNullOrEmpty(userProfile.ProfileSettingPreference?.Language))
                {
                    language = userProfile.ProfileSettingPreference.Language;
                }
            }
            else
            {
                string orgNumber = user.GetOrgNumber().ToString();
                actingParty = await _registerService.LookupParty(new PartyLookup { OrgNo = orgNumber });
            }

            // If layoutset exists pick correct layotFiles
            string formLayoutsFileContent = layoutSet == null?_resourceService.GetLayouts() : _resourceService.GetLayoutsForSet(layoutSet.Id);

            TextResource textResource = await _textService.GetText(org, app, language);

            if (textResource == null && language != "nb")
            {
                // fallback to norwegian if texts does not exist
                textResource = await _textService.GetText(org, app, "nb");
            }

            string textResourcesString = JsonConvert.SerializeObject(textResource);
            Dictionary <string, Dictionary <string, string> > optionsDictionary = await GetOptionsDictionary(formLayoutsFileContent);

            PDFContext pdfContext = new PDFContext
            {
                Data              = encodedXml,
                FormLayouts       = JsonConvert.DeserializeObject <Dictionary <string, object> >(formLayoutsFileContent),
                LayoutSettings    = layoutSettings,
                TextResources     = JsonConvert.DeserializeObject(textResourcesString),
                OptionsDictionary = optionsDictionary,
                Party             = await _registerService.GetParty(instanceOwnerId),
                Instance          = instance,
                UserParty         = actingParty,
                Language          = language
            };

            Stream pdfContent = await _pdfService.GeneratePDF(pdfContext);

            await StorePDF(pdfContent, instance, textResource);

            pdfContent.Dispose();
        }