8

This maybe sounds "easy" but I haven't found a real solution. I need to create Pdf files from Html strings from an API on .Net Core. The library must be free (Not payments or anything related). I found that PDFSharp was a good option, but now I check out that PDFSharp is not available for .Net Core and It doesn't allow to inject a Html string.

So, PDFSharp is a good option for this case? Or what other library would you recommend me?

Thanks a lot.

Lukas
  • 1,101
  • 1
  • 9
  • 27
Roger Ospina
  • 335
  • 1
  • 2
  • 12

3 Answers3

20

I recently ran into the exact same issue myself. There is currently extremely limited ways in dealing with PDFs in general in .NET Core. To go through them...

  • PDF Sharp - Does not support .NET Core/Standard.
  • SelectPDF - Does have a "free" community version hidden in their website footer. Should be useable in most cases. https://selectpdf.com/community-edition/
  • IronPDF - "Enterprise" pricing. Starts at $1.5k
  • WKHTMLTOPDF - This is actually just an executable that someone has written a C# wrapper over the top to run the exe. Not a great solution.
  • iTextSharp - Has "hidden" pricing but apparently this is the only one that specifically will run on Linux under .NET Core (If that's important to you).

IMO the only free one that will do what you need is SelectPDF. And that's saying something because I don't rate the library or the API. But it's free and it works.

More info : https://dotnetcoretutorials.com/2019/07/02/creating-a-pdf-in-net-core/

MindingData
  • 11,181
  • 6
  • 44
  • 66
  • Hi @MindingData, I really appreciate your answer and your time. At the end I decided to use the Google Chrome print-to-pdf feature. This until the manager approve me to use a paid library. Thanks again, have a good day – Roger Ospina Feb 27 '20 at 19:40
  • 1
    IronPDF's pricing is now much cheaper and more flexible starting at $399 for a single project license. – NickG Mar 13 '20 at 11:04
  • 1
    I ended up going for iText7 (iTextSharp). It does have 'hidden' pricing for its paid version but it also has a free and open source version with AGPL licence. – Desmond Jun 18 '20 at 10:12
7

if anyone needs an option with this, here is the final solution that I decided to use.

    var url = "https://stackoverflow.com/questions/564650/convert-html-to-pdf-in-net";
var chromePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
var output = Path.Combine(Environment.CurrentDirectory, "printout.pdf");
using (var p = new Process())
    {
        p.StartInfo.FileName = chromePath;
        p.StartInfo.Arguments = $"--headless --disable-gpu --print-to-pdf={output} {url}";
        p.Start();
        p.WaitForExit();
    }

https://stackoverflow.com/a/59943987/3877537

Thanks @leonard-AB

Roger Ospina
  • 335
  • 1
  • 2
  • 12
5

Install NuGet package - Polybioz.HtmlRenderer.PdfSharp.Core https://www.nuget.org/packages/Polybioz.HtmlRenderer.PdfSharp.Core/1.0.0?_src=template

using PdfSharpCore;
using PdfSharpCore.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;

 var document = new PdfDocument();
var html = "<html><body style='color:black'>PMKJ</body></html>";
 PdfGenerator.AddPdfPages(document, html , PageSize.A4);

 Byte[] res = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    document.Save(ms);
                    res = ms.ToArray();
                }
Anirban Bhadra
  • 211
  • 3
  • 5