public async Task Invoke(HttpContext context)
        {
            // put to the Response.Body our own stream so we can later forward the underlying buffer to jsreport
            var buffer = new MemoryStream();
            var originalResponseStream = context.Response.Body;

            context.Response.Body = buffer;

            context.Features.Set <IJsReportFeature>(new JsReportFeature(context));

            try
            {
                await _next(context);

                var feature = context.Features.Get <IJsReportFeature>();

                // we don't deal with failed request and let the other middlewares handle it
                if (!feature.Enabled || context.Response.StatusCode != 200)
                {
                    buffer.Seek(0, SeekOrigin.Begin);
                    await buffer.CopyToAsync(originalResponseStream);

                    return;
                }

                buffer.Seek(0, SeekOrigin.Begin);
                var    reader       = new StreamReader(buffer);
                string responseBody = await reader.ReadToEndAsync();

                feature.RenderRequest.Template.Content = responseBody;

                var report = await _renderService.RenderAsync(feature.RenderRequest);

                context.Response.ContentType = report.Meta.ContentType;
                context.Response.Headers["Content-Disposition"] = report.Meta.ContentDisposition;

                feature.AfterRender?.Invoke(report);

                await report.Content.CopyToAsync(originalResponseStream);
            }
            finally
            {
                // finally copy the jsreport output stream to the Response.Body
                context.Response.Body = originalResponseStream;
            }
        }
Ejemplo n.º 2
0
        public async Task <MemoryStream> RenderPdfAsync <TModel>(string viewName, TModel model, ITenantInfo tenantInfo = null)
            where TModel : TemplateViewModel
        {
            if (_renderService == null)
            {
                throw new Exception("JSReport render service is not available");
            }

            var htmlContent = await RenderViewAsync(viewName, model, tenantInfo).ConfigureAwait(false);

            var pdf = await _renderService.RenderAsync(new RenderRequest()
            {
                Template = new Template()
                {
                    Content = htmlContent,
                    Engine  = Engine.None,
                    Recipe  = Recipe.ChromePdf,
                    Chrome  = new Chrome()
                    {
                        DisplayHeaderFooter = true,
                        MarginTop           = "2cm",
                        MarginLeft          = "2cm",
                        MarginRight         = "2cm",
                        MarginBottom        = "2cm",
                        HeaderTemplate      = "",
                        FooterTemplate      = "<span style='color:black; font-size:8pt; font-family: sans-serif !important; width:100%;text-align:right;margin-right:2cm;'>"
                                              + Messages.page
                                              + " <span class=\"pageNumber\"></span> "
                                              + Messages.of
                                              + " <span class=\"totalPages\"></span></span>"
                    }
                }
            }).ConfigureAwait(false);

            var ms = new MemoryStream();

            pdf.Content.CopyTo(ms);

            return(ms);
        }
Ejemplo n.º 3
0
        public async Task <Stream> GeneratePdf(
            string bodyTemplate,
            string headerTemplate               = null,
            string footerTemplate               = null,
            object templateData                 = null,
            PageMargins margins                 = null,
            PageFormat format                   = PageFormat.A4,
            PageOrientation orientation         = PageOrientation.Portrait,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var renderRequest = new RenderRequest
            {
                Template = new Template
                {
                    Engine  = Engine.Handlebars,
                    Recipe  = Recipe.ChromePdf,
                    Content = bodyTemplate,
                    Helpers = _handlebarsHelpers,
                    Chrome  = new Chrome
                    {
                        Landscape           = orientation == PageOrientation.Landscape,
                        Format              = format.ToString(),
                        DisplayHeaderFooter = true,
                        HeaderTemplate      = headerTemplate ?? "&nbsp;",
                        FooterTemplate      = footerTemplate ?? "&nbsp;",
                        MarginTop           = margins?.TopInPx != null ? $"{margins.TopInPx}px" : null,
                        MarginRight         = margins?.RightInPx != null ? $"{margins.RightInPx}px" : null,
                        MarginBottom        = margins?.BottomInPx != null ? $"{margins.BottomInPx}px" : null,
                        MarginLeft          = margins?.LeftInPx != null ? $"{margins.LeftInPx}px" : null,
                        MediaType           = MediaType.Print
                    }
                },
                Data = templateData
            };

            Report report = await _renderService.RenderAsync(renderRequest, cancellationToken);

            return(report.Content);
        }
 public Task <Report> RenderAsync(RenderRequest request, CancellationToken ct = default(CancellationToken))
 {
     return(_renderService.RenderAsync(request));
 }