Can PDFSharp create Pdf file from a Html string in Net Core?
Asked Answered
P

5

12

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.

Pell answered 21/2, 2020 at 16:26 Comment(0)
P
28

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/

Pannier answered 25/2, 2020 at 1:33 Comment(5)
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 dayPell
IronPDF's pricing is now much cheaper and more flexible starting at $399 for a single project license.Washable
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.Vookles
Just to add one to the list, I've used PrinceXML in the past. It's another one that isn't .net, but has a .net wrapper. It's free for non-commercial use. Works well.Pskov
Select.Pdf can be quite slow when converting HTML to PDF. PuppeteerSharp or Playwright might be better options to consider.Sharecropper
P
16

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

    var url = "https://mcmap.net/q/48109/-convert-html-to-pdf-in-net-closed";
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://mcmap.net/q/48109/-convert-html-to-pdf-in-net-closed

Thanks @leonard-AB

Pell answered 27/2, 2020 at 19:37 Comment(3)
I love the simplicity of this one!Lafollette
Yeah, I like it way more than using CefSharp or Puppetteer. Is there any disadvantage? I cannot think of any.Ecosphere
Since it uses Chrome, how's the memory use/CPU utilization? Does it not kill the server if you have a few going concurrently?Pskov
J
9

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();
                }
Janejanean answered 26/1, 2021 at 23:46 Comment(4)
what if we need it for .net framework 5.0 as it not working in it.Harlen
@HeemanshuBhalla do you have any update on this issue?Sharecropper
This works unless you're trying to convert anything with tables. In that case, I've found that Select.HtmlToPdf.NetCore works.Cowboy
HtmlRenderer appears to be abandoned. It haven't been updated in many years.Offside
C
0

I run through this problem last few hours and found solution that work for me in .net6 C#.

install 3 library by Nugget:

here is my function:

            [HttpPost]
            public IActionResult PrintPDF([FromBody] string htmlString)
            {
                using (var ms = new MemoryStream())
                {
                    // Parse the HTML
                    PdfDocument pdfx = PdfGenerator.GeneratePdf(htmlString, PageSize.A4);
                    pdfx.Save(ms);
                  
                    //return File(ms, "application/pdf", "file.pdf");
                    var result = new FileContentResult(ms.ToArray(), "application/pdf");
                    result.FileDownloadName = "file.pdf";
                    return result;
                }
            }
 
Conlan answered 17/1, 2023 at 8:54 Comment(2)
Hi. Have you found a way to use CSS properly with the HtmlRenderer.Core? I try to use flexbox with no luck and there no documentation online. I would appreciate any help. Thanks!Volplane
Hi @Volplane , I put all the styles inside the HTML string and the CSS work properly. hope it helps.Conlan
W
-3

I know this is an old question, but there are free .Net Core options, there are a few PDFSharp ports to Core, the one I use is

https://github.com/ststeiger/PdfSharpCore

It's being actively developed, at the point of writing this answer the update was 2 weeks ago.

It works well, only point to note is one of the libraries used does not have a very permissive licence,

https://github.com/ststeiger/PdfSharpCore/issues/285

But if you can work within the bounds of the licence then this is a good way to go with .Net Core, I am running a few projects on .Net7 with this package successfully.

Winou answered 20/12, 2022 at 3:58 Comment(3)
Can it handle HTML?Batter
IIn terms of taking HTML and rendering a PDF from it?, Not as far as I could see, I am using the following package github.com/j-petty/HtmlRendererCore to render a PDF from HTML.Winou
So this does not answer the question asked here.Batter

© 2022 - 2024 — McMap. All rights reserved.