在ASP.NET Core中将html导出为pdf

在ASP.NET Core中将html导出为pdf

问题描述:

我想将一段html导出为pdf文件,但是我没有任何兼容的nuget包.

I want to export a piece of html to a pdf file but i do not any compatible nuget package.

当我尝试安装任何人时:"X与netcoreapp1.0(.NETCoreApp,Version = v1.0)不兼容."

When I try to install anyone: "X not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0)."

有人知道使用asp.net核心导出到pdf的任何方式吗?

Does anyone know any way to export to a pdf using asp.net core??

您可以使用 jsreport .net SDK 如果您使用的是.net core 2.0,那么它也不需要更复杂的节点服务.其中包括其他功能过滤器,可将您现有的剃刀视图转换为pdf.从文档:

You can use jsreport .net sdk if you are in .net core 2.0 also without more complex node services. This includes among other features filters to convert your existing razor views into pdf. From the docs:

1. 安装nugets jsreport.Binary jsreport.AspNetCore

1. Install nugets jsreport.Binary, jsreport.Local and jsreport.AspNetCore

2. 在Startup.cs中,将其配置为以下

2. In you Startup.cs configure it as the following

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();              
    services.AddJsReport(new LocalReporting()
        .UseBinary(JsReportBinary.GetBinary())
        .AsUtility()
        .Create());
}

3. 然后,您需要将MiddlewareFilter属性添加到特定操作中,并指定要使用的转换.在这种情况下,将html转换为pdf.

3. Then you need to add MiddlewareFilter attribute to the particular action and specify which conversion you want to use. In this case html to pdf conversion.

[MiddlewareFilter(typeof(JsReportPipeline))]
public IActionResult Invoice()
{
    HttpContext.JsReportFeature().Recipe(Recipe.ChromePdf);
    return View();
}

您可以在JsReportFeature()上找到许多其他的页眉,页脚或页面布局选项.请注意,您也可以通过html生成excel文件的方式相同.在文档中查看更多信息.

You can reach bunch of other options for headers, footers or page layout on JsReportFeature(). Note that the same way you can also produce excel files from html. See more information in the documentation.

PS:我是jsreport的作者.

PS: I'm the author of jsreport.