Search code examples
asp.net-coreasp.net-core-mvcdnx50

Export to pdf using ASP.NET 5


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.


Solution

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

    1 - Wait a bit

    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.

    2 - Fork (or similar)

    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 ;)


    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:

    1 - References

    Add those to your project.json:

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

    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; });
       // ...
    }
    

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

    3 - Usage

    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 - 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.

    First, find your dnx cache for the embedded server. Should be something like:
    C:\Users\<name>\.dnx\packages\jsreport.Embedded\0.8.1.

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

    So, to be plain simple and fool-proof: copy contents (files only) from
    C:\Users\<name>\.dnx\packages\jsreport.Embedded\0.8.1\contents
    into
    C:\Users\<name>\.dnx\packages\jsreport.Embedded\0.8.1\lib\net45.

    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.