在 ASP.NET Core 中将 html 导出为 pdf [英] Export html to pdf in ASP.NET Core

查看:40
本文介绍了在 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 core导出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.Binaryjsreport.Localjsreport.AspNetCore

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

2.在你的Startup.cs中配置如下

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.

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

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