JsReport .NET Core - 从 url 生成 PDF

JsReport .NET Core - Generate PDF from url

尝试使用 JsReport 从 url 生成 pdf,但在其 github 存储库中找不到任何文档或示例。

基本上我需要生成 pdf 并将其附加到电子邮件中,我已经设法将数据作为字节 [] 取回,但我似乎无法弄清楚如何使用现有的 View/Action.

这是生成 PDF 以供查看的操作...

[MiddlewareFilter(typeof(JsReportPipeline))]
    public async Task<IActionResult> Pdf(Guid id)
    {
        var serviceOrder = await _serviceOrderService.Get(id);
        if (serviceOrder == null) return new NotFoundResult();

        var model = _mapper.Map<ServiceOrderModel>(serviceOrder);
        HttpContext.JsReportFeature().Recipe(Recipe.PhantomPdf);
        return View(model);
    }

此操作应从 "Details" 获取 pdf 视图并生成我可以附加的 PDF。下面我可以用像 "Hello from pdf" 这样的静态内容生成它,但我不知道如何在 ASPNET Core 中使用我的 "Details" 视图。

public async Task<IActionResult> Email(Guid id)
    {
        var rs = new LocalReporting().UseBinary(JsReportBinary.GetBinary()).AsUtility().Create();

        var report = await rs.RenderAsync(new RenderRequest()
        {
            Template = new Template()
            {
                Recipe = Recipe.PhantomPdf,
                Engine = Engine.None,
                Content = "Hello from pdf",
            }
        });

        var memoryStream = new MemoryStream();
        await report.Content.CopyToAsync(memoryStream);
        memoryStream.Seek(0, SeekOrigin.Begin);

        return new FileStreamResult(memoryStream, "application/pdf") { FileDownloadName = "out.pdf" };
    }

取自 JsReport Github Dotnet Example

[MiddlewareFilter(typeof(JsReportPipeline))]
public IActionResult InvoiceDownload()
{
    HttpContext.JsReportFeature().Recipe(Recipe.ChromePdf)
        .OnAfterRender((r) => HttpContext.Response.Headers["Content-Disposition"] = "attachment; filename=\"myReport.pdf\"");

    return View("Invoice", InvoiceModel.Example());
}

如果您想 return 来自 Asp.net Core Controller Action 方法的文件,请尝试以下方法

[MiddlewareFilter(typeof(JsReportPipeline))]
public async Task<IActionResult> Pdf(Guid id)
{
    var serviceOrder = await _serviceOrderService.Get(id);
    if (serviceOrder == null) return new NotFoundResult();

    var model = _mapper.Map<ServiceOrderModel>(serviceOrder);
    HttpContext.JsReportFeature().Recipe(Recipe.PhantomPdf).OnAfterRender((r) =>
       HttpContext.Response.Headers["Content-Disposition"] = "attachment; filename=\"out.pdf\"");

    return View(model);
}