使用 ASP.NET 5 导出为 pdf [英] Export to pdf using ASP.NET 5

查看:13
本文介绍了使用 ASP.NET 5 导出为 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 MVC 6 应用程序(DNX Core 5.0 框架).不幸的是,我没有找到任何用于 pdf 导出的库.

I am working on MVC 6 application(DNX Core 5.0 framework). Unfortunately, I don't find any library for pdf export.

任何帮助将不胜感激.

推荐答案

如果您必须依赖 Core,您将有两种选择:

If you must rely on Core you'll have two options:

Core 仍然是 RC1,慢慢地移到 RC2,很快你不会找到太多的库.由于 .NET Core 备受关注,第一个库应该会在几个月后发布,但我猜你至少要等到 RC2 版本.

Core is still RC1, slowly moving to RC2, and you won't find much libs really soon. Since .NET Core is taking much attention, first libs should come out in a few months, but I'd guess you'll have to wait for at least RC2 release.

您可以获取最适合您需求的开源项目、fork(如果在 GitHub 上)或直接下载并开始更新到 .NET Core.我刚刚用 DapperExtensions 完成了它,它的工作就像一个魅力.您甚至可以为您添加一些辣味;)

You can grab an open-source project that best fits your needs, fork (if on GitHub) or just download and start updating to .NET Core. I've just done that with DapperExtensions and it's working like a charm. You can even add some spicy just for you ;)

另一方面,如果您只需要一些可以工作但不需要直接嵌入到 .NET Core 中的东西,我已经设法使 JsReport 工作正常.它将基于 Node 启动它自己的服务器(嵌入式服务器),但集成非常容易(使用 AspNet Core 非常自己的 Dependecy Injection 系统!)并且创建 PDF 没有其他问题.

On the other hand, if you just need something that works but with no direct need of embedding into .NET Core, I've managed to make JsReport work fine. It will start it's very own server (embedded server) based on Node but integration is really easy (with AspNet Core very own Dependecy Injection system!) and PDF are created with no further issue.

如果您对此感兴趣,这里有一些说明:

If that interests you, here are some instructions:

将这些添加到您的 project.json:

Add those to your project.json:

"jsreport.Embedded": "0.8.1",
"jsreport.Client": "0.8.1"

2 - AspNet 集成

之后,按照 此处的 jsReport 说明进行操作.您可以在此处配置 AspNet DI 系统:

2 - AspNet integration

After, follow instructions from jsReport here. You can configure AspNet DI system as here:

public void ConfigureServices(IServiceCollection services)
{
   // ...
   var _server = new EmbeddedReportingServer();
   _server.StartAsync().Wait();
   services.AddInstance<IEmbeddedReportingServer>(_server);
   services.AddSingleton<IReportingService>((s) => { return s.GetRequiredService<IEmbeddedReportingServer>().ReportingService; });
   // ...
}

例如,要使用您只需接收 IReportingService 或从控制器上的 Resolver 手动获取它.

To use you'll just have to either receive an IReportingService or manually grab it from Resolver on your controller, for instance.

public IActionResult SomeReport()
{
   // This is <my> type of usage. It's a bit manual because I'm currently loading reports from DB. You can use it in a diferent way (check jsReport docs).
   var service = Resolver.GetRequiredService<jsreport.Client.IReportingService>();

   var phantomOptions = new jsreport.Client.Entities.Phantom()
   {
      format = "A4",
      orientation = "portrait",
      margin = "0cm"
   };
   phantomOptions.footer = "<h2>Some footer</h2>";
   phantomOptions.footerHeight = "50px";
   phantomOptions.header = "<h2>Some header</h2>";
   phantomOptions.headerHeight = "50px";
   var request = new jsreport.Client.RenderRequest()
   {
      template = new jsreport.Client.Entities.Template()
      {
         content = "<div>Some content for your report</div>",
         recipe = "phantom-pdf",
         name = "Your report name",
         phantom = phantomOptions
      }
   };

   var _report = service.RenderAsync(request).Result;

   // Request file download.
   return File(_report.Content, "application/pdf", "Some fancy name.pdf");
}

4 - 重要提示:您的服务器无法启动(缺少 zip 文件)

由于 AspNet 项目中 NuGet 的更改,您必须手动移动一些不会自动移动的内容文件.

4 - Important: your server won't start (missing a zip file)

Due to changes from NuGet on AspNet projects, you have to manually move some content files which are not moved automatically.

首先,找到嵌入式服务器的 dnx 缓存.应该是这样的:
C:Users<name>.dnxpackagesjsreport.Embedded.8.1.

First, find your dnx cache for the embedded server. Should be something like:
C:Users<name>.dnxpackagesjsreport.Embedded.8.1.

您会注意到那里有一个名为 content 的文件夹.只需将其内容(两个文件:node.exejsreport-net-embedded.zip)复制到 lib et45.

You'll notice a folder called content there. Simply copy it's contents (two files: node.exe and jsreport-net-embedded.zip) into lib et45.

所以,简单明了:从
复制内容(仅限文件)C:Users<name>.dnxpackagesjsreport.Embedded.8.1contents
进入
C:Users<name>.dnxpackagesjsreport.Embedded.8.1lib et45.

那应该可以解决启动问题.请记住:第一次启动将提取文件,应该需要几分钟.之后,它会快得多.

So, to be plain simple and fool-proof: copy contents (files only) from
C:Users<name>.dnxpackagesjsreport.Embedded.8.1contents
into
C:Users<name>.dnxpackagesjsreport.Embedded.8.1lib et45.

That should solve startup issues. Remember: first startup will extract files and should take a few minutes. After that, it will be much much faster.

这篇关于使用 ASP.NET 5 导出为 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆