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.
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.
I have amended the RazorAnt/RazorPDF which was working only for older MVC versions to work with ASP.NET Core. Its RazorPDFCore, available on nuget and github:
Example usage
In your Startup.cs
add the following line before
services.AddMVc();
PLEASE NOTE:
You need to inhere
RazorPDF.Controller
from your base controller before using theViewPdf()
methodNecromancing.
Adding a dependency to NodeJS is subideal IMHO, especially considering .NET Core self-contained deployment.
As per 2017, you could use my port of PdfSharpCore to .NET Core 1.1
Resolves fonts, and it can use images. Comes with a nice sample application. You'll have to replace the DB part, however.
Credits go to:
https://github.com/groege/PdfSharpCore
which is a bit outdated, and doesn't contain a sample on how to use it with images.
Note that you need to register the font-resolver and the imageSource-Implementation before using the respective features:
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:
2 - AspNet integration
After, follow instructions from jsReport here. You can configure AspNet DI system as here:
To use you'll just have to either receive an
IReportingService
or manually grab it fromResolver
on your controller, for instance.3 - Usage
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
andjsreport-net-embedded.zip
) intolib\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.
I know that this question was asked a while ago, and I know that there have been several answers provided already that may well be right for certain projects. But I recently created a GitHub repository that allows for the creation of PDFs directly from your C# code without any requirement for nodejs, javascript, or razor. The feature set is a bit limited at the moment but it generates PDFs with images (.jpg only at this stage), shapes, and formatted text. The library works with .net core 2.0 and has no dependency on any other PDF generation tool.
Please note that this is my own repository: https://github.com/GZidar/CorePDF
I do plan to add functionality over time but at least for now this may provide the basis for others to include simple PDF capability in their own projects without the need for additional tooling.
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 thisNow 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.
and follow the instructions to create the package.json file
Create a file
pdf.js
in the root of the project containingHave a look here for more explanation on
jsreport-core
.Now create an action in an Mvc controller that calls this Node.js script
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')
inpdf.js
and usevar result = await nodeServices.InvokeAsync<byte[]>("./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:
I haven't tried any of these though.
I hope we will soon see some open source progress in this area.