예제 #1
0
        /// <inheritdoc/>
        public async Task GenerateAndStoreReceiptPDF(Instance instance)
        {
            string      app             = instance.AppId.Split("/")[1];
            string      org             = instance.Org;
            int         instanceOwnerId = int.Parse(instance.InstanceOwner.PartyId);
            Application application     = _appResourcesService.GetApplication();
            Guid        instanceGuid    = Guid.Parse(instance.Id.Split("/")[1]);

            DataType dataModelDataElement     = application.DataTypes.Find(element => element.AppLogic != null);
            Guid     dataModelDataElementGuid = Guid.Parse(instance.Data.Find(element => element.DataType.Equals(dataModelDataElement.Id))?.Id);

            Stream dataStream = await _dataService.GetBinaryData(org, app, instanceOwnerId, instanceGuid, dataModelDataElementGuid);

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

            string encodedXml = System.Convert.ToBase64String(dataAsBytes);

            byte[] formLayout    = _appResourcesService.GetAppResource(org, app, _appSettings.FormLayoutJSONFileName);
            byte[] textResources = _appResourcesService.GetText(org, app, "resource.nb.json");

            string formLayoutString    = GetUTF8String(formLayout);
            string textResourcesString = GetUTF8String(textResources);

            PDFContext pdfContext = new PDFContext
            {
                Data          = encodedXml,
                FormLayout    = JsonConvert.DeserializeObject(formLayoutString),
                TextResources = JsonConvert.DeserializeObject(textResourcesString),
                Party         = await _registerService.GetParty(instanceOwnerId),
                Instance      = instance
            };

            Stream pdfContent;

            try
            {
                pdfContent = await GeneratePDF(pdfContext);
            }
            catch (Exception exception)
            {
                _logger.LogError($"Could not generate pdf for {instance.Id}, failed with message {exception.Message}");
                return;
            }

            try
            {
                await StorePDF(pdfContent, instance);
            }
            catch (Exception exception)
            {
                _logger.LogError($"Could not store pdf for {instance.Id}, failed with message {exception.Message}");
                return;
            }
            finally
            {
                pdfContent.Dispose();
            }
        }
        public IActionResult Index(string org, string app, string id)
        {
            byte[] fileContent = _appResourceService.GetAppResource(org, app, id);

            if (fileContent != null)
            {
                return(new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower())));
            }

            return(StatusCode(404));
        }
예제 #3
0
        /// <inheritdoc/>
        public async Task GenerateAndStoreReceiptPDF(Instance instance, DataElement dataElement)
        {
            string      app             = instance.AppId.Split("/")[1];
            string      org             = instance.Org;
            int         instanceOwnerId = int.Parse(instance.InstanceOwner.PartyId);
            Application application     = _appResourcesService.GetApplication();
            Guid        instanceGuid    = Guid.Parse(instance.Id.Split("/")[1]);

            Stream dataStream = await _dataService.GetBinaryData(org, app, instanceOwnerId, instanceGuid, new Guid(dataElement.Id));

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

            string encodedXml = Convert.ToBase64String(dataAsBytes);

            UserContext userContext = await _userHelper.GetUserContext(_httpContextAccessor.HttpContext);

            UserProfile userProfile = await _profileService.GetUserProfile(userContext.UserId);

            byte[]       formLayout   = _appResourcesService.GetAppResource(org, app, _appSettings.FormLayoutJSONFileName);
            TextResource textResource = await _textService.GetText(org, app, userProfile.ProfileSettingPreference.Language);

            string formLayoutString    = GetUTF8String(formLayout);
            string textResourcesString = JsonConvert.SerializeObject(textResource);

            PDFContext pdfContext = new PDFContext
            {
                Data          = encodedXml,
                FormLayout    = JsonConvert.DeserializeObject(formLayoutString),
                TextResources = JsonConvert.DeserializeObject(textResourcesString),
                Party         = await _registerService.GetParty(instanceOwnerId),
                Instance      = instance,
                UserProfile   = userProfile,
                UserParty     = userProfile.Party
            };

            Stream pdfContent;

            try
            {
                pdfContent = await GeneratePDF(pdfContext);
            }
            catch (Exception exception)
            {
                _logger.LogError($"Could not generate pdf for {instance.Id}, failed with message {exception.Message}");
                return;
            }

            try
            {
                await StorePDF(pdfContent, instance, textResource);
            }
            catch (Exception exception)
            {
                _logger.LogError($"Could not store pdf for {instance.Id}, failed with message {exception.Message}");
                return;
            }
            finally
            {
                pdfContent.Dispose();
            }
        }