Generating Pdf From Html C#

Generating Pdf From Html C#

  1. Generating Pdf From Latex Files
  2. Generating Pdf From Html C#

Generating Pdf From Latex Files

Active2 years, 3 months ago

To generate a PDF from a HTML file with C# in Windows Forms, we are going to use Pechkin, the.NET Wrapper for wkhtmltopdf DLL, library that uses Webkit engine to convert HTML pages to PDF. Pechkin is available as a NuGet package, in most cases you should use SynchronizedPechkin as it protects multithreaded code from crashing the lib. You can use pdfsharp library for generating pdf file from c# applications check this one. Create PDF file from C# is explain the simple step by step methods to generate pdf file from c#. There are PDF generators that use HTML as templates or Word-documents (eg: www.docati.com). I prefer those, since they provide a nice.

Pdf

I am using the following library to generate a PDF document (https://www.nuget.org/packages/HtmlRenderer.PdfSharp).

I am having some odd issues while trying to use the library. The HTML string is being appropriately passed, yet I am receiving the following error.

However, the the html variable looks as follows:

I saved the HTML in a separate file and opened it in a browser and it displayed just.

Additionally, I tried wrapping the HTML in <html><body></body></html>. The odd thing is, I received no error, but got a blank PDF.

In case it helps, the HTML is generated by the following:

and passed into a View

by a Controller method

Any advice would be appreciated.

Mr Lister
36.6k11 gold badges79 silver badges123 bronze badges
KellyMKellyM

Generating Pdf From Html C#

8521 gold badge15 silver badges44 bronze badges

1 Answer

You have to call doc.AddPage() before you can use a document. It returns a reference to the page which you can use for setting dimensions or getting the XGraphics/renderContext.

Generating Pdf From Html C#

Can't instantly find it, but there is a very similar question either here on SF or on the PdfSharp / HtmlRenderer forum/help pages.

TyethTyeth
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not the answer you're looking for? Browse other questions tagged c#pdf or ask your own question.