Exemple #1
0
        /// <inheritdoc />
        public async Task <PdfRendererResult> Render(string templateDirectoryName, string pdfTemplateName, string filenameTemplateName, string headerTemplateName, string footerTemplateName, PdfOptions?options, object viewModel)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            options ??= new PdfOptions(); // fall back to default options

            if (options.HeaderHtml == null && !string.IsNullOrWhiteSpace(headerTemplateName))
            {
                try
                {
                    options.HeaderHtml = (await _templater.Render(templateDirectoryName, headerTemplateName, viewModel)).Trim();
                    _logger.LogTrace($"Using header from template '{headerTemplateName}'.");
                }
                catch
                {
                    // do nothing here. Happens usually when template does not exist, and we don't want anything to happen in this case
                }
            }

            if (options.FooterHtml == null && !string.IsNullOrWhiteSpace(footerTemplateName))
            {
                try
                {
                    options.FooterHtml = (await _templater.Render(templateDirectoryName, footerTemplateName, viewModel)).Trim();
                    _logger.LogTrace($"Using footer from template '{footerTemplateName}'.");
                }
                catch
                {
                    // do nothing here. Happens usually when template does not exist, and we don't want anything to happen in this case
                }
            }

            var result = new PdfRendererResult
            {
                Html     = (await _templater.Render(templateDirectoryName, pdfTemplateName, viewModel)).Trim(),
                Filename = (await _templater.Render(templateDirectoryName, filenameTemplateName, viewModel)).Trim(),
            };

            stopwatch.Stop();
            _logger.LogTrace($"Rendering of HTML templates took {stopwatch.ElapsedMilliseconds}ms.");

            stopwatch.Restart();
            result.Bytes = await _pdfer.HtmlToPdf(result.Html, options);

            _logger.LogTrace($"Conversion to PDF took {stopwatch.ElapsedMilliseconds}ms, the result is {result.Bytes.Length:N0} bytes.");

            return(result);
        }
Exemple #2
0
        public virtual async Task RenderAndSend(string recipientAddress, string templateDirectoryName, string subjectTemplateName, string htmlTemplateName, string textTemplateName, object viewModel, params IEmailAttachment[] attachments)
        {
            var subject = (await _templater.Render(templateDirectoryName, $"{subjectTemplateName}", viewModel)).Trim();
            var html    = (await _templater.Render(templateDirectoryName, $"{htmlTemplateName}", viewModel)).Trim();

            string text;

            try
            {
                text = (await _templater.Render(templateDirectoryName, $"{textTemplateName}", viewModel)).Trim();
            }
            catch // support e-mails without a text version, just to avoid having to "duplicate" the HTML template all the time
            {
                text = "";
            }

            await _mailer.Send(recipientAddress, subject, html, text, attachments);
        }
Exemple #3
0
        /// <inheritdoc cref="ISmsRenderer.RenderAndSendAsync(string,string,string,object)"/>
        /// <exception cref="ArgumentException">The <paramref name="recipientAddress"/> is null or whitespace.</exception>
        public virtual async Task RenderAndSendAsync(string recipientAddress, string templateDirectoryName,
                                                     string textTemplateName, object viewModel)
        {
            if (IsNullOrWhiteSpace(recipientAddress))
            {
                throw new ArgumentException(@"The recipient must not be null.", nameof(recipientAddress));
            }

            string text;

            try
            {
                text = (await _templater.Render(templateDirectoryName, $"{textTemplateName}", viewModel)).Trim();
            }
            catch
            {
                text = Empty;
            }

            await _smsMessagingGateway.SendAsync(recipientAddress, text);
        }