Export to pdf using ASP.NET 5

后端 未结 5 1732
暗喜
暗喜 2020-12-31 12:34

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

Any help will be appreciated.

5条回答
  •  情话喂你
    2020-12-31 13:30

    I finally figured out a way to generate pdf's from .NET Core (without any .NET framework dependencies) is using Node.js from within my .NET Core application. The following example shows how to implementing a HTML to PDF converter in a clean ASP.NET Core Web Application project (Web API template).

    Install the NuGet package Microsoft.AspNetCore.NodeServices

    In Startup.cs add the line services.AddNodeServices() like this

    public void ConfigureServices(IServiceCollection services)
    {
        // ... all your existing configuration is here ...
    
        // Enable Node Services
        services.AddNodeServices();
    }
    

    Now install the required Node.js packages:

    From the command line change working directory to the root of the .NET Core project and run these commands.

    npm init
    

    and follow the instructions to create the package.json file

    npm install jsreport-core --save
    npm install jsreport-jsrender --save
    npm install jsreport-phantom-pdf --save
    

    Create a file pdf.js in the root of the project containing

    module.exports = function (callback) {
        var jsreport = require('jsreport-core')();
    
        jsreport.init().then(function () {
            return jsreport.render({
                template: {
                    content: '

    Hello {{:foo}}

    ', engine: 'jsrender', recipe: 'phantom-pdf' }, data: { foo: "world" } }).then(function (resp) { callback(/* error */ null, resp.content.toJSON().data); }); }).catch(function (e) { callback(/* error */ e, null); }) };

    Have a look here for more explanation on jsreport-core.

    Now create an action in an Mvc controller that calls this Node.js script

    [HttpGet]
    public async Task MyAction([FromServices] INodeServices nodeServices)
    {
        var result = await nodeServices.InvokeAsync("./pdf");
    
        HttpContext.Response.ContentType = "application/pdf";
    
        string filename = @"report.pdf";
        HttpContext.Response.Headers.Add("x-filename", filename);
        HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "x-filename");
        HttpContext.Response.Body.Write(result, 0, result.Length);
        return new ContentResult();
    }
    

    Off course you can do whatever you want with the byte[] returned from nodeServices, in this example I'm just outputting it from a controller action so it can be viewed in the browser.

    You could also exchange the data between Node.js and .NET Core by a base64 encoded string using resp.content.toString('base64') in pdf.js and use var result = await nodeServices.InvokeAsync("./pdf"); in the action and then decode the base64 encoded string.


    Alternatives

    Most pdf generator solutions still depend on .NET 4.5/4.6 framework. None of the two answers above (JsReport and RazorPDFCore) works for .NET Core yet.

    There seems to be some paid alternatives available if you don't like to use Node.js:

    • NReco.PdfGenerator.LT
    • EVO HTML to PDF Converter Client for .NET Core
    • Winnovative HTML to PDF Converter Client for .NET Core

    I haven't tried any of these though.

    I hope we will soon see some open source progress in this area.

提交回复
热议问题