示例#1
0
        public string GeneratePdf(PurchaseOrder entity, CancellationToken cancellationToken = default)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            cancellationToken.ThrowIfCancellationRequested();

            // read template
            string templateFile = _uriComposer.ComposeTemplatePath("purchase_order.html");
            string htmlContent  = File.ReadAllText(templateFile);

            htmlContent = MapTemplateValue(htmlContent, entity);

            // prepare destination pdf
            string pdfFileName     = DateTime.Now.ToString("yyyyMMdd_hhmmss_") + Guid.NewGuid().ToString() + ".pdf";
            string fullPdfFileName = _uriComposer.ComposeDownloadPath(pdfFileName);
            ConverterProperties converterProperties = new ConverterProperties();

            HtmlConverter.ConvertToPdf(htmlContent, new FileStream(fullPdfFileName, FileMode.Create), converterProperties);

            return(fullPdfFileName);
        }
示例#2
0
        public async Task <Email> SendEmail(List <string> receiver, List <string> carbonCopy, string subject, string templateFileName, Dictionary <string, string> contentMap, List <Attachment> attachments, CancellationToken cancellationToken = default)
        {
            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }
            if (contentMap == null)
            {
                throw new ArgumentException(nameof(contentMap));
            }
            if (contentMap.Count <= 0)
            {
                throw new Exception("Informasi mapping email tidak boleh kosong");
            }

            string emailReceiver = string.Join(";", receiver);
            string emailCC       = string.Empty;

            if (carbonCopy?.Count > 0)
            {
                emailCC = string.Join(",", carbonCopy);
            }

            string fileTemplate = _uriComposer.ComposeTemplatePath("email/" + templateFileName);

            if (!System.IO.File.Exists(fileTemplate))
            {
                throw new Exception($"File email template tidak ditemukan. File template: {fileTemplate}");
            }

            string template = System.IO.File.ReadAllText(fileTemplate);

            foreach (var key in contentMap.Keys)
            {
                var value = contentMap[key];
                template = template.Replace($"{{{key}}}", value);
            }

            var newEmail = new Email(emailReceiver, subject, template)
            {
                ReceiverCC = emailCC
            };

            if (attachments?.Count > 0)
            {
                foreach (var item in attachments)
                {
                    newEmail.AddAttachment(item);
                }
            }

            var result = await _unitOfWork.Emails.AddAsync(newEmail, cancellationToken);

            if (result == null)
            {
                AddError("Gagal menyimpan email ke dalam antrian");
                return(null);
            }

            // di comment karena commit hanya dilakukan oleh service utama
            //await _unitOfWork.CommitAsync();
            return(result);
        }