I want to generate a PDF by passing HTML contents to a function. I have made use of iTextSharp for this but it does not perform well when it encounters tables and the layout
Winnovative offer a .Net PDF library that supports HTML input. They offer an unlimited free trial. Depending on how you wish to deploy your project, this might be sufficient.
ABCpdf.NET (http://www.websupergoo.com/abcpdf-5.htm)
We use and recommend.
Very good component, it not only convert a webpage to PDF like an image but really convert text, image, formatting, etc...
It's not free but it's cheap.
Last Updated: October 2020
This is the list of options for HTML to PDF conversion in .NET that I have put together (some free some paid)
GemBox.Document
PDF Metamorphosis .Net
HtmlRenderer.PdfSharp
PuppeteerSharp
EO.Pdf
WnvHtmlToPdf_x64
IronPdf
Spire.PDF
Aspose.Html
EvoPDF
ExpertPdfHtmlToPdf
Zetpdf
PDFtron
WkHtmlToXSharp
SelectPDF
If none of the options above help you you can always search the NuGet packages:
https://www.nuget.org/packages?q=html+pdf
I found the following library more effective in converting html to pdf.
nuget: https://www.nuget.org/packages/Select.HtmlToPdf/
Quite likely most projects will wrap a C/C++ Engine rather than implementing a C# solution from scratch. Try Project Gotenberg.
To test it
docker run --rm -p 3000:3000 thecodingmachine/gotenberg:6
Curl Example
curl --request POST \
--url http://localhost:3000/convert/url \
--header 'Content-Type: multipart/form-data' \
--form remoteURL=https://brave.com \
--form marginTop=0 \
--form marginBottom=0 \
--form marginLeft=0 \
--form marginRight=0 \
-o result.pdf
C# sample.cs
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;
using static System.Console;
namespace HelloWorld
{
class Program
{
public static async Task Main(string[] args)
{
try
{
var client = new HttpClient();
var formContent = new MultipartFormDataContent
{
{new StringContent("https://duckduckgo.com/"), "remoteURL"},
{new StringContent("0"), "marginTop" }
};
var result = await client.PostAsync(new Uri("http://localhost:3000/convert/url"), formContent);
await File.WriteAllBytesAsync("duckduck.com.pdf", await result.Content.ReadAsByteArrayAsync());
}
catch (Exception ex)
{
WriteLine(ex);
}
}
}
}
To compile
csc sample.cs -langversion:latest -reference:System.Net.Http.dll && mono ./sample.exe
I was also looking for this a while back. I ran into HTMLDOC http://www.easysw.com/htmldoc/ which is a free open source command line app that takes an HTML file as an argument and spits out a PDF from it. It's worked for me pretty well for my side project, but it all depends on what you actually need.
The company that makes it sells the compiled binaries, but you are free to download and compile from source and use it for free. I managed to compile a pretty recent revision (for version 1.9) and I intend on releasing a binary installer for it in a few days, so if you're interested I can provide a link to it as soon as I post it.
Edit (2/25/2014): Seems like the docs and site moved to http://www.msweet.org/projects.php?Z1